Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark-jordanovic-lewis/85a3d0c99ffdb7b8fdb6f9af10b10e19 to your computer and use it in GitHub Desktop.
Save mark-jordanovic-lewis/85a3d0c99ffdb7b8fdb6f9af10b10e19 to your computer and use it in GitHub Desktop.
Sinatra cheatsheet

Useful commands

last_response
    .body

last_request
    .path
    .url
    .session
    .cookies

Test

 get "/", nil, "rack.session" => {"uid" => '12345'}
 # last_request.session => {"uid"=>"12345"}

Request object

# app running on http://example.com/example
get '/foo' do
  t = %w[text/css text/html application/javascript]
  request.accept              # ['text/html', '*/*']
  request.accept? 'text/xml'  # true
  request.preferred_type(t)   # 'text/html'
  request.body                # request body sent by the client (see below)
  request.scheme              # "http"
  request.script_name         # "/example"
  request.path_info           # "/foo"
  request.port                # 80
  request.request_method      # "GET"
  request.query_string        # ""
  request.content_length      # length of request.body
  request.media_type          # media type of request.body
  request.host                # "example.com"
  request.get?                # true (similar methods for other verbs)
  request.form_data?          # false
  request["SOME_HEADER"]      # value of SOME_HEADER header
  request.referrer            # the referrer of the client or '/'
  request.user_agent          # user agent (used by :agent condition)
  request.cookies             # hash of browser cookies
  request.xhr?                # is this an ajax request?
  request.url                 # "http://example.com/example/foo"
  request.path                # "/example/foo"
  request.ip                  # client IP address
  request.secure?             # false (would be true over ssl)
  request.forwarded?          # true (if running behind a reverse proxy)
  request.env                 # raw env hash handed in by Rack
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment