Sinatra Basics

732 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
732
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
8
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Sinatra Basics

  1. 1. Sinatra Up and Running Tarcisio Coutinho 04-10-2013
  2. 2. About Me ● Bachelor in Computer Science at CIn - UFPE ● Software Developer ● 空手の学生 ● git & Github lover @tacsio
  3. 3. What is Sinatra?
  4. 4. What is Sinatra? Isn’t Francis Albert "Frank" Sinatra Even if He has been a great singer… Now I’m listening him
  5. 5. What is Sinatra? Sinatra is a domain-specific language for building websites, web services, and web applications in Ruby.
  6. 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. 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. 8. Sinatra’s Characteristics ● Good option for design APIs ● Simple ecosystem ○ DSL ○ HTTP Verbs ○ Rack Middleware ○ Sinatra Base
  9. 9. Sinatra’s Characteristics ● Simple ecosystem ○ DSL ■ Domain Specific Language ○ HTTP Verbs ○ Rack Middleware ○ Sinatra Base
  10. 10. Sinatra’s Characteristics ● Simple ecosystem ○ DSL ○ HTTP Verbs ■ Get Post Put Delete Options ○ Rack Middleware ○ Sinatra Base
  11. 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. 12. Sinatra’s Characteristics ● Simple ecosystem ○ DSL ○ HTTP Verbs ○ Rack Middleware ○ Sinatra Base ■ Sinatra application
  13. 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. 14. Who is Using It? ● Github ● Heroku ● BBC ● thoughtbot ● Songbird ● Engine Yard
  15. 15. Let’s start
  16. 16. Sinatra Installation gem install sinatra
  17. 17. Hello Sinatra require ‘sinatra’ get ‘/’ do “Hello, Sinatra!” end
  18. 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. 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. 20. Not Found (404) & Server Error (500) not_found do “Route not found!” end error do “Server error!” end
  21. 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. 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. 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. 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. 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. 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>
  27. 27. Hands On
  28. 28. And now ?
  29. 29. Deploy on Heroku
  30. 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
  31. 31. Questions ?
  32. 32. References 1. Sinatra: Up and Running - O'Reilly Media 2. http://www.sinatrarb.com 3. https://github.com/sinatra/sinatra

×