SlideShare a Scribd company logo
1 of 32
Download to read offline
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

Similar to Sinatra Basics

Can we run the Whole Web on Apache Sling?
Can we run the Whole Web on Apache Sling?Can we run the Whole Web on Apache Sling?
Can we run the Whole Web on Apache Sling?Bertrand Delacretaz
 
Accelerated Spark on Azure: Seamless and Scalable Hardware Offloads in the C...
 Accelerated Spark on Azure: Seamless and Scalable Hardware Offloads in the C... Accelerated Spark on Azure: Seamless and Scalable Hardware Offloads in the C...
Accelerated Spark on Azure: Seamless and Scalable Hardware Offloads in the C...Databricks
 
The Semantic Web An Introduction
The Semantic Web An IntroductionThe Semantic Web An Introduction
The Semantic Web An Introductionshaouy
 
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...Alberto Perdomo
 
Learning the basics of Apache NiFi for iot OSS Europe 2020
Learning the basics of Apache NiFi for iot OSS Europe 2020Learning the basics of Apache NiFi for iot OSS Europe 2020
Learning the basics of Apache NiFi for iot OSS Europe 2020Timothy Spann
 
Jump Start with Apache Spark 2.0 on Databricks
Jump Start with Apache Spark 2.0 on DatabricksJump Start with Apache Spark 2.0 on Databricks
Jump Start with Apache Spark 2.0 on DatabricksAnyscale
 
Analyzing 1.2 Million Network Packets per Second in Real-time
Analyzing 1.2 Million Network Packets per Second in Real-timeAnalyzing 1.2 Million Network Packets per Second in Real-time
Analyzing 1.2 Million Network Packets per Second in Real-timeDataWorks Summit
 
Deep Dive into Building Streaming Applications with Apache Pulsar
Deep Dive into Building Streaming Applications with Apache Pulsar Deep Dive into Building Streaming Applications with Apache Pulsar
Deep Dive into Building Streaming Applications with Apache Pulsar Timothy Spann
 
flickr's architecture & php
flickr's architecture & php flickr's architecture & php
flickr's architecture & php coolpics
 
HKNOG 7.0: RPKI - it's time to start deploying it
HKNOG 7.0: RPKI - it's time to start deploying itHKNOG 7.0: RPKI - it's time to start deploying it
HKNOG 7.0: RPKI - it's time to start deploying itAPNIC
 
Fluentd and Embulk Game Server 4
Fluentd and Embulk Game Server 4Fluentd and Embulk Game Server 4
Fluentd and Embulk Game Server 4N Masahiro
 
The columnar roadmap: Apache Parquet and Apache Arrow
The columnar roadmap: Apache Parquet and Apache ArrowThe columnar roadmap: Apache Parquet and Apache Arrow
The columnar roadmap: Apache Parquet and Apache ArrowDataWorks Summit
 
OSS EU: Deep Dive into Building Streaming Applications with Apache Pulsar
OSS EU:  Deep Dive into Building Streaming Applications with Apache PulsarOSS EU:  Deep Dive into Building Streaming Applications with Apache Pulsar
OSS EU: Deep Dive into Building Streaming Applications with Apache PulsarTimothy Spann
 
Scala for Everything: From Frontend to Backend Applications - Scala Matsuri 2020
Scala for Everything: From Frontend to Backend Applications - Scala Matsuri 2020Scala for Everything: From Frontend to Backend Applications - Scala Matsuri 2020
Scala for Everything: From Frontend to Backend Applications - Scala Matsuri 2020Taro L. Saito
 
Securing Internet Routing: RPSL & RPKI
Securing Internet Routing: RPSL & RPKISecuring Internet Routing: RPSL & RPKI
Securing Internet Routing: RPSL & RPKIAPNIC
 
Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...
Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...
Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...CloudxLab
 
What is Digital Rebar Provision (and how RackN extends)?
What is Digital Rebar Provision (and how RackN extends)?What is Digital Rebar Provision (and how RackN extends)?
What is Digital Rebar Provision (and how RackN extends)?rhirschfeld
 

Similar to Sinatra Basics (20)

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

Recently uploaded

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 

Recently uploaded (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 

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