Skip to content

Commit

Permalink
Updated specs a bit. Added #timeline to CLI. Add #source, #truncated,…
Browse files Browse the repository at this point in the history
… #in_reply_to_status_id, #in_reply_to_user_id, #favorited to Twitter::Status. Added #protected to Twitter::User.
  • Loading branch information
jnunemaker committed Jul 22, 2008
1 parent 35dccbe commit d02d233
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 83 deletions.
20 changes: 19 additions & 1 deletion lib/twitter/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def run
mode 'install' do
def run
migrate
say 'Twitter setup installed.'
say 'Twitter installed.'
end
end

Expand Down Expand Up @@ -228,4 +228,22 @@ def run
end
end
end

mode 'timeline' do
argument( 'timeline' ) {
description 'the timeline you wish to see (friends, public, me)'
default 'friends'
}

def run
do_work do
timeline = params['timeline'].value == 'me' ? 'user' : params['timeline'].value
base.timeline(timeline.to_sym).each do |s|
Tweet.create_from_tweet(current_account, s) if timeline != :public
say "#{CGI::unescapeHTML(s.text)}\n --\e[34m #{CGI::unescapeHTML(s.user.name)}\e[32m at #{s.created_at}"
say "\e[0m"
end
end
end
end
}
2 changes: 1 addition & 1 deletion lib/twitter/cli/migrations/20080722194508_create_tweets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class CreateTweets < ActiveRecord::Migration
def self.up
create_table :tweets do |t|
t.datetime :occurred_at
t.boolean :truncated, :favorited, :protected, :default => false
t.boolean :truncated, :favorited, :user_protected, :default => false
t.integer :twitter_id, :user_id, :in_reply_to_status_id, :in_reply_to_user_id, :user_followers_count
t.text :body
t.string :source, :user_name, :user_screen_name, :user_location, :user_description, :user_profile_image_url, :user_url
Expand Down
17 changes: 17 additions & 0 deletions lib/twitter/cli/models/tweet.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
class Tweet < ActiveRecord::Base
belongs_to :account

def self.create_from_tweet(account, s)
tweet = account.tweets.find_or_initialize_by_twitter_id(s.id)
tweet.body = s.text
tweet.occurred_at = s.created_at

%w[truncated favorited in_reply_to_status_id in_reply_to_user_id source].each do |m|
tweet.send("#{m}=", s.send(m))
end

%w[id followers_count name screen_name location description
profile_image_url url protected].each do |m|
tweet.send("user_#{m}=", s.user.send(m))
end
tweet.save!
tweet
end
end
25 changes: 14 additions & 11 deletions lib/twitter/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ module Twitter
class Status
include EasyClassMaker

attributes :created_at, :id, :text, :user
attributes :created_at, :id, :text, :user, :source, :truncated, :in_reply_to_status_id, :in_reply_to_user_id, :favorited

class << self
# Creates a new status from a piece of xml
def new_from_xml(xml)
Status.new do |s|
s.id = (xml).at('id').innerHTML
s.created_at = (xml).at('created_at').innerHTML
s.text = (xml).get_elements_by_tag_name('text').innerHTML
s.user = User.new_from_xml(xml) if (xml).at('user')
end
end
# Creates a new status from a piece of xml
def self.new_from_xml(xml)
s = new
s.id = (xml).at('id').innerHTML
s.created_at = (xml).at('created_at').innerHTML
s.text = (xml).get_elements_by_tag_name('text').innerHTML
s.source = (xml).at('source').innerHTML
s.truncated = (xml).at('truncated').innerHTML == 'false' ? false : true
s.favorited = (xml).at('favorited').innerHTML == 'false' ? false : true
s.in_reply_to_status_id = (xml).at('in_reply_to_status_id').innerHTML
s.in_reply_to_user_id = (xml).at('in_reply_to_user_id').innerHTML
s.user = User.new_from_xml(xml.at('user')) if (xml).at('user')
s
end
end
end
55 changes: 27 additions & 28 deletions lib/twitter/user.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
module Twitter
class User
class User
include EasyClassMaker

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
:favourites_count, :statuses_count, :utc_offset , :protected

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

# 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
# Creates a new user from a piece of xml
def self.new_from_xml(xml)
u = new
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.protected = (xml).at('protected').innerHTML == 'false' ? false : true if (xml).at('protected')
u.status = Status.new_from_xml(xml.at('status')) if (xml).at('status')
u
end
end
end
47 changes: 32 additions & 15 deletions spec/status_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
require File.dirname(__FILE__) + '/spec_helper.rb'

