2008-07-28 10:49:53 -04:00
= httparty
2008-07-27 11:52:18 -04:00
== DESCRIPTION:
2008-07-28 13:20:03 -04:00
Makes http fun again!
2008-07-27 11:52:18 -04:00
== FEATURES/PROBLEMS:
2008-07-28 13:20:03 -04:00
* Easy get, post, put, delete requests
* Basic http authentication
* Default request query string parameters (ie: for api keys that are needed on each request)
2008-08-22 21:57:38 -04:00
* Automatic parsing of JSON and XML into ruby hashes based on response content-type
2008-07-27 11:52:18 -04:00
== SYNOPSIS:
2008-07-28 13:20:03 -04:00
The following is a simple example of wrapping Twitter's API for posting updates.
class Twitter
2008-07-28 16:48:22 -04:00
include HTTParty
base_uri 'twitter.com'
basic_auth 'username', 'password'
2008-07-28 13:20:03 -04:00
end
2008-07-28 16:48:22 -04:00
Twitter.post('/statuses/update.json', :query => {:status => "It's an HTTParty and everyone is invited!"})
2008-07-28 13:20:03 -04:00
That is really it! The object returned is a ruby hash that is decoded from Twitter's json response. JSON parsing is used because of the .json extension in the path of the request. You can also explicitly set a format (see the examples).
That works and all but what if you don't want to embed your username and password in the class? Below is an example to fix that:
class Twitter
include HTTParty
base_uri 'twitter.com'
2008-07-31 00:29:17 -04:00
def initialize(u, p)
@auth = {:username => u, :password => p}
2008-07-28 13:20:03 -04:00
end
2008-07-31 00:29:17 -04:00
2008-07-28 13:20:03 -04:00
def post(text)
2008-07-31 00:29:17 -04:00
options = { :query => {:status => text}, :basic_auth => @auth }
self.class.post('/statuses/update.json', options)
2008-07-28 13:20:03 -04:00
end
end
Twitter.new('username', 'password').post("It's an HTTParty and everyone is invited!")
2008-07-27 11:52:18 -04:00
== REQUIREMENTS:
2008-07-28 13:20:03 -04:00
* Active Support >= 2.1
2008-07-27 11:52:18 -04:00
== INSTALL:
2008-07-28 13:20:03 -04:00
* sudo gem install httparty