1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Documented and tweaked the examples a bit.

This commit is contained in:
John Nunemaker 2008-07-28 12:58:40 -04:00
parent 0713c832c1
commit aafde86709
3 changed files with 27 additions and 4 deletions

View file

@ -6,17 +6,27 @@ config = YAML::load(File.read(File.join(ENV['HOME'], '.aaws')))
module AAWS
class Book
include HTTParty
# sets the base url for each request
base_uri 'http://ecs.amazonaws.com'
# adds default parameters for each request
default_params :Service => 'AWSECommerceService', :Operation => 'ItemSearch', :SearchIndex => 'Books'
# parse xml automatically
format :xml
def initialize(key)
# update default params with amazon access key
self.class.default_params :AWSAccessKeyId => key
end
def search(options={})
raise ArgumentError, 'You must search for something' if options[:query].blank?
# amazon uses camelized query params
options[:query] = options[:query].inject({}) { |h, q| h[q[0].to_s.camelize] = q[1]; h }
# make a request and return the items (NOTE: this doesn't handle errors at this point)
self.class.get('/onca/xml', options)['ItemSearchResponse']['Items']
end
end

View file

@ -6,10 +6,14 @@ config = YAML::load(File.read(File.join(ENV['HOME'], '.delicious')))
class Delicious
include HTTParty
# sets the base url for each request
base_uri 'https://api.del.icio.us/v1'
# parse xml automatically
format :xml
def initialize(user, pass)
# set basic http authentication for all requests
self.class.basic_auth(user, pass)
end
@ -19,6 +23,7 @@ class Delicious
# url (optional). Filter by this url.
# ie: posts(:query => {:tag => 'ruby'})
def posts(options={})
# get posts and convert to structs so we can do .key instead of ['key'] with results
self.class.get('/posts/get', options)['posts']['post'].map { |b| b.to_struct }
end
@ -30,8 +35,11 @@ class Delicious
end
end
pp Delicious.new(config['username'], config['password']).posts
delicious = Delicious.new(config['username'], config['password'])
puts '', 'RECENT'
pp Delicious.new(config['username'], config['password']).recent
pp delicious.posts(:query => {:tag => 'ruby'})
puts '', '*' * 50, ''
pp delicious.recent

View file

@ -5,14 +5,19 @@ config = YAML::load(File.read(File.join(ENV['HOME'], '.twitter')))
class Twitter
include HTTParty
# sets the base url for each request
base_uri 'twitter.com'
def initialize(user, pass)
# set basic http authentication for all requests
self.class.basic_auth user, pass
end
# which can be :friends, :user or :public
# options[:query] can be things like since, since_id, count, etc.
def timeline(which=:friends, options={})
self.class.get("/statuses/#{which}_timeline.xml", options)['statuses'].map(&:to_struct)
self.class.get("/statuses/#{which}_timeline.xml", options)['statuses'].map { |s| s.to_struct }
end
def post(text)