(I) Do Sinatra Series 1

Your first Sinatra Program

Purpose

This is the start of a multi-part series of Sinatra introductory lectures. While Sinatra is far simpler than Rails, and easier to understand, it still is a new framework. These series will introduce you step by step to Sinatra and in particular the aspects that are related to Scaling.

Mission

  • Implementing a simple service in Sinatra
  • Service = Web Service = Web API
  • It will offer CRUD operations on a database
  • It will use Sinatra and ActiveRecord

Key technologies, standards, libraries, etc.

  • Like Rails, developing Sinatra apps involves many other pieces of technology
  • Some of it is very reminiscent of Rails
  • Some is new.
    • Ruby
    • Sinatra
    • ActiveRecord
    • Erb
    • Postgres
    • JSON
    • Minitest

Setting Up Sinatra

Checking your installation

Don’t worry if you don’t have precisely the same version number. Ask if you’re not sure


rbenv -v; ruby -v; git --version; psql --version
rbenv 1.2.0
ruby 3.0.3
git version 2.28.0
psql (PostgreSQL) 13.3

Ensure you have these gems


sinatra (2.1.0)
sinatra-activerecord (2.0.25)
  • Unlike Rails, Sinatra does not dictate a directory structure
  • I have found the following to work well
  • The reason it looks a tiny bit like Rails is because it accommodates ActiveRecord most easily that way

/config
  database.yml
/db
  /migrate
    ...migration files
  schema.rb (generated)
/views
  ...erb files
/test
  ... test files
/models
  ... activerecord model files
config.ru
Gemfile
app.rb
Rakefile

Key files



# Gemfile
source "https://rubygems.org"

gem "rake"
gem "sinatra"
gem "activerecord"
gem "sinatra-activerecord"
gem "pg"
gem "rack-test"

# Rakefile
require './app'
require 'sinatra/activerecord/rake'

# app.rb
require 'sinatra'
require 'sinatra/activerecord'
require_relative 'models/user.rb'

get '/' do
  "Hello Sinatra!"
end


# config.ru
require './app'
run Sinatra::Application

# database.yml, where we declare various DB
# settings and parameters
development:
  adapter: postgresql
  encoding: unicode
  database: sin_series_dev
  pool: 2

test:
  adapter: postgresql
  encoding: unicode
  database: sin_series_test
  pool: 2

production:
  adapter: postgresql
  encoding: unicode
  database: sin_series_prod
  pool: 2
  username: your_username
  password: your_password

Using Rack::Test to do TDD

  • Add gem rack-test to Gemfile, bundle
  • Notice that all your server does so far is to return hello sinatra when it receives a GET at root
  • Lets test that
  • We are going to be using Rack::Test with Minitest/Spec


# test/root_test.rb
ENV['APP_ENV'] = 'test'

require_relative '../app.rb'
require 'minitest/autorun'
require 'rack/test'

include Rack::Test::Methods

def app
  Sinatra::Application
end

describe 'The HelloWorld App' do

  it "says hello" do
    get '/'
    last_response.ok?
    assert_equal last_response.body, 'Hello Sinatra!'
  end
end

  • When you run this program (ruby test/root_test.rb) it will run the test and it should succeed
  • This is the basic pattern we will use for testing.