Sinatra Up and Running
Tarcisio 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 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.
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’ do
post = Post.create! params
redirect “/posts/#{post.id}”
end
Static Files
Simple way to delivery static files
Directory Tree
|-- public
| `-- public.html
|-- app.rb
public.html
Hello Sinatra!
app.rb
require ‘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’ do
erb :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.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>
Hands On
And now ?
Deploy on
Heroku
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
Questions ?
References
1. Sinatra: Up and Running - O'Reilly Media
2. http://www.sinatrarb.com
3. https://github.com/sinatra/sinatra

More Related Content

PPTX
IronRuby - Fosdem 2010
PDF
Git Internals
PDF
Prestogres, ODBC & JDBC connectivity for Presto
PDF
Initial Experiences Route Filtering at the Edge AS15169 by Arturo L. Servin
PPTX
Network Traffic Search using Apache HBase
PDF
Presto as a Service - Tips for operation and monitoring
PDF
Linked Media Management with Apache Marmotta
PPS
Flickr Php
IronRuby - Fosdem 2010
Git Internals
Prestogres, ODBC & JDBC connectivity for Presto
Initial Experiences Route Filtering at the Edge AS15169 by Arturo L. Servin
Network Traffic Search using Apache HBase
Presto as a Service - Tips for operation and monitoring
Linked Media Management with Apache Marmotta
Flickr Php

Similar to Sinatra Basics (20)

PDF
Can we run the Whole Web on Apache Sling?
PDF
Accelerated Spark on Azure: Seamless and Scalable Hardware Offloads in the C...
PPT
The Semantic Web An Introduction
PDF
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
PDF
Learning the basics of Apache NiFi for iot OSS Europe 2020
PPTX
Archive integration with RDF
PDF
Jump Start with Apache Spark 2.0 on Databricks
PPTX
Analyzing 1.2 Million Network Packets per Second in Real-time
PDF
Deep Dive into Building Streaming Applications with Apache Pulsar
PDF
flickr's architecture & php
PPT
PPTX
HKNOG 7.0: RPKI - it's time to start deploying it
PDF
Fluentd and Embulk Game Server 4
PPTX
The columnar roadmap: Apache Parquet and Apache Arrow
PDF
OSS EU: Deep Dive into Building Streaming Applications with Apache Pulsar
PDF
grpc-Malmo.pdf
PDF
Scala for Everything: From Frontend to Backend Applications - Scala Matsuri 2020
PDF
Securing Internet Routing: RPSL & RPKI
PDF
Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...
PDF
What is Digital Rebar Provision (and how RackN extends)?
Can we run the Whole Web on Apache Sling?
Accelerated Spark on Azure: Seamless and Scalable Hardware Offloads in the C...
The Semantic Web An Introduction
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Learning the basics of Apache NiFi for iot OSS Europe 2020
Archive integration with RDF
Jump Start with Apache Spark 2.0 on Databricks
Analyzing 1.2 Million Network Packets per Second in Real-time
Deep Dive into Building Streaming Applications with Apache Pulsar
flickr's architecture & php
HKNOG 7.0: RPKI - it's time to start deploying it
Fluentd and Embulk Game Server 4
The columnar roadmap: Apache Parquet and Apache Arrow
OSS EU: Deep Dive into Building Streaming Applications with Apache Pulsar
grpc-Malmo.pdf
Scala for Everything: From Frontend to Backend Applications - Scala Matsuri 2020
Securing Internet Routing: RPSL & RPKI
Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...
What is Digital Rebar Provision (and how RackN extends)?
Ad

Recently uploaded (20)

PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
Architecture types and enterprise applications.pdf
PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PDF
UiPath Agentic Automation session 1: RPA to Agents
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PDF
Improvisation in detection of pomegranate leaf disease using transfer learni...
DOCX
search engine optimization ppt fir known well about this
PDF
STKI Israel Market Study 2025 version august
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
Five Habits of High-Impact Board Members
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PPT
Geologic Time for studying geology for geologist
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
NewMind AI Weekly Chronicles – August ’25 Week III
Module 1.ppt Iot fundamentals and Architecture
Architecture types and enterprise applications.pdf
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
A review of recent deep learning applications in wood surface defect identifi...
Convolutional neural network based encoder-decoder for efficient real-time ob...
UiPath Agentic Automation session 1: RPA to Agents
Final SEM Unit 1 for mit wpu at pune .pptx
The influence of sentiment analysis in enhancing early warning system model f...
Consumable AI The What, Why & How for Small Teams.pdf
Improvisation in detection of pomegranate leaf disease using transfer learni...
search engine optimization ppt fir known well about this
STKI Israel Market Study 2025 version august
Benefits of Physical activity for teenagers.pptx
Five Habits of High-Impact Board Members
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Geologic Time for studying geology for geologist
A proposed approach for plagiarism detection in Myanmar Unicode text
Ad

Sinatra Basics

  • 1. Sinatra Up and Running Tarcisio Coutinho 04-10-2013
  • 2. About Me ● Bachelor in Computer Science at CIn - UFPE ● Software Developer ● 空手の学生 ● git & Github lover @tacsio
  • 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
  • 8. Sinatra’s Characteristics ● Good option for design APIs ● Simple ecosystem ○ DSL ○ HTTP Verbs ○ Rack Middleware ○ Sinatra Base
  • 9. Sinatra’s Characteristics ● Simple ecosystem ○ DSL ■ Domain Specific Language ○ HTTP Verbs ○ Rack Middleware ○ Sinatra Base
  • 10. Sinatra’s Characteristics ● Simple ecosystem ○ DSL ○ HTTP Verbs ■ Get Post Put Delete Options ○ Rack Middleware ○ Sinatra Base
  • 11. Sinatra’s Characteristics ● Simple ecosystem ○ DSL ○ HTTP Verbs ○ Rack Middleware ■ Provides an minimal interface between webservers supporting Ruby and Ruby frameworks. ○ Sinatra Base
  • 12. Sinatra’s Characteristics ● Simple ecosystem ○ DSL ○ HTTP Verbs ○ Rack Middleware ○ Sinatra Base ■ Sinatra application
  • 13. 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
  • 14. Who is Using It? ● Github ● Heroku ● BBC ● thoughtbot ● Songbird ● Engine Yard
  • 17. Hello Sinatra require ‘sinatra’ get ‘/’ do “Hello, Sinatra!” end
  • 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
  • 32. References 1. Sinatra: Up and Running - O'Reilly Media 2. http://www.sinatrarb.com 3. https://github.com/sinatra/sinatra