Skip to content

Commit

Permalink
renamed my test files correctly with _test suffix, woops; added direc…
Browse files Browse the repository at this point in the history
…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
jnunemaker committed Apr 1, 2007
1 parent 21ca95f commit 0f4d699
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 36 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 7 additions & 1 deletion Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 activesupport )
p.extra_deps << %w[ hpricot ]
#p.spec_extras - A hash of extra values to set in the gemspec.
end

Expand Down
3 changes: 2 additions & 1 deletion lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
16 changes: 12 additions & 4 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
31 changes: 15 additions & 16 deletions lib/twitter/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:"
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions lib/twitter/direct_message.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion lib/twitter/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ def expect(expected_value, &block)
end
end

# load config
# load config
CONFIG = YAML::load(open('/Users/nunemaker/.twitter'))
11 changes: 0 additions & 11 deletions test/unit/base.rb

This file was deleted.

39 changes: 39 additions & 0 deletions test/unit/base_test.rb
Original file line number Diff line number Diff line change
@@ -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
Empty file removed test/unit/command.rb
Empty file.
39 changes: 39 additions & 0 deletions test/unit/direct_message_test.rb
Original file line number Diff line number Diff line change
@@ -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
Empty file removed test/unit/easy_class_maker.rb
Empty file.
File renamed without changes.
File renamed without changes.

0 comments on commit 0f4d699

Please sign in to comment.