Web services seem to be multiplying like rabbits these days. For a good sampling of just how many there are check out the Programmable Web API list. In general it is pretty easy to consume basic REST web services but after you have done it enough it starts getting old. Thankfully for those of us who like to tinker with a lot of the new APIs there is HTTParty to make it quick and easy.
The author of HTTParty has a few examples that show all the features but I figured I would toss a couple more out there for good measure. To get started you will need to install HTTParty with gem:
[code lang=”bash”]
gem install httparty
[/code]
The first example uses the new BestBuy Remix API. The site has all the information on the web service they offer. You can search stores, products and determine if a product is available in a given area/store. In the following example I have implemented store search and a couple different ways of doing product search just to show how easy it is. I even tossed in a very hacked up method missing example. One thing to note in the following code is the URI escaping that goes on for some of the searches that allow free form input.
[code lang=”ruby”]
require ‘rubygems’
require ‘httparty’
class BestBuy
include HTTParty
base_uri ‘api.remix.bestbuy.com’
default_params :apiKey => ‘put your key here’
def self.get_all_stores
get(“/v1/stores”)
end
def self.get_stores_by_zip(zip)
get(“/v1/stores(postalCode=#{zip})”)
end
def self.get_stores_by_zip_and_distance(zip, distance)
get(“/v1/stores(area(#{zip},#{distance}))”)
end
def self.get_product_by_sku(sku)
get(“/v1/products/#{sku}.xml”)
end
def self.get_products(filter)
response = get(URI.escape(“/v1/products(#{filter})”))
response[“products”][“product”]
end
def self.method_missing(method_id, *args)
if match = /get_products_by_([_a-zA-Z]\w*)/.match(method_id.to_s)
attribute_names = match.captures.last.split(‘_and_’)
request = “”
attribute_names.each_with_index { |name, idx| request = request + name + “=” + args[idx] + (attribute_names.length-1 == idx ? “” : “&”) }
get_products(request)
else
super
end
end
end
[/code]
Here is the above in action:
[code lang=”ruby”]
pp BestBuy.get_stores_by_zip_and_distance(40299, 10)
pp BestBuy.get_product_by_sku(8880044)
pp BestBuy.get_products_by_manufacturer(‘canon’)
pp BestBuy.get_products(“manufacturer=’canon’&salePrice<33")
pp BestBuy.get_products_by_manufacturer_and_department('canon','video')
pp BestBuy.get_products_by_manufacturer_and_department('canon','accessories')
products = BestBuy.get_products_by_manufacturer_and_department('canon','accessories')
products.each do |product|
print "------------------------------------------------\n"
print product["name"] + "\n"
print product["longDescription"] + "\n"
end
[/code]
The second example I have uses the Congressional API that was just put out by the NY Times developer program. To get started it may help to look at the blog post introducing the congressional API. I’ve created this class in a different way than the above BestBuy just for diversity’s sake. You may notice that I had to define the format specifically here otherwise HTTParty can not figure out how to parse the return.
[code lang=”ruby”]
require ‘rubygems’
require ‘httparty’
class NYTimesCongress
include HTTParty
base_uri ‘http://api.nytimes.com/svc/politics/v2/us/legislative/congress’
format :xml
def initialize(apikey)
@apikey = apikey
end
# Roll-Call Votes
def rollCallVotes(congressNumber, sessionNumber, rollCallNumber)
NYTimesCongress.get(“/#{congressNumber}/house/sessions/#{sessionNumber}/votes/#{rollCallNumber}”, :query => {“api-key” => @apikey})
end
# Member Positions
def memberPositions(memberId)
NYTimesCongress.get(“/members/#{memberId}/votes”, :query => {“api-key” => @apikey})
end
# Members of Congress
def membersOfCongress(congressNumber)
NYTimesCongress.get(“/#{congressNumber}/house/members”, :query => {“api-key” => @apikey})
end
# Member Bio and Role
def memberBioAndRoles(memberId)
NYTimesCongress.get(“/members/#{memberId}”, :query => {“api-key” => @apikey})
end
end
[/code]
Here it is in action:
[code lang=”ruby”]
nyTimesCongress = NYTimesCongress.new(“your api key”)
pp nyTimesCongress.rollCallVotes(111, 1, 9)
pp nyTimesCongress.membersOfCongress(111)
pp nyTimesCongress.memberBioAndRoles(“Y000062”)
pp nyTimesCongress.memberPositions(“Y000062”)
[/code]
Just recently HTTParty got a command line interface that makes it even easier to fiddle with a new service. Here is an example of how easy it is to use the command line version with the Congressional API:
[code lang=”bash”]
httparty -f xml -a get “http://api.nytimes.com/svc/politics/v2/us/legislative/congress/members/Y000062?api-key=your api key”
OK
Copyright (c) 2009 The New York Times Company. All Rights Reserved.
Y000062
John Yarmuth
1947-11-04
M
http://yarmuth.house.gov
412211
111
House
Representative
KY
2009-01-06
2011-01-04
110
House
Representative
KY
2007-01-04
2009-01-03
[/code]
Thanks for this. I am starting out with Ruby and using web services is an interesting way to get to know the language better. I had some trouble with the HTTParty docs, but this is a great tutorial.