Slide 1

Slide 1 text

Hartog de Mik | OrganisedMinds Rails &| Padrino

Slide 2

Slide 2 text

Agenda • Introduction • Padrino Apps and Rails Engines • Routing from the inside out • Yielding Sinatra style development • Examples

Slide 3

Slide 3 text

• My name is • Side-by-side comparison • A little more Padrino than Rails Introduction

Slide 4

Slide 4 text

Padrino The Elegant Ruby Web Framework

Slide 5

Slide 5 text

get %r{/droid/([a-z0-9\-])}, :provides => :text do “these are not the droids...” end • Structured Sinatra • Mimics Rails • Very fast Padrino

Slide 6

Slide 6 text

Padrino Apps • What are they? • Why apps within apps? % padrino gen project my_cool_project % cd my_cool_project % padrino gen app oauth2

Slide 7

Slide 7 text

Rails Engines • Heavy Metal • Rails-engine can be Rack-application • Routing the Mount My::Application.routes.draw do mount MyEngine::RackApp => '/magic' end

Slide 8

Slide 8 text

Routing, from the inside out • No routes file • More work; less hassle

Slide 9

Slide 9 text

resources :user # what will the 'index' path be called? Predict that route!

Slide 10

Slide 10 text

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!

Slide 11

Slide 11 text

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!

Slide 12

Slide 12 text

# 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!

Slide 13

Slide 13 text

# 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!

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

# 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

Slide 16

Slide 16 text

# 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

Slide 17

Slide 17 text

# 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

Slide 18

Slide 18 text

Example: JSON

Slide 19

Slide 19 text

# 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

Slide 20

Slide 20 text

# 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

Slide 21

Slide 21 text

@coffeeaddict_nl github.com/coffeeaddict/ Lead developer @ has_questions?

Slide 22

Slide 22 text

Links • http://padrinorb.com/ • http://sinatrarb.com/ • http://rubyonrails.org/ • http://ruby-doc.org/core-1.9.3/ • http://bit.ly/R7fvkx - this presentation