Sinatra Basics
-
Upload
tarcisio-coutinho -
Category
Technology
-
view
470 -
download
1
Transcript of Sinatra Basics
Sinatra Up and RunningTarcisio Coutinho
04-10-2013
About Me
● Bachelor in Computer Science at CIn - UFPE● Software Developer● 空手の学生 ● git & Github lover
@tacsio
What is Sinatra?
What is Sinatra?
Isn’t Francis Albert "Frank" Sinatra
Even if He has been a great singer… Now I’m listening him
What is Sinatra?
Sinatra is a domain-specific language for building websites, web services, and web applications in Ruby.
What is Sinatra?
It emphasizes a minimalistic approach to development, offering only what is essential to handle HTTP requests and deliver responses to clients.
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
Sinatra’s Characteristics
● Good option for design APIs
● Simple ecosystem○ DSL○ HTTP Verbs○ Rack Middleware○ Sinatra Base
Sinatra’s Characteristics
● Simple ecosystem○ DSL
■ Domain Specific Language○ HTTP Verbs○ Rack Middleware○ Sinatra Base
Sinatra’s Characteristics
● Simple ecosystem○ DSL○ HTTP Verbs
■ Get Post Put Delete Options○ Rack Middleware○ Sinatra Base
Sinatra’s Characteristics
● Simple ecosystem○ DSL○ HTTP Verbs○ Rack Middleware
■ Provides an minimal interface between
webservers supporting Ruby and Ruby frameworks.
○ Sinatra Base
Sinatra’s Characteristics
● Simple ecosystem○ DSL○ HTTP Verbs○ Rack Middleware○ Sinatra Base
■ Sinatra application
Sinatra’s Characteristics
● Templating (erb, haml)● Tests/specs (test:unit, rspec)● Filters (Before/After)● Helpers● Error Handlers● Inline templates● Code reloading (shotgun gem)● HTTP Caching (Etag)● Streams● Rack Middleware
Who is Using It?
● Github● Heroku● BBC● thoughtbot● Songbird● Engine Yard
Let’s start
Sinatra Installation
gem install sinatra
Hello Sinatra
require ‘sinatra’
get ‘/’ do“Hello, Sinatra!”
end
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.
HTTP Verbs and REST
GET - Is used to ask a server to return a representation of a resourcePOST - 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.
Not Found (404) & Server Error (500)
not_found do“Route not found!”
end
error do“Server error!”
end
Route Params & Redirects
get ‘/posts/:id’ do@post = Post.find(params[:id])erb :post
end
post ‘/posts’ dopost = Post.create! paramsredirect “/posts/#{post.id}”
end
Static Files
Simple way to delivery static files
Directory Tree|-- public| `-- public.html|-- app.rb
public.htmlHello Sinatra!
app.rbrequire ‘sinatra’
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
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
Inline Templates
Are defined in the application code file itself
require ‘sinatra’
get ‘/index’ doerb :index_inner
end
__END__@@index_inner
__END__@@index_inner
<!DOCTYPE html><html>
<body><h1>Hello Sinatra</h1></body>
</html>
External Templates
Sinatra will look for them by default in the views subfolder.
app.erbrequire ‘sinatra’
get ‘/index’ do@msg = “Hello Sinatra”erb :index
end
Views directory :: index.erb<!DOCTYPE html><html>
<body><h1><%= @msg %></h1></body>
</html>
Hands On
And now ?
Deploy on Heroku
We must configure the database properties
Development environmentconfigure :development do
set :database, ‘sqlite3:///db/database.sqlite’end
Production environmentconfigure :development do
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')end
Questions ?
References
1. Sinatra: Up and Running - O'Reilly Media2. http://www.sinatrarb.com3. https://github.com/sinatra/sinatra