Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Sinatra Basic

Sinatra Basic

Basic concepts and constructions of Sinatra micro-framework

Tarcisio Coutinho

October 04, 2013
Tweet

More Decks by Tarcisio Coutinho

Other Decks in Programming

Transcript

  1. About Me • Bachelor in Computer Science at CIn -

    UFPE • Software Developer • 空手の学生 • git & Github lover @tacsio
  2. What is Sinatra? Isn’t Francis Albert "Frank" Sinatra Even if

    He has been a great singer… Now I’m listening him
  3. What is Sinatra? Sinatra is a domain-specific language for building

    websites, web services, and web applications in Ruby.
  4. What is Sinatra? It emphasizes a minimalistic approach to development,

    offering only what is essential to handle HTTP requests and deliver responses to clients.
  5. 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
  6. Sinatra’s Characteristics • Good option for design APIs • Simple

    ecosystem ◦ DSL ◦ HTTP Verbs ◦ Rack Middleware ◦ Sinatra Base
  7. Sinatra’s Characteristics • Simple ecosystem ◦ DSL ▪ Domain Specific

    Language ◦ HTTP Verbs ◦ Rack Middleware ◦ Sinatra Base
  8. Sinatra’s Characteristics • Simple ecosystem ◦ DSL ◦ HTTP Verbs

    ▪ Get Post Put Delete Options ◦ Rack Middleware ◦ Sinatra Base
  9. Sinatra’s Characteristics • Simple ecosystem ◦ DSL ◦ HTTP Verbs

    ◦ Rack Middleware ▪ Provides an minimal interface between webservers supporting Ruby and Ruby frameworks. ◦ Sinatra Base
  10. Sinatra’s Characteristics • Simple ecosystem ◦ DSL ◦ HTTP Verbs

    ◦ Rack Middleware ◦ Sinatra Base ▪ Sinatra application
  11. 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
  12. Who is Using It? • Github • Heroku • BBC

    • thoughtbot • Songbird • Engine Yard
  13. 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.
  14. 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.
  15. Not Found (404) & Server Error (500) not_found do “Route

    not found!” end error do “Server error!” end
  16. 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
  17. Static Files Simple way to delivery static files Directory Tree

    |-- public | `-- public.html |-- app.rb public.html Hello Sinatra! app.rb require ‘sinatra’
  18. 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
  19. 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
  20. 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>
  21. 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>
  22. 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
  23. References 1. Sinatra: Up and Running - O'Reilly Media 2.

    http://www.sinatrarb.com 3. https://github.com/sinatra/sinatra