From 99cd89d33d6f82a9f757a16753c2d08270bb4e8c Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Sun, 27 Jul 2008 19:13:44 -0400 Subject: [PATCH] added delicious example and tweaked the twitter example --- examples/delicious.rb | 37 +++++++++++++++++++++++++++++++++++++ examples/twitter.rb | 29 +++++++++++++++++------------ 2 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 examples/delicious.rb diff --git a/examples/delicious.rb b/examples/delicious.rb new file mode 100644 index 0000000..7954214 --- /dev/null +++ b/examples/delicious.rb @@ -0,0 +1,37 @@ +dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) +require File.join(dir, 'web') +require 'pp' +config = YAML::load(File.read(File.join(ENV['HOME'], '.delicious'))) + +class Delicious + include Web + + base_uri 'https://api.del.icio.us/v1' + format :xml + + def initialize(user, pass) + self.class.basic_auth(user, pass) + end + + # query params that filter the posts are: + # tag (optional). Filter by this tag. + # dt (optional). Filter by this date (CCYY-MM-DDThh:mm:ssZ). + # url (optional). Filter by this url. + # ie: posts(:query => {:tag => 'ruby'}) + def posts(options={}) + self.class.get('/posts/get', options)['posts']['post'].map { |b| b.to_struct } + end + + # query params that filter the posts are: + # tag (optional). Filter by this tag. + # count (optional). Number of items to retrieve (Default:15, Maximum:100). + def recent(options={}) + self.class.get('/posts/recent', options)['posts']['post'].map { |b| b.to_struct } + end +end + +pp Delicious.new(config['username'], config['password']).posts + +puts '', 'RECENT' +pp Delicious.new(config['username'], config['password']).recent + diff --git a/examples/twitter.rb b/examples/twitter.rb index bf966af..5da5e30 100644 --- a/examples/twitter.rb +++ b/examples/twitter.rb @@ -6,24 +6,29 @@ config = YAML::load(File.read(File.join(ENV['HOME'], '.twitter'))) class Twitter include Web base_uri 'twitter.com' - format :xml def initialize(user, pass) self.class.basic_auth user, pass end - def timeline - self.class.get('/statuses/user_timeline.xml')['statuses'].map(&:to_struct) + def timeline(which=:friends, options={}) + self.class.get("/statuses/#{which}_timeline.xml", options)['statuses'].map(&:to_struct) + end + + def post(text) + self.class.post('/statuses/update.xml', :query => {:status => text})['status'].to_struct end end -twitter = Twitter.new(config['email'], config['password']) -statuses = twitter.timeline -pp statuses -statuses.each do |s| - puts s.user.name - puts s.text - puts s.created_at - puts '', '' -end \ No newline at end of file +twitter = Twitter.new(config['email'], config['password']) + +twitter.timeline.each do |s| + puts s.user.name, s.text, "#{s.created_at} #{s.id}", '' +end + +# twitter.timeline(:friends, :query => {:since_id => 868482746}).each do |s| +# puts s.user.name, s.text, "#{s.created_at} #{s.id}", '' +# end +# +# pp twitter.post('this is a test') \ No newline at end of file