Sinatra
https://sinatrarb.com
Sinatra
Sinatra is a *DSL for quickly creating web applications
in Ruby with minimal e ort
# myapp.rb
require 'sinatra'
get '/' do
'Hello world!'
end
It’s a Rack-based application framework
Rack is a Ruby web server interface
* A domain-speci c language (DSL) is a computer language specialized to a particular application domain.
fi
ff
Routes
A route is an HTTP method paired with a UReL-matching pattern. Each route is
associated with a block
Routes are matched in the order they are de ned. The rst route that matches the
request is invoked.
get '/' do
.. show something ..
end
post '/' do options '/' do
.. create something .. .. appease something ..
end end
put '/' do link '/' do
.. replace something .. .. affiliate something ..
end end
patch '/' do unlink '/' do
.. modify something .. .. separate something ..
end end
delete '/' do
.. annihilate something ..
end
fi
fi
Routes - Query parameters
# matches "GET /posts?title=foo&author=bar"
get '/posts' do
# matches "GET /posts?title=foo&author=bar"
title = params['title']
author = params['author']
# uses title and author variables; query is optional to the /posts route
end
get '/posts' do
title = params['title']
author = params['author']
# uses title and author variables; query is optional to the /posts route
end
Routes - named parameters (path params)
get '/users/:name' do
# matches "GET /users/foo" and "GET /users/bar"
# params['name'] is 'foo' or 'bar'
"Hello #{params['name']}!"
end
get '/users/:name' do |n|
# matches "GET /hello/foo" and "GET /hello/bar"
# params['name'] is 'foo' or 'bar'
# n stores params['name']
"Hello #{n}!"
end
Routes - examples
get '/hola-mundo' do
… show something … GET http://localhost:9292/hola-mundo
end
get ‘/users’ do
GET http://localhost:9292/users … show all users …
end
get ‘/users’ do
… params[:sort] … GET http://localhost:9292/users?sort=firstName
end
get ‘/user/:id’ do
… show user with id :id …
GET http://localhost:9292/user/5 params[:id]
end
Routes - examples (POST)
post '/users' do
… show user with id :id …
JSON.parse(request.body.read)
end
HTTP Request
POST http://localhost:9292/users
{ "firstName": 'test' }
curl -X POST -H 'Content-Type: application/json' \
http://localhost:4567/users --data '{"firstName": "test"}'
Request
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_param"] # value of some_param parameter. [] is a shortcut to the params hash.
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
https://sinatrarb.com/intro#Accessing%20the%20Request%20Object
Responses
Most commonly, this is a string. But other values are also accepted.
You can return any object that would either be a valid Rack response, Rack body object or HTTP status code:
[status (Fixnum), headers (Hash), response body (responds to #each)]
halt 402, {'Content-Type' => 'text/plain'}, 'revenge'
Views
get '/' do
'hola'
end
get '/' do
"<form action="/login" method="post">
<label for="uname"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="uname" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<button type="submit">Login</button>
</form>"
end
Templates
get '/' do
erb :index
end
renders views/index.erb.
<form action="/login" method="post">
<label for="uname"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="uname" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<button type="submit">Login</button>
</form>
http://www.kuwata-lab.com/erubis/users-guide.html
Templates
get '/' do
@user = current_user
erb :landing
end
# views/landing.erb.
<% if @user.present? %>
Hola <%= @user.username %>
<% else %>
<a href=“/login”> Ir a ingresar </a>
<% end %>
References
• Ruby https://www.ruby-lang.org/en/
• Sinatra http://sinatrarb.com/
• ERB http://www.kuwata-lab.com/erubis/users-guide.html