Permalink
Browse files

Documented and tweaked the examples a bit.

  • Loading branch information...
1 parent 0713c83 commit aafde86709f43342154b7bfd242671d60cf427d7 @jnunemaker committed Jul 28, 2008
Showing with 27 additions and 4 deletions.
  1. +10 −0 examples/aaws.rb
  2. +11 −3 examples/delicious.rb
  3. +6 −1 examples/twitter.rb
View
@@ -6,17 +6,27 @@
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
@@ -6,10 +6,14 @@
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 @@ def initialize(user, pass)
# 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 @@ def recent(options={})
end
end
-pp Delicious.new(config['username'], config['password']).posts
+delicious = Delicious.new(config['username'], config['password'])
+
+pp delicious.posts(:query => {:tag => 'ruby'})
+
+puts '', '*' * 50, ''
-puts '', 'RECENT'
-pp Delicious.new(config['username'], config['password']).recent
+pp delicious.recent
View
@@ -5,14 +5,19 @@
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)

0 comments on commit aafde86

Please sign in to comment.