Permalink
Browse files

added friends_for method which allows getting the friends for any use…

…r by their id or username

git-svn-id: http://svn.addictedtonew.com/public/gems/twitter@43 fe7eae16-9a24-0410-a59d-9e59979e88be
  • Loading branch information...
1 parent 1f210f8 commit 21ca95ffa3f42aaf7728c3d5c2aa5f1f9ed84fe7 jnunemaker committed Mar 31, 2007
Showing with 49 additions and 23 deletions.
  1. +1 −1 Rakefile
  2. +1 −1 lib/twitter.rb
  3. +27 −9 lib/twitter/base.rb
  4. +20 −11 lib/twitter/command.rb
  5. +0 −1 test/unit/base.rb
View
@@ -44,7 +44,7 @@ hoe = Hoe.new(GEM_NAME, VERS) do |p|
# == Optional
#p.changes - A description of the release's latest changes.
- p.extra_deps = %w( hpricot )
+ p.extra_deps << %w( hpricot activesupport )
#p.spec_extras - A hash of extra values to set in the gemspec.
end
View
@@ -1,4 +1,4 @@
-%w(uri net/http yaml rubygems hpricot).each { |f| require f }
+%w(uri net/http yaml rubygems hpricot active_support).each { |f| require f }
require 'twitter/version'
require 'twitter/easy_class_maker'
View
@@ -39,11 +39,21 @@ def friends
users(call(:friends))
end
+ # Returns an array of users who are friends for the id or username passed in
+ def friends_for(id)
+ users(call(:friends, {:args => {:id => id}}))
+ end
+
# Returns an array of users who are following you
def followers
users(call(:followers))
end
+ # waiting for twitter to correclty implement this in the api as it is documented
+ # def featured
+ # users(call(:featured))
+ # end
+
# Updates your twitter with whatever status string is passed in
def post(status)
url = URI.parse("http://#{@@api_url}/statuses/update.xml")
@@ -53,11 +63,11 @@ def post(status)
req.set_form_data({'status' => status})
result = Net::HTTP.new(url.host, url.port).start { |http| http.request(req) }
- Status.new_from_xml(Hpricot.XML(result.body).at('status'))
+ Status.new_from_xml(parse(result.body).at('status'))
end
alias :update :post
- private
+ private
# Converts xml to an array of statuses
def statuses(doc)
(doc/:status).inject([]) { |statuses, status| statuses << Status.new_from_xml(status); statuses }
@@ -68,26 +78,34 @@ def users(doc)
(doc/:user).inject([]) { |users, user| users << User.new_from_xml(user); users }
end
- # Calls whatever api method requested
+ # Calls whatever api method requested that deals with statuses
#
# ie: call(:public_timeline, :auth => false)
- def call(method, arg_options={})
- options = { :auth => true }.merge(arg_options)
+ def call(method, options={})
+ options.reverse_merge!({ :auth => true, :args => {} })
path = "/statuses/#{method.to_s}.xml"
- headers = { "User-Agent" => @config[:email] }
-
+ path += '?' + options[:args].inject('') { |qs, h| qs += "#{h[0]}=#{h[1]}&"; qs } unless options[:args].blank?
+ request(path, options)
+ end
+
+ def request(path, options)
+ options.reverse_merge!({:headers => { "User-Agent" => @config[:email] }})
begin
response = Net::HTTP.start(@@api_url, 80) do |http|
- req = Net::HTTP::Get.new(path, headers)
+ req = Net::HTTP::Get.new(path, options[:headers])
req.basic_auth(@config[:email], @config[:password]) if options[:auth]
http.request(req)
end
raise BadResponse unless response.message == 'OK'
- Hpricot.XML(response.body)
+ parse(response.body)
rescue
raise CantConnect
end
end
+
+ def parse(response)
+ Hpricot.XML(response)
+ end
end
end
@@ -2,7 +2,7 @@
# It is only used and included in the bin/twitter file.
module Twitter
class Command
- @@commands = [:post, :timeline, :friends, :friend, :followers, :follower]
+ @@commands = [:post, :timeline, :friends, :friend, :followers, :follower, :featured]
@@template = <<EOF
# .twitter
@@ -19,18 +19,13 @@ class Command
class << self
def process!
command = ARGV.shift
-
- case command
- when "post" then Command.post
- when "timeline" then Command.timeline
- when "friends" then Command.friends
- when "friend" then Command.friend
- when "followers" then Command.followers
- when "follower" then Command.follower
+
+ if @@commands.include?(command.intern)
+ send(command)
else
puts "\nUsage: twitter <command> [options]\n\nAvailable Commands:"
- Twitter::Command.commands.each do |com|
- puts " - #{com}"
+ Twitter::Command.commands.each do |c|
+ puts " - #{c}"
end
end
end
@@ -64,6 +59,7 @@ def timeline
puts
Twitter::Base.new(config['email'], config['password']).timeline(timeline).each do |s|
puts "#{s.text}\n-- #{s.created_at} by #{s.user.name}"
+ puts
end
puts
end
@@ -95,6 +91,7 @@ def friend
Twitter::Base.new(config['email'], config['password']).friends.each do |u|
if u.screen_name == screen_name
puts "#{u.name} last updated #{u.status.created_at}\n-- #{u.status.text}"
+ puts
found = true
end
end
@@ -139,6 +136,18 @@ def follower
puts
end
+ def featured
+ puts
+ puts 'This is all implemented, just waiting for twitter to get the api call working'
+ # config = create_or_find_config
+ #
+ # puts
+ # Twitter::Base.new(config['email'], config['password']).featured.each do |u|
+ # puts "#{u.name} last updated #{u.status.created_at}\n-- #{u.status.text}"
+ # puts
+ # end
+ end
+
private
# Checks for the config, creates it if not found
def create_or_find_config
View
@@ -8,5 +8,4 @@ def setup
test 'should have friend and public class level timelines' do
assert_equal 3, Twitter::Base.timelines.size
end
-
end

0 comments on commit 21ca95f

Please sign in to comment.