describe "Twitter::Status" do
describe Twitter::Status do
it "should create new status from xml doc" do
xml = <<EOF
<status>
<created_at>Sat Mar 31 06:33:21 +0000 2007</created_at>
<id>16440221</id>
<text>Back from underground sushi with b/c/g/j/m. Hope jack and britt get in to ratatat, too!</text>
</status>
<created_at>Sun May 04 21:59:52 +0000 2008</created_at>
<id>803435310</id>
<text>Writing tests (rspec) for the twitter gem that all can run. Wish I would have done this when I wrote it years back.</text>
<source>&lt;a href="http://iconfactory.com/software/twitterrific"&gt;twitterrific&lt;/a&gt;</source>
<truncated>false</truncated>
<in_reply_to_status_id></in_reply_to_status_id>
<in_reply_to_user_id></in_reply_to_user_id>
<favorited>false</favorited>
<user>
<id>4243</id>
<name>John Nunemaker</name>
<screen_name>jnunemaker</screen_name>
<location>Indiana</location>
<description>Loves his wife, ruby, notre dame football and iu basketball</description>
<profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/52619256/ruby_enterprise_shirt_normal.jpg</profile_image_url>
<url>http://addictedtonew.com</url>
<protected>false</protected>
<followers_count>363</followers_count>
</user>
</status>
EOF
s = Twitter::Status.new do |s|
s.created_at = 'Sat Mar 31 06:33:21 +0000 2007'
s.id = '16440221'
s.text = 'Back from underground sushi with b/c/g/j/m. Hope jack and britt get in to ratatat, too!'
end
s2 = Twitter::Status.new_from_xml(Hpricot.XML(xml))

s.id.should == s2.id
s.created_at.should == s2.created_at
s.text.should == s2.text
s = Twitter::Status.new_from_xml(Hpricot.XML(xml))
s.id.should == '803435310'
s.created_at.should == 'Sun May 04 21:59:52 +0000 2008'
s.text.should == 'Writing tests (rspec) for the twitter gem that all can run. Wish I would have done this when I wrote it years back.'
s.source.should == '&lt;a href="http://iconfactory.com/software/twitterrific"&gt;twitterrific&lt;/a&gt;'
s.truncated.should == false
s.in_reply_to_status_id.should == ''
s.in_reply_to_user_id.should == ''
s.favorited.should == false
s.user.id.should == '4243'
s.user.name.should == 'John Nunemaker'
end
end
60 changes: 33 additions & 27 deletions spec/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
require File.dirname(__FILE__) + '/spec_helper.rb'

describe "Twitter::User" do
describe Twitter::User do
it "should create new user from xml doc" do
xml = <<EOF
<user>
<id>18713</id>
<name>Alex Payne</name>
<screen_name>al3x</screen_name>
<location>Arlington, VA</location>
<description>A description</description>
<profile_image_url>http://static.twitter.com/system/user/profile_image/18713/normal/361219175_c11b881657.jpg?1171954960</profile_image_url>
<url>http://www.al3x.net</url>
<id>18713</id>
<name>Alex Payne</name>
<screen_name>al3x</screen_name>
<location>San Francisco, CA</location>
<description>Oh, hi. No, I just work here.</description>
<profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/51961745/al3x_normal.jpg</profile_image_url>
<url>http://www.al3x.net</url>
<protected>false</protected>
<followers_count>2889</followers_count>
<status>
<created_at>Sun May 04 22:38:39 +0000 2008</created_at>
<id>803453211</id>
<text>@5dots Yes. Give me about 8 hours. *sigh*</text>
<source>&lt;a href="http://iconfactory.com/software/twitterrific"&gt;twitterrific&lt;/a&gt;</source>
<truncated>false</truncated>
<in_reply_to_status_id>803450314</in_reply_to_status_id>
<in_reply_to_user_id>618923</in_reply_to_user_id>
<favorited>false</favorited>
</status>
</user>
EOF
u = Twitter::User.new do |u|
u.id = '18713'
u.name = 'Alex Payne'
u.screen_name = 'al3x'
u.location = 'Arlington, VA'
u.description = 'A description'
u.profile_image_url = 'http://static.twitter.com/system/user/profile_image/18713/normal/361219175_c11b881657.jpg?1171954960'
u.url = 'http://www.al3x.net'
end

u2 = Twitter::User.new_from_xml(Hpricot.XML(xml))

u.id.should == u2.id
u.name.should == u2.name
u.screen_name.should == u2.screen_name
u.location.should == u2.location
u.description.should == u2.description
u.profile_image_url.should == u2.profile_image_url
u.url.should == u2.url
u = Twitter::User.new_from_xml(Hpricot.XML(xml))
u.id.should == '18713'
u.name.should =='Alex Payne'
u.screen_name.should == 'al3x'
u.location.should == 'San Francisco, CA'
u.description.should == 'Oh, hi. No, I just work here.'
u.profile_image_url.should == 'http://s3.amazonaws.com/twitter_production/profile_images/51961745/al3x_normal.jpg'
u.url.should == 'http://www.al3x.net'
u.protected.should == false
u.followers_count.should == '2889'
u.status.text.should == '@5dots Yes. Give me about 8 hours. *sigh*'
end
end

0 comments on commit d02d233

Please sign in to comment.