Permalink
Browse files

More examples in readme.

  • Loading branch information...
1 parent e80cbc5 commit d95974b2ddb047aa0688635fb50b303d1658ea85 @jnunemaker committed Apr 22, 2012
Showing with 39 additions and 4 deletions.
  1. +39 −4 README.md
View
@@ -10,20 +10,55 @@ gem install httparty
## Requirements
-* multijson and multixml
+* multi_json and multi_xml
* You like to party!
## Examples
-See http://github.com/jnunemaker/httparty/tree/master/examples
+```ruby
+# Use the class methods to get down to business quickly
+response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
+puts response.body, response.code, response.message, response.headers.inspect
+
+response.each do |item|
+ puts item['user']['screen_name']
+end
+
+# Or wrap things up in your own class
+class Twitter
+ include HTTParty
+ base_uri 'twitter.com'
+
+ def initialize(u, p)
+ @auth = {:username => u, :password => p}
+ end
+
+ # which can be :friends, :user or :public
+ # options[:query] can be things like since, since_id, count, etc.
+ def timeline(which=:friends, options={})
+ options.merge!({:basic_auth => @auth})
+ self.class.get("/statuses/#{which}_timeline.json", options)
+ end
+
+ def post(text)
+ options = { :query => {:status => text}, :basic_auth => @auth }
+ self.class.post('/statuses/update.json', options)
+ end
+end
+
+twitter = Twitter.new(config['email'], config['password'])
+pp twitter.timeline
+```
+
+See the [examples directory](http://github.com/jnunemaker/httparty/tree/master/examples) for even more goodies.
## Command Line Interface
-httparty also includes the executable <tt>httparty</tt> which can be
+httparty also includes the executable `httparty` which can be
used to query web services and examine the resulting output. By default
it will output the response as a pretty-printed Ruby object (useful for
grokking the structure of output). This can also be overridden to output
-formatted XML or JSON. Execute <tt>httparty --help</tt> for all the
+formatted XML or JSON. Execute `httparty --help` for all the
options. Below is an example of how easy it is.
```

0 comments on commit d95974b

Please sign in to comment.