Cli*_*ach 19

如果您只需要处理REST,那么rest-client库就太棒了.

如果您使用的API不是完全RESTful - 或者即使它们是 - HTTParty确实值得一试.它简化了使用REST API以及非RESTful Web API的过程.看看这段代码(从上面的链接复制):

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"}}}
Run Code Online (Sandbox Code Playgroud)


Aar*_*nni 5

rest-open-uri是整个RESTful Web Services书中大量使用的.

gem install rest-open-uri
Run Code Online (Sandbox Code Playgroud)

用法示例:

response = open('https://wherever/foo',
                :method => :put,
                :http_basic_authentication => ['my-user', 'my-passwd'],
                :body => 'payload')

puts response.read
Run Code Online (Sandbox Code Playgroud)