Sinatra Flashcards
Sinatra
steps to create a new Sinatra app
mkdir app && touch app/Gemfile app/app.rb
mkdir app/views && touch app/views/layout.erb app/views/index.erb
mkdir app/test && touch app/test/app_test.rb
mkdir app/assets
mkdir app/public
Gemfile
source ‘https://rubygems.org’ ruby ‘2.3.1’
gem ‘sinatra’ gem ‘minitest’
$ bundle install
app.rb
require ‘sinatra’
require ‘sinatra/reloader’
configure do
enable :sessions
set :session_secret, ‘secret’
set :erb, escape_html: true
end
get ‘/’ do
erb :index
end
Sinatra
escape all HTML by default
set :erb, escape_html: true
Sinatra
set up a test file using Minitest and Sinatra Rack
touch test/app_test.rb
app_test.rb
require ‘minitest’
require ‘minitest/autorun’
require ‘rack/test’
require_relative ‘../app.rb’
Minitest::Reporters.use!
class AppTest
include Rack::Test::Methods
def app
Sinatra::Application
end
def setup
end
def teardown
end
def session
last_request.env[“rack.session”]
end
Sinatra
an instance of Rack::MockResponse
; comes from the Rack::Test::Methods module; contains information about the response generated by the application returned by a method called app
last_response
Sinatra
an instance of Rack::MockRequest
; comes from the Rack::Test::Methods module; it represents the last request to the app
last_request
Sinatra
set a custom HTTP header in the response from a Sinatra app
header[‘Content-Type’] = ‘text/html’
Sinatra
specify the response status code in a Sinatra app
status 422
Sinatra
mkdir app && touch app/Gemfile app/app.rb
mkdir app/views && touch app/views/layout.erb app/views/index.erb
mkdir app/test && touch app/test/app_test.rb
mkdir app/assets
mkdir app/public
Gemfile
source ‘https://rubygems.org’ ruby ‘2.3.1’
gem ‘sinatra’ gem ‘minitest’
$ bundle install
app.rb
require ‘sinatra’
require ‘sinatra/reloader’
configure do
enable :sessions
set :session_secret, ‘secret’
set :erb, escape_html: true
end
get ‘/’ do
erb :index
end
steps to create a new Sinatra app
Sinatra
set :erb, escape_html: true
escape all HTML by default
Sinatra
touch test/app_test.rb
app_test.rb
require ‘minitest’
require ‘minitest/autorun’
require ‘rack/test’
require_relative ‘../app.rb’
Minitest::Reporters.use!
class AppTest
include Rack::Test::Methods
def app
Sinatra::Application
end
def setup
end
def teardown
end
def session
last_request.env[“rack.session”]
end
set up a test file using Minitest and Sinatra Rack
Sinatra
last_response
an instance of Rack::MockResponse
; comes from the Rack::Test::Methods module; contains information about the response generated by the application returned by a method called app
Sinatra
last_request
an instance of Rack::MockRequest
; comes from the Rack::Test::Methods module; it represents the last request to the app
Sinatra
header[‘Content-Type’] = ‘text/html’
set a custom HTTP header in the response from a Sinatra app
Sinatra
status 422
specify the response status code in a Sinatra app
Sinatra
object available to a Sinatra application that contains methods pertaining to logging messages
logger
ex
logger.info “message”