Skip to content

Commit

Permalink
updated to nearly full support of twitter api
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.addictedtonew.com/public/gems/twitter@76 fe7eae16-9a24-0410-a59d-9e59979e88be
  • Loading branch information
jnunemaker committed Aug 4, 2007
1 parent 139a820 commit c2d8c55
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 33 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
0.1.2 - to be released soon
0.2.0 - Aug 4, 2007
* added sent_messages
* alias direct_messages to received_messages
* added create_friendship
* added destroy_friendship
* added featured to retrieve the featured twitter users
* added replies
* added destroy to destroy a status by id
* added status to find a status by id
* added active support as an extra dependency
* implemented d method to send direct messages (jnewland)
* fixed since argument in direct_messages method (jnewland)
0.1.1 - May 20, 2007
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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[ 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 active_support).each { |f| require f }
%w(uri cgi net/http yaml rubygems hpricot active_support).each { |f| require f }

require 'twitter/version'
require 'twitter/easy_class_maker'
Expand Down
63 changes: 54 additions & 9 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,79 @@ def followers
users(call(:followers))
end

# waiting for twitter to correclty implement this in the api as it is documented
# TODO: uncomment this when it is working
# def featured
# users(call(:featured))
# end
# Returns a single status for a given id
def status(id)
statuses(call("show/#{id}")).first
end

# returns all the profile information and the last status for a user
def user(id_or_screenname)
users(request("users/show/#{id_or_screenname}.xml", :auth => true)).first
end

# Returns an array of statuses that are replies
def replies
statuses(call(:replies))
end

# Destroys a status by id
def destroy(id)
call("destroy/#{id}")
end

# waiting for twitter to correctly implement this in the api as it is documented
def featured
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.
# TODO: allow since_id and page as well for direct messages
def direct_messages(since=nil)
path = 'direct_messages.xml'
since.nil? ? 1 : path << "?since=#{CGI.escape(since.to_s)}"
doc = request(path, { :auth => true })
(doc/:direct_message).inject([]) { |dms, dm| dms << DirectMessage.new_from_xml(dm); dms }
end
alias :received_messages :direct_messages

# Returns 20 direct messages sent by auth user
# TODO: allow since_id and page as well for sent messages
def sent_messages(since=nil)
path = 'direct_messages/sent.xml'
since.nil? ? 1 : path << "?since=#{CGI.escape(since.to_s)}"
doc = request(path, { :auth => true })
(doc/:direct_message).inject([]) { |dms, dm| dms << DirectMessage.new_from_xml(dm); dms }
end

# destroys a give direct message by id if the auth user is a recipient
# TODO: return http status code
def destroy_direct_message(id)
request("direct_messages/destroy/#{id}.xml", :auth => true)
end

#Sends a direct message to <code>text</code> to <code>user</code>
# Sends a direct message <code>text</code> to <code>user</code>
def d(user, text)
url = URI.parse("http://#{@@api_url}/direct_messages/new.xml")
req = Net::HTTP::Post.new(url.path)

req.basic_auth(@config[:email], @config[:password])
req.set_form_data({'text' => text, 'user' => user})

response = Net::HTTP.new(url.host, url.port).start { |http| http.request(req) }
DirectMessage.new_from_xml(parse(response.body).at('direct_message'))
end

# Befriends the user specified in the ID parameter as the authenticating user.
def create_friendship(id_or_screenname)
users(request("friendships/create/#{id_or_screenname}.xml", :auth => true)).first
end

def destroy_friendship(id_or_screenname)
users(request("friendships/destroy/#{id_or_screenname}.xml", :auth => true)).first
end

# Updates your twitter with whatever status string is passed in
def post(status)
url = URI.parse("http://#{@@api_url}/statuses/update.xml")
Expand Down Expand Up @@ -111,7 +156,7 @@ def call(method, options={})
request(path, options)
end

def request(path, options)
def request(path, options={})
options.reverse_merge!({:headers => { "User-Agent" => @config[:email] }})
begin
response = Net::HTTP.start(@@api_url, 80) do |http|
Expand Down
26 changes: 15 additions & 11 deletions lib/twitter/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,27 @@ def follower
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
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

def important
config = create_or_find_config

puts
Twitter::Base.new(config['email'], config['password']).timeline(:friends).each do |s|
if config['important'].include?(s.user.screen_name)
puts "#{s.text}\n-- #{s.user.name} at #{s.created_at}"
puts
if config['important'].nil?
puts "You have not listed your most important twitter buddies in your config file.\nYou can add important twitterers by adding the following to your config file:\nimportant:\n- jnunemaker\n- frankfurter"
else
Twitter::Base.new(config['email'], config['password']).timeline(:friends).each do |s|
if config['important'].include?(s.user.screen_name)
puts "#{s.text}\n-- #{s.user.name} at #{s.created_at}"
puts
end
end
end
end
Expand Down
33 changes: 24 additions & 9 deletions lib/twitter/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@ module Twitter
class User
include EasyClassMaker

