Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lightweight Webservices with Sinatra and RestClient

49,047 views

Published on

Talk presented by Adam Wiggins & Blake Mizerany at Rubyconf 2008

Published in: Technology

Lightweight Webservices with Sinatra and RestClient

  1. Lightweight web services with Sinatra and RestClient Adam Wiggins & Blake Mizerany Rubyconf 2008
  2. What for?
  3. app lifecycle
  4. Birth: small and beautiful
  5. Fate: sprawling mess
  6. web services
  7. Rails?
  8. Rails? Too much.
  9. Bare mongrel handler or Rack app?
  10. Bare mongrel handler or Rack app? Too little.
  11. Sinatra - the classy microframework for Ruby
  12. require 'rubygems' require 'sinatra' get '/' do quot;Hello, whirledquot; end
  13. $ ruby hello.rb
  14. require 'rubygems' require 'sinatra' require 'lib/posts' post '/posts' post = Post.create! params redirect quot;/posts/#{post.id}quot; do get '/posts/:id' do @post = Post.find(params[:id]) erb :post end
  15. ‣Templating (erb, haml, builder) ‣Tests/specs (test/spec, rspec, test::unit) ‣Before filters ‣Helpers ‣Error handlers ‣Inline templates ‣Code reloading ‣HTTP caching (etag, last-modified) ‣Rack / inline middleware
  16. A commitment to small
  17. A commitment to small Rails 87,990 Merb-core 12,417 Ramaze 11,796 Camping 1,704 Sinatra 1,576
  18. Sinatra is a proud Ruby citizen
  19. examples
  20. git-wiki http://github.com/sr/git-wiki 355 lines of Ruby
  21. get '/:page' do @page = Page.find(params[:page]) haml :show end __END__ @@ layout %html %body %content=yield @@ show %h1= title #page_content= @page.to_html
  22. rifgraf http://github.com/adamwiggins/rifgraf 61 lines of Ruby
  23. github-services http://github.com/pjhyett/github-services 423 lines of Ruby
  24. scanty http://github.com/adamwiggins/scanty 194 lines of Ruby
  25. client side
  26. ActiveResource?
  27. ActiveResource? Too much.
  28. Net::HTTP?
  29. Net::HTTP? Too little.
  30. RestClient - the Sinatra-inspired microclient for Ruby
  31. require 'rubygems' require 'rest_client' RestClient.get 'http://localhost:4567/posts'
  32. require 'rubygems' require 'rest_client' RestClient.post 'http://localhost:4567/posts', :author => 'Me', :title => 'First Post'
  33. Console $ restclient http://example.com >> post '/resource', :value => 42 => quot;resultquot;
  34. A better curl $ restclient get http://example.com/posts > posts.xml $ restclient post http://example.com/posts < post.xml $ restclient put http://example.com/posts/1 < post.xml $ restclient delete http://example.com/posts/1
  35. Logging & replay $ RESTCLIENT_LOG=stdout ruby myscript.rb RestClient.get quot;http://example.com/postsquot; # => 200 OK | text/html 3781 bytes RestClient.post quot;http://example.com/postsquot;, quot;title=First+postquot; # => 200 OK | text/html 40 bytes
  36. examples
  37. heroku-client http://github.com/adamwiggins/heroku-client
  38. couchrest http://github.com/jchris/couchrest
  39. principles
  40. “lagom”
  41. “lagom” just the right amount
  42. Fewer classes, less inheritance
  43. Controller object mapping & routes vs. URLs
  44. Controller object s ! RL mapping & routes U e r th fea vs. n ’t o URLs D
  45. Exposed simplicity instead of hidden complexity
  46. Small things, loosely joined, written fast - Justin Gehtland @ Relevance
  47. http://github.com/bmizerany/sinatra http://rest-client.heroku.com/ http://adam.blog.heroku.com/ Adam Wiggins & Blake Mizerany Rubyconf 2008

×