Lightweight web services
with Sinatra and RestClient



Adam Wiggins &
Blake Mizerany
Rubyconf 2008
What for?
app lifecycle
Birth: small and beautiful
Fate: sprawling mess
web services
Rails?
Rails?


Too much.
Bare mongrel handler or
Rack app?
Bare mongrel handler or
Rack app?

Too little.
Sinatra - the classy
microframework for
Ruby
require 'rubygems'
require 'sinatra'

get '/' do
  quot;Hello, whirledquot;
end
$ ruby hello.rb
require 'rubygems'
require 'sinatra'
require 'lib/posts'

post '/posts'
 post = Post.create! params
 redirect quot;/posts/...
‣Templating (erb, haml, builder)
‣Tests/specs (test/spec, rspec, test::unit)
‣Before filters
‣Helpers
‣Error handlers
‣Inl...
A commitment to small
A commitment to small
Rails       87,990
Merb-core   12,417
Ramaze      11,796
Camping      1,704
Sinatra      1,576
Sinatra is a proud
Ruby citizen
examples
git-wiki
http://github.com/sr/git-wiki

355 lines
of Ruby
get '/:page' do
  @page = Page.find(params[:page])
  haml :show
end

__END__
@@ layout
%html
  %body
    %content=yield

@...
rifgraf
http://github.com/adamwiggins/rifgraf

61 lines
of Ruby
github-services
http://github.com/pjhyett/github-services

423 lines
of Ruby
scanty
http://github.com/adamwiggins/scanty

194 lines
of Ruby
client side
ActiveResource?
ActiveResource?


Too much.
Net::HTTP?
Net::HTTP?


Too little.
RestClient -
the Sinatra-inspired
microclient for Ruby
require 'rubygems'
require 'rest_client'

RestClient.get 'http://localhost:4567/posts'
require 'rubygems'
require 'rest_client'

RestClient.post 'http://localhost:4567/posts',
    :author => 'Me', :title => 'F...
Console
$ restclient http://example.com
>> post '/resource', :value => 42
=> quot;resultquot;
A better curl
$ restclient get http://example.com/posts > posts.xml

$ restclient post http://example.com/posts < post.xml...
Logging & replay
$ RESTCLIENT_LOG=stdout ruby myscript.rb

RestClient.get quot;http://example.com/postsquot;
# => 200 OK |...
examples
heroku-client
http://github.com/adamwiggins/heroku-client
couchrest
http://github.com/jchris/couchrest
principles
“lagom”
“lagom”
just the right amount
Fewer classes,
less inheritance
Controller object
mapping & routes
        vs.
URLs
Controller object       s !
                    RL
mapping & routes  U e
             r   th
         fea
           vs.
 ...
Exposed simplicity
instead of
hidden complexity
Small things,
loosely joined,
written fast


- Justin Gehtland @ Relevance
http://github.com/bmizerany/sinatra
http://rest-client.heroku.com/

http://adam.blog.heroku.com/


   Adam Wiggins &
   Bl...
Upcoming SlideShare
Loading in …5
×

Lightweight Webservices with Sinatra and RestClient

45,754 views

Published on

Talk presented by Adam Wiggins & Blake Mizerany at Rubyconf 2008

Published in: Technology
3 Comments
64 Likes
Statistics
Notes
No Downloads
Views
Total views
45,754
On SlideShare
0
From Embeds
0
Number of Embeds
2,179
Actions
Shares
0
Downloads
496
Comments
3
Likes
64
Embeds 0
No embeds

No notes for slide

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

×