From d95974b2ddb047aa0688635fb50b303d1658ea85 Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Sun, 22 Apr 2012 15:44:45 -0400 Subject: [PATCH] More examples in readme. --- README.md | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0e10fbe3..bf90ca93 100644 --- a/README.md +++ b/README.md @@ -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 httparty 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 httparty --help for all the +formatted XML or JSON. Execute `httparty --help` for all the options. Below is an example of how easy it is. ```