Sinatra Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Sinatra

steps to create a new Sinatra app

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Sinatra

escape all HTML by default

A

set :erb, escape_html: true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Sinatra

set up a test file using Minitest and Sinatra Rack

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

last_response

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Sinatra

an instance of Rack::MockRequest; comes from the Rack::Test::Methods module; it represents the last request to the app

A

last_request

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Sinatra

set a custom HTTP header in the response from a Sinatra app

A

header[‘Content-Type’] = ‘text/html’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Sinatra

specify the response status code in a Sinatra app

A

status 422

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

steps to create a new Sinatra app

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Sinatra

set :erb, escape_html: true

A

escape all HTML by default

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

set up a test file using Minitest and Sinatra Rack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Sinatra

last_response

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Sinatra

last_request

A

an instance of Rack::MockRequest; comes from the Rack::Test::Methods module; it represents the last request to the app

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Sinatra

header[‘Content-Type’] = ‘text/html’

A

set a custom HTTP header in the response from a Sinatra app

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Sinatra

status 422

A

specify the response status code in a Sinatra app

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Sinatra

object available to a Sinatra application that contains methods pertaining to logging messages

A

logger

ex

logger.info “message”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Sinatra

with ‘sinatra/reloader’, let Sinatra know about other files that need to be reloaded when changes are saved

A

also_reload(‘filepath.ext’)

17
Q

group code you only want executed in development environment without multiple conditional statements

A

configure(:development) do

require ‘sinatra/reloader’

also_reload ‘some_file.rb’

end