4. What is Sinatra?
Isn’t Francis Albert "Frank" Sinatra
Even if He has been a great singer… Now I’m listening him
5. What is Sinatra?
Sinatra is a domain-specific language for
building websites, web services, and web
applications in Ruby.
6. What is Sinatra?
It emphasizes a minimalistic approach to
development, offering only what is essential
to handle HTTP requests and deliver
responses to clients.
7. Sinatra’s Characteristics
● Sinatra is not a framework
○ Doesn’t have an ORM
○ Predefined configuration files
○ Neither defined structures of directories
● Flexible by nature
Do What You Want, Cause A Pirate Is Free...
You Are A Pirate
18. Routing
The First Sufficient Match Wins When Sinatra
parses routes, the first sufficient match is the
one that will be executed.
This is true even when there is a better or
more specific route definition later in the file.
19. HTTP Verbs and REST
GET - Is used to ask a server to return a
representation of a resource
POST - Is used to submit data to a web server.
PUT - Is used to create or update a
representation of a resource on a server.
DELETE - Is used to destroy a resource on a
server.
20. Not Found (404) & Server Error (500)
not_found do
“Route not found!”
end
error do
“Server error!”
end
21. Route Params & Redirects
get ‘/posts/:id’ do
@post = Post.find(params[:id])
erb :post
end
post ‘/posts’ do
post = Post.create! params
redirect “/posts/#{post.id}”
end
22. Static Files
Simple way to delivery static files
Directory Tree
|-- public
| `-- public.html
|-- app.rb
public.html
Hello Sinatra!
app.rb
require ‘sinatra’
23. Static Files :: If we got this scenario?
Directory Tree
|-- public
| `-- public.html
|-- static_file.rb
require ‘sinatra’
get ‘/public.html’ do
“This is delivery via the route.”
end
24. Views & Templates
Views in Sinatra are HTML templates that can
optionally contain data passed from the
application.
Available engines
● Erb
● Haml
● Erubis
● ...
Template mode
● Inline Templates
● External Templates
25. Inline Templates
Are defined in the application code file itself
require ‘sinatra’
get ‘/index’ do
erb :index_inner
end
__END__
@@index_inner
__END__
@@index_inner
<!DOCTYPE html>
<html>
<body>
<h1>Hello Sinatra</h1>
</body>
</html>
26. External Templates
Sinatra will look for them by default in the
views subfolder.
app.erb
require ‘sinatra’
get ‘/index’ do
@msg = “Hello Sinatra”
erb :index
end
Views directory :: index.erb
<!DOCTYPE html>
<html>
<body>
<h1><%= @msg %></h1>
</body>
</html>
30. We must configure the database
properties
Development environment
configure :development do
set :database, ‘sqlite3:///db/database.sqlite’
end
Production environment
configure :development do
ActiveRecord::Base.establish_connection(ENV
['DATABASE_URL'] || 'postgres://localhost/mydb')
end