twittertype 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,6 +5,7 @@ module TwitterType
5
5
  :tweet_count,
6
6
  :reply_count,
7
7
  :retweet_count,
8
+ :original_count,
8
9
  :link_count,
9
10
  :inferred_type
10
11
 
@@ -12,6 +13,7 @@ module TwitterType
12
13
  @tweet_count = 0
13
14
  @reply_count = 0
14
15
  @retweet_count = 0
16
+ @original_count = 0
15
17
  @link_count = 0
16
18
  @screen_name = screen_name
17
19
  end
@@ -20,18 +22,32 @@ module TwitterType
20
22
  begin
21
23
  @tweet_count = @tweet_count + 1
22
24
 
23
- if tweet.text.slice(0, 1) == '@'
25
+ if reply?(tweet)
24
26
  @reply_count = @reply_count + 1
25
27
  return
26
28
  end
27
29
 
28
- @retweet_count = @retweet_count + 1 if tweet.text.slice(0, 2) == 'RT'
29
- @link_count = @link_count + 1 if tweet.text.index('http://') != nil
30
+ @retweet_count = @retweet_count + 1 if retweet?(tweet)
31
+ @link_count = @link_count + 1 if has_link?(tweet)
32
+ @original_count = @original_count + 1 if !retweet?(tweet) && !reply?(tweet)
33
+
30
34
  rescue NoMethodError => root
31
35
  raise ArgumentError.new("Missing method responses in tweet structure:" + root.to_s)
32
36
  end
33
37
  end
34
38
 
39
+ def retweet?(tweet)
40
+ tweet.text.slice(0, 2) == 'RT'
41
+ end
42
+
43
+ def has_link?(tweet)
44
+ tweet.text.index('http://') != nil
45
+ end
46
+
47
+ def reply?(tweet)
48
+ tweet.text.slice(0, 1) == '@'
49
+ end
50
+
35
51
  def to_s
36
52
  type_to_s = (@inferred_type ? "type " + @inferred_type.inspect + ", " : "")
37
53
  @screen_name + ": " + type_to_s + "#tweets " + @tweet_count.to_s + ", #replies " + @reply_count.to_s + ", #retweets " + @retweet_count.to_s + ", #links " + @link_count.to_s + "\n"
@@ -16,6 +16,7 @@ module TwitterType
16
16
  @client.gather_recent_tweets_for(screen_name)
17
17
  rescue Twitter::RESTError => error
18
18
  raise ProtectedUserAccessError.new(error.to_s) if ProtectedUserAccessError.fits(error)
19
+ raise InvalidUserAccessError.new(error.to_s) if InvalidUserAccessError.fits(error)
19
20
  raise TwitterClientError.new(error.to_s)
20
21
  end
21
22
  end
@@ -36,4 +37,12 @@ module TwitterType
36
37
  return !error.code.index(CODE).nil?
37
38
  end
38
39
  end
40
+
41
+ class InvalidUserAccessError < TwitterClientError
42
+ CODE = "404"
43
+
44
+ def self.fits(error)
45
+ return !error.code.index(CODE).nil?
46
+ end
47
+ end
39
48
  end
@@ -19,6 +19,9 @@ module TwitterType
19
19
  rescue TwitterType::ProtectedUserAccessError => error
20
20
  @profile = ProfileFactory.new(user).build([])
21
21
  @profile.inferred_type = :unknown_protected_user
22
+ rescue TwitterType::InvalidUserAccessError => error
23
+ @profile = ProfileFactory.new(user).build([])
24
+ @profile.inferred_type = :unknown_user
22
25
  end
23
26
 
24
27
  self
@@ -63,6 +63,23 @@ describe TwitterType::TweeterProfile, " being updated" do
63
63
 
64
64
  end
65
65
 
66
+ it "should increase the original count for each non retweet and non reply tweet sent to a user" do
67
+ @profile.original_count.should == 0
68
+
69
+ @profile.update_from(Tweet.new("@user text"))
70
+ @profile.original_count.should == 0
71
+
72
+ @profile.update_from(Tweet.new("RT text"))
73
+ @profile.original_count.should == 0
74
+
75
+ @profile.update_from(Tweet.new("original"))
76
+ @profile.original_count.should == 1
77
+
78
+ @profile.update_from(Tweet.new("text http://www.twitter.com", nil))
79
+ @profile.original_count.should == 2
80
+
81
+ end
82
+
66
83
  it "should increase the reply count for each tweet sent to a user" do
67
84
  @profile.reply_count.should == 0
68
85
 
@@ -1,5 +1,6 @@
1
1
  require "spec"
2
2
  require "twitterclientwrapper"
3
+ require 'twitter'
3
4
 
4
5
  describe TwitterType::TwitterClientWrapper do
5
6
 
@@ -15,7 +16,7 @@ describe TwitterType::TwitterClientWrapper do
15
16
  TwitterClientWrapper.new.client.class.should == TwitterClient
16
17
  end
17
18
 
18
- it "should turn raise a custom exception when an attempt to access a protected user's tweets is made" do
19
+ it "should raise a custom exception when an attempt to access a protected user's tweets is made" do
19
20
  protected_user_access_error = Twitter::RESTError.new
20
21
  protected_user_access_error.code = "401"
21
22
 
@@ -25,6 +26,16 @@ describe TwitterType::TwitterClientWrapper do
25
26
  lambda{TwitterClientWrapper.new(client).gather_recent_tweets_for("protected_user")}.should raise_error(ProtectedUserAccessError)
26
27
  end
27
28
 
29
+ it "should raise a custom exception when an attempt to access an unknown user's is made" do
30
+ invalid_user_access_error = Twitter::RESTError.new
31
+ invalid_user_access_error.code = "404"
32
+
33
+ client = mock
34
+ client.stub!(:gather_recent_tweets_for).with("invalid_user").and_raise(invalid_user_access_error)
35
+
36
+ lambda{TwitterClientWrapper.new(client).gather_recent_tweets_for("invalid_user")}.should raise_error(InvalidUserAccessError)
37
+ end
38
+
28
39
  it "should turn any other exceptiona raised by client into a generci exception" do
29
40
  access_error = Twitter::RESTError.new
30
41
  access_error.code = "404"
@@ -34,6 +34,16 @@ describe TypeInferrer do
34
34
 
35
35
  end
36
36
 
37
+ it "should use an unknown_user type for any invalid screen name" do
38
+ @mock_client.stub!(:gather_recent_tweets_for).with(VALID_TWITTER_USER).and_raise(InvalidUserAccessError.new(nil))
39
+ @cut.client = @mock_client
40
+
41
+ @cut.profile.should == nil
42
+ @cut.infer(VALID_TWITTER_USER)
43
+ @cut.profile.inferred_type.should == :unknown_user
44
+
45
+ end
46
+
37
47
  it "should infer a type for a Twitter User" do
38
48
  tweets = Array.new(1) {|i| Tweet.new("test", "to user")}
39
49
  @mock_client.stub!(:gather_recent_tweets_for).with(VALID_TWITTER_USER).and_return(tweets)
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andy Marks
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-01 00:00:00 +11:00
17
+ date: 2010-03-08 00:00:00 +11:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency