Skip to content
Browse files

renamed my test files correctly with _test suffix, woops; added direc…

…t_messages method which gets all the direct_messages for the authorized user

git-svn-id: http://svn.addictedtonew.com/public/gems/twitter@44 fe7eae16-9a24-0410-a59d-9e59979e88be
  • Loading branch information...
1 parent 21ca95f commit 0f4d699a5310dc8a4e2997b82853f5466292b320 jnunemaker committed Apr 1, 2007
View
5 CHANGELOG
@@ -1,3 +1,8 @@
+0.1.0 - March 31, 2007
+ * added friends_for method
+ * added a few tests
+ * removed relative_created_at as it is deprecated
+ * separated out the call method into call, request and parse methods
0.0.5 - just a bit of code cleanup
0.0.4 - added :location, :description, :url, :profile_image_url to user class (Alex Payne)
0.0.3 - added a bit more informative message when things go wrong
View
8 Manifest.txt
@@ -5,9 +5,15 @@ setup.rb
lib/twitter.rb
lib/twitter/base.rb
lib/twitter/command.rb
+lib/twitter/direct_message.rb
lib/twitter/easy_class_maker.rb
lib/twitter/status.rb
lib/twitter/user.rb
lib/twitter/version.rb
examples/twitter.rb
-bin/twitter
+bin/twitter
+test/test_helper.rb
+test/unit/base_test.rb
+test/unit/direct_message_test.rb
+test/unit/status_test.rb
+test/unit/user_test.rb
View
2 Rakefile
@@ -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 activesupport )
+ p.extra_deps << %w[ hpricot ]
#p.spec_extras - A hash of extra values to set in the gemspec.
end
View
3 lib/twitter.rb
@@ -4,4 +4,5 @@
require 'twitter/easy_class_maker'
require 'twitter/base'
require 'twitter/user'
-require 'twitter/status'
+require 'twitter/status'
+require 'twitter/direct_message'
View
16 lib/twitter/base.rb
@@ -54,6 +54,14 @@ def followers
# users(call(:featured))
# end
+ # Returns an array of all the direct messages for the authenticated user
+ #
+ # <tt>since</tt> - (optional) Narrows the resulting list of direct messages to just those sent after the specified HTTP-formatted date.
+ def direct_messages(since=nil)
+ doc = request('direct_messages.xml', { :auth => true })
+ (doc/:direct_message).inject([]) { |dms, dm| dms << DirectMessage.new_from_xml(dm); dms }
+ end
+
# Updates your twitter with whatever status string is passed in
def post(status)
url = URI.parse("http://#{@@api_url}/statuses/update.xml")
@@ -68,12 +76,12 @@ def post(status)
alias :update :post
private
- # Converts xml to an array of statuses
+ # Converts an hpricot doc to an array of statuses
def statuses(doc)
(doc/:status).inject([]) { |statuses, status| statuses << Status.new_from_xml(status); statuses }
end
- # Converts xml to an array of users
+ # Converts an hpricot doc to an array of users
def users(doc)
(doc/:user).inject([]) { |users, user| users << User.new_from_xml(user); users }
end
@@ -83,7 +91,7 @@ def users(doc)
# ie: call(:public_timeline, :auth => false)
def call(method, options={})
options.reverse_merge!({ :auth => true, :args => {} })
- path = "/statuses/#{method.to_s}.xml"
+ path = "statuses/#{method.to_s}.xml"
path += '?' + options[:args].inject('') { |qs, h| qs += "#{h[0]}=#{h[1]}&"; qs } unless options[:args].blank?
request(path, options)
end
@@ -92,7 +100,7 @@ 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, options[:headers])
+ req = Net::HTTP::Get.new('/' + path, options[:headers])
req.basic_auth(@config[:email], @config[:password]) if options[:auth]
http.request(req)
end
View
31 lib/twitter/command.rb
@@ -20,7 +20,7 @@ class << self
def process!
command = ARGV.shift
- if @@commands.include?(command.intern)
+ if !command.nil? && @@commands.include?(command.intern)
send(command)
else
puts "\nUsage: twitter <command> [options]\n\nAvailable Commands:"
@@ -58,19 +58,18 @@ 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 "#{s.text}\n-- #{s.user.name} at #{s.created_at}"
puts
end
- puts
end
def friends
config = create_or_find_config
puts
Twitter::Base.new(config['email'], config['password']).friends.each do |u|
- puts "#{u.name} (#{u.screen_name}) last updated #{u.status.created_at}\n-- #{u.status.text}"
- puts
+ puts "#{u.name} (#{u.screen_name})"
+ puts "#{u.status.text} at #{u.status.created_at}" unless u.status.nil?
end
end
@@ -85,19 +84,18 @@ def friend
end
screen_name = ARGV.shift
-
+
puts
found = false
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
+ puts "#{u.name} #{u.screen_name}"
+ puts "#{u.status.text} at #{u.status.created_at}" unless u.status.nil?
found = true
end
end
puts "Sorry couldn't find a friend of yours with #{screen_name} as a screen name" unless found
- puts
end
# Shows all followers and their last updated status
@@ -106,8 +104,8 @@ def followers
puts
Twitter::Base.new(config['email'], config['password']).followers.each do |u|
- puts "#{u.name} last updated #{u.status.created_at}\n-- #{u.status.text}"
- puts
+ puts "#{u.name} (#{u.screen_name})"
+ puts "#{u.status.text} at #{u.status.created_at}" unless u.status.nil?
end
end
@@ -127,13 +125,13 @@ def follower
found = false
Twitter::Base.new(config['email'], config['password']).followers.each do |u|
if u.screen_name == screen_name
- puts "#{u.name} (#{u.screen_name}) last updated #{u.status.created_at}\n-- #{u.status.text}"
+ puts "#{u.name} (#{u.screen_name})"
+ puts "#{u.status.text} at #{u.status.created_at}" unless u.status.nil?
found = true
end
end
puts "Sorry couldn't find a follower of yours with #{screen_name} as a screen name" unless found
- puts
end
def featured
@@ -151,11 +149,12 @@ def featured
private
# Checks for the config, creates it if not found
def create_or_find_config
+ home = ENV['HOME'] || ENV['USERPROFILE'] || ENV['HOMEPATH']
begin
- config = YAML::load open(ENV['HOME'] + "/.twitter")
+ config = YAML::load open(home + "/.twitter")
rescue
- open(ENV["HOME"] + '/.twitter','w').write(@@template)
- config = YAML::load open(ENV['HOME'] + "/.twitter")
+ open(home + '/.twitter','w').write(@@template)
+ config = YAML::load open(home + "/.twitter")
end
if config['email'] == nil or config['password'] == nil
View
22 lib/twitter/direct_message.rb
@@ -0,0 +1,22 @@
+module Twitter
+ class DirectMessage
+ include EasyClassMaker
+
+ attributes :id, :text, :sender_id, :recipient_id, :created_at, :sender_screen_name, :recipient_screen_name
+
+ class << self
+ # Creates a new status from a piece of xml
+ def new_from_xml(xml)
+ DirectMessage.new do |d|
+ d.id = (xml).at('id').innerHTML
+ d.text = (xml).at('text').innerHTML
+ d.sender_id = (xml).at('sender_id').innerHTML
+ d.recipient_id = (xml).at('recipient_id').innerHTML
+ d.created_at = (xml).at('created_at').innerHTML
+ d.sender_screen_name = (xml).at('sender_screen_name').innerHTML
+ d.recipient_screen_name = (xml).at('recipient_screen_name').innerHTML
+ end
+ end
+ end
+ end
+end
View
1 lib/twitter/status.rb
@@ -11,7 +11,6 @@ def new_from_xml(xml)
s.id = (xml).at('id').innerHTML
s.created_at = (xml).at('created_at').innerHTML
s.text = (xml).at('text').innerHTML
- s.created_at = (xml).at('created_at').innerHTML
s.user = User.new_from_xml(xml) if (xml).at('user')
end
end
View
3 test/test_helper.rb
@@ -17,4 +17,5 @@ def expect(expected_value, &block)
end
end
-# load config
+# load config
+CONFIG = YAML::load(open('/Users/nunemaker/.twitter'))
View
11 test/unit/base.rb
@@ -1,11 +0,0 @@
-require File.dirname(__FILE__) + '/../test_helper'
-
-class BaseTest < Test::Unit::TestCase
- def setup
- @t = Twitter::Base.new('', '')
- end
-
- test 'should have friend and public class level timelines' do
- assert_equal 3, Twitter::Base.timelines.size
- end
-end
View
39 test/unit/base_test.rb
@@ -0,0 +1,39 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class BaseTest < Test::Unit::TestCase
+ def setup
+ @t = Twitter::Base.new(CONFIG['email'], CONFIG['password'])
+ end
+
+ test 'should have friend and public class level timelines' do
+ assert_equal 3, Twitter::Base.timelines.size
+ end
+
+ test 'should be able to get public timeline' do
+ puts 'Public Timeline', @t.timeline(:public), "*"*50
+ end
+
+ test 'should be able to get friends timeline' do
+ puts 'Friends Timeline', @t.timeline(:friends), "*"*50
+ end
+
+ test 'should be able to get user timeline' do
+ puts 'User Timeline', @t.timeline(:user), "*"*50
+ end
+
+ test 'should be able to get friends for auth user' do
+ puts 'Friends', @t.friends, "*"*50
+ end
+
+ test 'should be able to get friends for another user' do
+ puts 'Friends For', @t.friends_for('jnunemaker'), "*"*50
+ end
+
+ test 'should be able to get followers for auth user' do
+ puts 'Followers', @t.followers, "*"*50
+ end
+
+ test 'should be able to get direct messages for auth user' do
+ puts 'Direct Messages', @t.direct_messages, "*"*50
+ end
+end
View
0 test/unit/command.rb
No changes.
View
39 test/unit/direct_message_test.rb
@@ -0,0 +1,39 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class DirectMessageTest < Test::Unit::TestCase
+ def setup
+ @xml = <<EOF
+<direct_message>
+ <id>331681</id>
+ <text>thanks for revving the twitter gem! had notice that it was broken but didn't have time to patch.</text>
+ <sender_id>18713</sender_id>
+ <recipient_id>4243</recipient_id>
+ <created_at>Sat Mar 10 22:10:37 +0000 2007</created_at>
+ <sender_screen_name>al3x</sender_screen_name>
+ <recipient_screen_name>jnunemaker</recipient_screen_name>
+</direct_message>
+EOF
+ end
+
+ test 'should create new from xml' do
+ d = Twitter::DirectMessage.new do |d|
+ d.id = '331681'
+ d.text = "thanks for revving the twitter gem! had notice that it was broken but didn't have time to patch."
+ d.sender_id = '18713'
+ d.recipient_id = '4243'
+ d.created_at = 'Sat Mar 10 22:10:37 +0000 2007'
+ d.sender_screen_name = 'al3x'
+ d.recipient_screen_name = 'jnunemaker'
+ end
+ d2 = Twitter::DirectMessage.new_from_xml(Hpricot.XML(@xml))
+
+ assert_equal d.id, d2.id
+ assert_equal d.text, d2.text
+ assert_equal d.sender_id, d2.sender_id
+ assert_equal d.recipient_id, d2.recipient_id
+ assert_equal d.created_at, d2.created_at
+ assert_equal d.sender_screen_name, d2.sender_screen_name
+ assert_equal d.recipient_screen_name, d2.recipient_screen_name
+ end
+
+end
View
0 test/unit/easy_class_maker.rb
No changes.
View
0 test/unit/status.rb → test/unit/status_test.rb
File renamed without changes.
View
0 test/unit/user.rb → test/unit/user_test.rb
File renamed without changes.

0 comments on commit 0f4d699

Please sign in to comment.
Something went wrong with that request. Please try again.