• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Sinatra for REST services
 

Sinatra for REST services

on

  • 5,921 views

Introduction to Sinatra miniframework. This talk was given at the @Webnetconf in Milan 2012.

Introduction to Sinatra miniframework. This talk was given at the @Webnetconf in Milan 2012.

Statistics

Views

Total Views
5,921
Views on SlideShare
5,904
Embed Views
17

Actions

Likes
9
Downloads
44
Comments
0

6 Embeds 17

https://twitter.com 11
http://www.linkedin.com 2
http://twitter.com 1
http://www.hanrss.com 1
https://si0.twimg.com 1
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Sinatra for REST services Sinatra for REST services Presentation Transcript

  • REST with Sinatra Emanuele DelBono @emadb
  • Thanks to the sponsors
  • About meI’m a software developer inCodicePlastico.I write Web applications in C# withASP.NET MVC. I’m a member of<WEBdeBS/> a local webcommunity....and I’m a wannabe Rubydeveloper ;-)
  • Agenda‣ Philosophy. Sweet and easy.‣ REST. Yet another boring introduction.‣ Hello world. Show some magic.‣ Features. Amazing!‣ Inside. How it works?‣ Q&A. ...and thanks.
  • Sinatra as a REST server
  • What is isn’t‣ Sinatra is not -another MV* framework -a full framework like Rails -an ORM -a game project
  • Sinatra is just a DSL written in ruby
  • Hello world
  • Hello world~$ gem install sinatra~$ vi app.rb
  • Hello world~$ gem install sinatra~$ vi app.rbrequire sinatraget / do hello world!end
  • Hello world~$ gem install sinatra~$ vi app.rbrequire sinatraget / do hello world!end~$ ruby app.rb
  • Is it really so easy?‣ Yep! (special thanks to Ruby sweeties) - get is a just a method (more later) - the route is the parameter - the block is what should be executed - the block returns the result - the server is built in (based on Rack)
  • Let’s go RESTWhat? Yet another REST introduction?
  • 5 REST principles‣ Uniform Interface ‣ HATEAOS‣ Stateless‣ Cacheable‣ Client-Server‣ Layered System
  • Sinatra and REST‣ Sinatra supports - All HTTP verbs - Caching - Content types - Routes - ....and all you need to build a rest server
  • Give a look at the
  • Verbs‣ Support for all verbs - Get, Post, Put, Delete, Head, Options, Patch‣ They are just methods
  • Routing‣ No need of route files or route maps‣ The verb method takes the route as parameter
  • Routingget ( ‘/todos’ ) {...}get ( ‘/todo/:id’ ) {...}get ( ‘/*’ ) {...}
  • Routing Conditionsget /foo, :agent => /Mozilla/(d.d)sw?/ do "Youre using Mozilla version#{params[:agent][0]}"endget /foo do # Matches non-Mozilla browsersend
  • Routing Customset(:prob) { |v| condition { rand <= v } }get /win_a_car, :prob => 0.1 do "You won!"endget /win_a_car {"Sorry, you lost."}
  • Xml or Json‣ You can set the content_type to whatever you needcontent_type :jsoncontent_type :xml
  • Status codes‣ Set the HTTP status code is status 200 status 403
  • Http Headers‣ You can add your own headersheaders "X-My-Value" => "this is myheader"
  • Redirectredirect to ‘/todos’
  • Cache‣ You can set your cache information using the headers methodheaders "Cache-Control" => "public,must-revalidate, max-age=3600","Expires" => Time.at(Time.now.to_i + (60
  • Cache‣ Or better, you can use the expiresexpires 3600, :public
  • Filtersbefore do #...endafter do #...
  • Authentication‣ Basic authentication support through Rack‣ Key-based authentication
  • Basic Authenticationuse Rack::Auth::Basic, "Private area" do|usr, pwd| [usr, pwd] == [admin, admin]end
  • Token authenticationbefore do if env[‘HTTP_MY_KEY’] ==’rubyrocks‘ error 401 endend
  • Demo
  • Stream‣ Supports streamed content‣ Streamed responses have no Content- Length header‣ But have Transfer-Encoding: chunked
  • Inside
  • Inside‣ Sinatra source code is on github: http://github.com/sinatra/sinatra‣ Less than 2k lines of code‣ Based on Rack
  • Application styleClassic application vsModular application
  • Classic App Single file Standalone
  • Modular Apps Multi fileSubclassing Sinatra::Base Distributed as library
  • The codeWhat happen when a request arrives?get (‘/hello’) do { ‘hello world’ }
  • Where do get come from?require "sinatra"outer_self = selfget / do "outer self: #{outer_self}, inner self: #{self}"end outer self: main, inner self: #<Sinatra::Application: closures in ruby are “scope gate”
  • $ irb
  • $ irbruby-1.9.2-p180 > require sinatra
  • $ irbruby-1.9.2-p180 > require sinatra=> true
  • $ irbruby-1.9.2-p180 > require sinatra=> trueruby-1.9.2-p180 > method(:get)
  • $ irbruby-1.9.2-p180 > require sinatra=> trueruby-1.9.2-p180 > method(:get)=> #<Method: Object(Sinatra::Delegator)#get>
  • $ irbruby-1.9.2-p180 > require sinatra=> trueruby-1.9.2-p180 > method(:get)=> #<Method: Object(Sinatra::Delegator)#get>ruby-1.9.2-p180 > Sinatra::Delegator.methods(false)
  • $ irbruby-1.9.2-p180 > require sinatra=> trueruby-1.9.2-p180 > method(:get)=> #<Method: Object(Sinatra::Delegator)#get>ruby-1.9.2-p180 > Sinatra::Delegator.methods(false)=> [:delegate, :target, :target=]
  • $ irbruby-1.9.2-p180 > require sinatra=> trueruby-1.9.2-p180 > method(:get)=> #<Method: Object(Sinatra::Delegator)#get>ruby-1.9.2-p180 > Sinatra::Delegator.methods(false)=> [:delegate, :target, :target=]ruby-1.9.2-p180 > Sinatra::Delegator.target
  • $ irbruby-1.9.2-p180 > require sinatra=> trueruby-1.9.2-p180 > method(:get)=> #<Method: Object(Sinatra::Delegator)#get>ruby-1.9.2-p180 > Sinatra::Delegator.methods(false)=> [:delegate, :target, :target=]ruby-1.9.2-p180 > Sinatra::Delegator.target=> Sinatra::Application
  • $ irbruby-1.9.2-p180 > require sinatra=> trueruby-1.9.2-p180 > method(:get)=> #<Method: Object(Sinatra::Delegator)#get>ruby-1.9.2-p180 > Sinatra::Delegator.methods(false)=> [:delegate, :target, :target=]ruby-1.9.2-p180 > Sinatra::Delegator.target=> Sinatra::Applicationruby-1.9.2-p180 > Sinatra::Application.method(:get)
  • $ irbruby-1.9.2-p180 > require sinatra=> trueruby-1.9.2-p180 > method(:get)=> #<Method: Object(Sinatra::Delegator)#get>ruby-1.9.2-p180 > Sinatra::Delegator.methods(false)=> [:delegate, :target, :target=]ruby-1.9.2-p180 > Sinatra::Delegator.target=> Sinatra::Applicationruby-1.9.2-p180 > Sinatra::Application.method(:get)=> #<Method:Sinatra::Application(Sinatra::Base).get>
  • Where do get come from?‣ get is defined twice: – Once in Sinatra::Delegator a mixin extending Object – Once in Sinatra::Application‣ The Delegator implementation simply delegate to Application‣ That’s why we have get/post/... methods on main
  • How the block is invoked?‣ The route! method finds the correct routedef route_eval throw :halt, yieldend‣ throw is used to go back to the invoker
  • In base.rb‣ The invokerdef invoke res = catch(:halt) { yield } # .... other code...end
  • It’s a kind of magic!
  • Sinatra has ended his set (crowd applauds)
  • Questions?
  • demos at:http://github.com/emadb/ webnetconf_demo
  • Please rate this sessionScan the code, go online, rate this session