HTTParty是目前最受Ruby爱好者欢迎的一个简单的HTTP/REST客户端库。
示例代码:
require'rubygems'
require'httparty'
classRepresentative
includeHTTParty
base_uri'whoismyrepresentative.com'
default_params:output=>'json'
format:json
defself.find_by_zip(zip)
get('/whoismyrep.php',:query=>{:zip=>zip})
end
defself.get_all_by_name(last_name)
get('/getall_reps_byname.php',:query=>{:lastname=>last_name})
end
end
putsRepresentative.get_all_by_name('Donnelly').inspect
#{"results"=>[{"district"=>"2","last"=>"Donnelly","first"=>"Joe","state"=>"IN","party"=>"D"}]}
评论