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

Rails &| Padrino

Rails &| Padrino

A comparison of Rails and the other Ruby Web Framework: Padrino

Hartog C. de Mik

October 16, 2012
Tweet

More Decks by Hartog C. de Mik

Other Decks in Programming

Transcript

  1. Agenda • Introduction • Padrino Apps and Rails Engines •

    Routing from the inside out • Yielding Sinatra style development • Examples
  2. • My name is • Side-by-side comparison • A little

    more Padrino than Rails Introduction
  3. get %r{/droid/([a-z0-9\-])}, :provides => :text do “these are not the

    droids...” end • Structured Sinatra • Mimics Rails • Very fast Padrino
  4. Padrino Apps • What are they? • Why apps within

    apps? % padrino gen project my_cool_project % cd my_cool_project % padrino gen app oauth2
  5. Rails Engines • Heavy Metal • Rails-engine can be Rack-application

    • Routing the Mount My::Application.routes.draw do mount MyEngine::RackApp => '/magic' end
  6. user_index GET /user(.:format) user#index POST /user(.:format) user#create new_user GET /user/new(.:format)

    user#new edit_user GET /user/:id/edit(.:format) user#edit user GET /user/:id(.:format) user#show PUT /user/:id(.:format) user#update DELETE /user/:id(.:format) user#destroy Predict that route!
  7. resources :dogs do member do get :leash # fetch the

    current leash put :leash # leash the dog end end # what action shall we run? Predict that route!
  8. # what action shall we run? leash_dog GET /dogs/:id/leash(.:format) dogs#leash

    PUT /dogs/:id/leash(.:format) dogs#leash # should have said: resources :dogs do member do get :leash, :action => “fetch_leash” put :leash, :action => “put_leash” end end leash_dog GET /dogs/:id/leash(.:format) dogs#fetch_leash PUT /dogs/:id/leash(.:format) dogs#put_leash Predict that route!
  9. # padrino style controller :dogs do get :leash, :with =>

    :id do # grab hold of the leash end put :leash, :with => [:id, :leash_id] # place the leash on the dog end End # renders Application: Foo URL REQUEST PATH (:dogs, :leash) GET /dogs/leash/:id (:dogs, :leash) PUT /dogs/leash/:id/:leash_id Predict that route!
  10. Yielding the Sinatra style • Using methods and paths together

    is very expressive • Having Padrino around to modularize your source makes it fun • Using modularized source makes it testable
  11. # lib/api/handler.rb – Contained, testable, handler code module API class

    Handler def login(username, password) end # get a listing of dogs for sale def availableDogs end # buy a given dog def buyDog(dog_id) end end end Example: XML-RPC
  12. # lib/dog_server.rb – a contained, testable, server require 'xmlrpc/server' #

    ruby core (4 real!) class DogServer < XMLRPC::BasicServer def initialize(class_delim=".") super self.add_handler("API", API::Handler.new) self.add_introspection end end Example: XML-RPC
  13. # app/controllers/service.rb – the Padrino controller Dog.controllers :service do get

    :index, :provides => :text do “these are not the droids...” end post :index, :provides => :xml do # some XML-RPC clients are very picky about this... headers 'Content-Type' => "text/xml;charset=utf-8" server = Dog::Server.new server.process(request.env["rack.input"].read) end end Example: XML-RPC
  14. # dog/helpers/json.rb – read & scrub json input Dog.helpers do

    def json_input env["rack.input"].rewind JSON.parse(env["rack.input"].read) end def json_attributes(*list) list.collect!(&:to_s) json_input.delete_if { |k,v| !list.include?(k) } end end Example: JSON
  15. # dog/controllers/json.rb – the Padrino controller Dog.controller :jason do provides

    :json before { @handler = API::Handler.new } post :login do @handler.login(json_attributes(:username, :password)) end get :available { @handler.availableDogs } put :buy_dog, :with => :id do @handler.buyDog(params[:id]) end end Example: JSON