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

5067b1f9cd2a443210fd96af0149a86a?s=128

Hartog C. de Mik

October 16, 2012
Tweet

Transcript

  1. Hartog de Mik | OrganisedMinds Rails &| Padrino

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

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

    more Padrino than Rails Introduction
  4. Padrino The Elegant Ruby Web Framework

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

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

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

    • Routing the Mount My::Application.routes.draw do mount MyEngine::RackApp => '/magic' end
  8. Routing, from the inside out • No routes file •

    More work; less hassle
  9. resources :user # what will the 'index' path be called?

    Predict that route!
  10. 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!
  11. 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!
  12. # 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!
  13. # 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!
  14. 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
  15. # 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
  16. # 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
  17. # 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
  18. Example: JSON

  19. # 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
  20. # 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
  21. @coffeeaddict_nl github.com/coffeeaddict/ Lead developer @ has_questions?

  22. Links • http://padrinorb.com/ • http://sinatrarb.com/ • http://rubyonrails.org/ • http://ruby-doc.org/core-1.9.3/ •

    http://bit.ly/R7fvkx - this presentation