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 module AAWS
class Book class Book
include HTTParty include HTTParty
# sets the base url for each request
base_uri 'http://ecs.amazonaws.com' base_uri 'http://ecs.amazonaws.com'
# adds default parameters for each request
default_params :Service => 'AWSECommerceService', :Operation => 'ItemSearch', :SearchIndex => 'Books' default_params :Service => 'AWSECommerceService', :Operation => 'ItemSearch', :SearchIndex => 'Books'
# parse xml automatically
format :xml format :xml
def initialize(key) def initialize(key)
# update default params with amazon access key
self.class.default_params :AWSAccessKeyId => key self.class.default_params :AWSAccessKeyId => key
end end
def search(options={}) def search(options={})
raise ArgumentError, 'You must search for something' if options[:query].blank? 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 } 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'] self.class.get('/onca/xml', options)['ItemSearchResponse']['Items']
end end
end end

View file

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

View file

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