Net::HTTP can be rather cumbersome for the standard use case!
Under REST architecture, the client and server can only interact in one way: The client sends a request to the server, then the server sends a response back to the client. Servers cannot make requests and clients cannot respond — all interactions are initiated by the client.
REST Client is a method or a tool to invoke a REST service API that is exposed for communication by any system or service provider. For example: if an API is exposed to get real time traffic information about a route from Google, the software/tool that invokes the Google traffic API is called the REST client.
REST is a client-server architecture, where the server and the client act independently of one another as long as the interface stays the same when processing a request and a response. The server exposes the REST API, and the client makes use of it.
If you only have to deal with REST, the rest-client library is fantastic.
If the APIs you're using aren't completely RESTful - or even if they are - HTTParty is really worth checking out. It simplifies using REST APIs, as well as non-RESTful web APIs. Check out this code (copied from the above link):
require 'rubygems'
require 'httparty'
class Representative
include HTTParty
format :xml
def self.find_by_zip(zip)
get('http://whoismyrepresentative.com/whoismyrep.php', :query => {:zip => zip})
end
end
puts Representative.find_by_zip(46544).inspect
# {"result"=>{"n"=>"1", "rep"=>{"name"=>"Joe Donnelly", "district"=>"2", "office"=>"1218 Longworth", "phone"=>"(202) 225-3915", "link"=>"http://donnelly.house.gov/", "state"=>"IN"}}}
rest-open-uri is the one that is used heavily throughout the RESTful Web Services book.
gem install rest-open-uri
Example usage:
response = open('https://wherever/foo',
:method => :put,
:http_basic_authentication => ['my-user', 'my-passwd'],
:body => 'payload')
puts response.read
I'm a big fan of rest-client, which does just enough to be useful without getting in the way of your implementation. It handles exceptions intelligently, and supports logging and auth, out of the box.
HyperactiveResource is in its infancy, but it's looking pretty good.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With