code_junkie

Is there a more user friendly alternative to Net::HTTP for interacting with REST APIs?

Nov 11th, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. require 'rubygems'
  2. require 'httparty'
  3.  
  4. class Representative
  5. include HTTParty
  6. format :xml
  7.  
  8. def self.find_by_zip(zip)
  9. get('http://whoismyrepresentative.com/whoismyrep.php', :query => {:zip => zip})
  10. end
  11. end
  12.  
  13. puts Representative.find_by_zip(46544).inspect
  14. # {"result"=>{"n"=>"1", "rep"=>{"name"=>"Joe Donnelly", "district"=>"2", "office"=>"1218 Longworth", "phone"=>"(202) 225-3915", "link"=>"http://donnelly.house.gov/", "state"=>"IN"}}}
  15.  
  16. gem install rest-open-uri
  17.  
  18. response = open('https://wherever/foo',
  19. :method => :put,
  20. :http_basic_authentication => ['my-user', 'my-passwd'],
  21. :body => 'payload')
  22.  
  23. puts response.read
Add Comment
Please, Sign In to add comment