diff --git a/examples/aaws.rb b/examples/aaws.rb index 2d33478..7006e55 100644 --- a/examples/aaws.rb +++ b/examples/aaws.rb @@ -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 diff --git a/examples/delicious.rb b/examples/delicious.rb index d42e601..99358c3 100644 --- a/examples/delicious.rb +++ b/examples/delicious.rb @@ -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 diff --git a/examples/twitter.rb b/examples/twitter.rb index 9926aee..64ebda3 100644 --- a/examples/twitter.rb +++ b/examples/twitter.rb @@ -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)