attributes :id, :name, :screen_name, :status, :location, :description, :url, :profile_image_url
attributes :id, :name, :screen_name, :status, :location, :description, :url,
:profile_image_url, :profile_background_color, :profile_text_color, :profile_link_color,
:profile_sidebar_fill_color, :profile_sidebar_border_color, :friends_count, :followers_count,
:favourites_count, :statuses_count, :utc_offset

class << self
# Creates a new user from a piece of xml
def new_from_xml(xml)
User.new do |u|
u.id = (xml).at('id').innerHTML
u.name = (xml).at('name').innerHTML
u.screen_name = (xml).at('screen_name').innerHTML
u.location = (xml).at('location').innerHTML
u.description = (xml).at('description').innerHTML
u.url = (xml).at('url').innerHTML
u.profile_image_url = (xml).at('profile_image_url').innerHTML
u.status = Status.new_from_xml(xml) if (xml).at('status')
u.id = (xml).at('id').innerHTML
u.name = (xml).at('name').innerHTML
u.screen_name = (xml).at('screen_name').innerHTML
u.location = (xml).at('location').innerHTML
u.description = (xml).at('description').innerHTML
u.url = (xml).at('url').innerHTML
u.profile_image_url = (xml).at('profile_image_url').innerHTML

# optional, not always present
u.profile_background_color = (xml).at('profile_background_color').innerHTML if (xml).at('profile_background_color')
u.profile_text_color = (xml).at('profile_text_color').innerHTML if (xml).at('profile_text_color')
u.profile_link_color = (xml).at('profile_link_color').innerHTML if (xml).at('profile_link_color')
u.profile_sidebar_fill_color = (xml).at('profile_sidebar_fill_color').innerHTML if (xml).at('profile_sidebar_fill_color')
u.profile_sidebar_border_color = (xml).at('profile_sidebar_border_color').innerHTML if (xml).at('profile_sidebar_border_color')
u.friends_count = (xml).at('friends_count').innerHTML if (xml).at('friends_count')
u.followers_count = (xml).at('followers_count').innerHTML if (xml).at('followers_count')
u.favourites_count = (xml).at('favourites_count').innerHTML if (xml).at('favourites_count')
u.statuses_count = (xml).at('statuses_count').innerHTML if (xml).at('statuses_count')
u.utc_offset = (xml).at('utc_offset').innerHTML if (xml).at('utc_offset')
u.status = Status.new_from_xml(xml) if (xml).at('status')
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/version.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Twitter #:nodoc:
module VERSION #:nodoc:
MAJOR = 0
MINOR = 1
TINY = 1
MINOR = 2
TINY = 0

STRING = [MAJOR, MINOR, TINY].join('.')
end
Expand Down
43 changes: 43 additions & 0 deletions test/unit/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def setup
puts 'Friends', @t.friends, "*"*50
end

test 'should be able to get featured users' do
puts 'Featured', @t.featured, "*"*50
end

test 'should be able to get friends for another user' do
puts 'Friends For', @t.friends_for('jnunemaker'), "*"*50
end
Expand All @@ -36,4 +40,43 @@ def setup
test 'should be able to get direct messages for auth user' do
puts 'Direct Messages', @t.direct_messages, "*"*50
end

test 'should be able to get direct messages for auth user by alias received messages' do
puts 'Recieved Messages', @t.received_messages, "*"*50
end

test 'should be able to send a direct message' do
@t.d('jnunemaker', 'just testing')
end

test 'should be able to get sent messages for auth user' do
puts 'Sent Messages', @t.sent_messages, "*"*50
end

test 'should be able to get a status by id' do
puts "Status 185005122", @t.status(185005122).inspect, "*"*50
end

test 'should be able to get replies for auth user' do
puts "Replies", @t.replies, "*"*50
end

test "should be able to get a user's info" do
puts "User", @t.user('jnunemaker').inspect, "*"*50
end

test 'should be able to create and destroy friendships' do
puts "Destroying Friendship with Snitch Test", @t.destroy_friendship('snitch_test'), "*"*50
puts "Creating Friendship with Snitch Test", @t.create_friendship('snitch_test'), "*"*50
end
#
# test 'should be able to destroy a status' do
# # this has to be checked individually, create a status, put the id in and make sure it was deleted
# #@t.destroy(185855442)
# end
#
# test 'should be able to destroy a direct message' do
# # must be tested individually
# @t.destroy_direct_message(4687032)
# end
end

0 comments on commit c2d8c55

Please sign in to comment.