Skip to content

Commit

Permalink
added friends_for method which allows getting the friends for any use…
Browse files Browse the repository at this point in the history
…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
jnunemaker committed Mar 31, 2007
1 parent 1f210f8 commit 21ca95f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/twitter.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
36 changes: 27 additions & 9 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 }
Expand All @@ -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
31 changes: 20 additions & 11 deletions lib/twitter/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion test/unit/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.