Skip to content

Commit

Permalink
Break up Twitter::User class into 4 classes
Browse files Browse the repository at this point in the history
* Twitter::BasicUser
* Twitter::SourceUser
* Twitter::TargetUser
* Twitter::User
  • Loading branch information
sferik committed Jul 12, 2012
1 parent 437f0d1 commit 9d4f1e5
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ vulnerabilities are discovered.

Here are some fun facts about the 3.0 release:

* The entire library is implemented in just 2,216 lines of code
* The entire library is implemented in just 2,245 lines of code
* With over 5,000 lines of specs, the spec-to-code ratio is well over 2:1
* The spec suite contains 643 examples and runs in under 2 seconds on a MacBook
* The spec suite contains 657 examples and runs in under 2 seconds on a MacBook
* This project has 100% C0 code coverage (the tests execute every line of
source code at least once)
* At the time of release, this library is comprehensive: you can request all
Expand Down
10 changes: 10 additions & 0 deletions lib/twitter/basic_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'twitter/identity'

module Twitter
class BasicUser < Twitter::Identity
attr_reader :following, :screen_name

alias following? following

end
end
7 changes: 4 additions & 3 deletions lib/twitter/relationship.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
require 'twitter/base'
require 'twitter/user'
require 'twitter/source_user'
require 'twitter/target_user'

module Twitter
class Relationship < Twitter::Base

# @return [Twitter::User]
def source
@source ||= Twitter::User.fetch_or_create(@attrs[:source]) unless @attrs[:source].nil?
@source ||= Twitter::SourceUser.fetch_or_create(@attrs[:source]) unless @attrs[:source].nil?
end

# @return [Twitter::User]
def target
@target ||= Twitter::User.fetch_or_create(@attrs[:target]) unless @attrs[:target].nil?
@target ||= Twitter::TargetUser.fetch_or_create(@attrs[:target]) unless @attrs[:target].nil?
end

# Update the attributes of a Relationship
Expand Down
15 changes: 15 additions & 0 deletions lib/twitter/source_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'twitter/basic_user'

module Twitter
class SourceUser < BasicUser
attr_reader :all_replies, :blocking, :can_dm, :followed_by, :marked_spam,
:notifications_enabled, :want_retweets
alias all_replies? all_replies
alias blocking? blocking
alias can_dm? can_dm
alias followed_by? followed_by
alias marked_spam? marked_spam
alias notifications_enabled? notifications_enabled
alias want_retweets? want_retweets
end
end
8 changes: 8 additions & 0 deletions lib/twitter/target_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'twitter/basic_user'

module Twitter
class TargetUser < BasicUser
attr_reader :followed_by
alias followed_by? followed_by
end
end
30 changes: 10 additions & 20 deletions lib/twitter/user.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
require 'twitter/basic_user'
require 'twitter/core_ext/hash'
require 'twitter/creatable'
require 'twitter/identity'
require 'twitter/status'

module Twitter
class User < Twitter::Identity
class User < BasicUser
include Twitter::Creatable
attr_reader :all_replies, :blocking, :can_dm, :connections,
:contributors_enabled, :default_profile, :default_profile_image,
:description, :favourites_count, :follow_request_sent, :followed_by,
:followers_count, :following, :friends_count, :geo_enabled,
:is_translator, :lang, :listed_count, :location, :marked_spam, :name,
:notifications, :notifications_enabled, :profile_background_color,
:profile_background_image_url, :profile_background_image_url_https,
:profile_background_tile, :profile_image_url, :profile_image_url_https,
attr_reader :connections, :contributors_enabled, :default_profile,
:default_profile_image, :description, :favourites_count,
:follow_request_sent, :followers_count, :friends_count, :geo_enabled,
:is_translator, :lang, :listed_count, :location, :name, :notifications,
:profile_background_color, :profile_background_image_url,
:profile_background_image_url_https, :profile_background_tile,
:profile_link_color, :profile_sidebar_border_color,
:profile_sidebar_fill_color, :profile_text_color,
:profile_use_background_image, :protected, :screen_name, :statuses_count,
:time_zone, :url, :utc_offset, :verified, :want_retweets
alias all_replies? all_replies
alias blocking? blocking
alias can_dm? can_dm
:profile_use_background_image, :protected, :statuses_count, :time_zone,
:url, :utc_offset, :verified
alias contributors_enabled? contributors_enabled
alias default_profile? default_profile
alias default_profile_image? default_profile_image
alias follow_request_sent? follow_request_sent
alias following? following
alias followed_by? followed_by
alias favorite_count favourites_count
alias favorites_count favourites_count
alias favourite_count favourites_count
alias follower_count followers_count
alias friend_count friends_count
alias geo_enabled? geo_enabled
alias is_translator? is_translator
alias marked_spam? marked_spam
alias notifications? notifications
alias notifications_enabled? notifications_enabled
alias profile_background_tile? profile_background_tile
alias profile_use_background_image? profile_use_background_image
alias protected? protected
Expand All @@ -48,7 +39,6 @@ class User < Twitter::Identity
alias update_count statuses_count
alias updates_count statuses_count
alias verified? verified
alias want_retweets? want_retweets

# Return the URL to the user's profile image
#
Expand Down
23 changes: 23 additions & 0 deletions spec/twitter/basic_user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'helper'

describe Twitter::BasicUser do

describe "#==" do
it "returns true when objects IDs are the same" do
saved_search = Twitter::BasicUser.new(:id => 1, :name => "foo")
other = Twitter::BasicUser.new(:id => 1, :name => "bar")
(saved_search == other).should be_true
end
it "returns false when objects IDs are different" do
saved_search = Twitter::BasicUser.new(:id => 1)
other = Twitter::BasicUser.new(:id => 2)
(saved_search == other).should be_false
end
it "returns false when classes are different" do
saved_search = Twitter::BasicUser.new(:id => 1)
other = Twitter::Identity.new(:id => 1)
(saved_search == other).should be_false
end
end

end
4 changes: 2 additions & 2 deletions spec/twitter/relationship_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe "#source" do
it "returns a User when source is set" do
source = Twitter::Relationship.new(:relationship => {:source => {:id => 7505382}}).source
source.should be_a Twitter::User
source.should be_a Twitter::SourceUser
end
it "returns nil when source is not set" do
source = Twitter::Relationship.new.source
Expand All @@ -16,7 +16,7 @@
describe "#target" do
it "returns a User when target is set" do
target = Twitter::Relationship.new(:relationship => {:target => {:id => 7505382}}).target
target.should be_a Twitter::User
target.should be_a Twitter::TargetUser
end
it "returns nil when target is not set" do
target = Twitter::Relationship.new.target
Expand Down
23 changes: 23 additions & 0 deletions spec/twitter/source_user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'helper'

describe Twitter::SourceUser do

describe "#==" do
it "returns true when objects IDs are the same" do
saved_search = Twitter::SourceUser.new(:id => 1, :name => "foo")
other = Twitter::SourceUser.new(:id => 1, :name => "bar")
(saved_search == other).should be_true
end
it "returns false when objects IDs are different" do
saved_search = Twitter::SourceUser.new(:id => 1)
other = Twitter::SourceUser.new(:id => 2)
(saved_search == other).should be_false
end
it "returns false when classes are different" do
saved_search = Twitter::SourceUser.new(:id => 1)
other = Twitter::Identity.new(:id => 1)
(saved_search == other).should be_false
end
end

end
23 changes: 23 additions & 0 deletions spec/twitter/target_user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'helper'

describe Twitter::TargetUser do

describe "#==" do
it "returns true when objects IDs are the same" do
saved_search = Twitter::TargetUser.new(:id => 1, :name => "foo")
other = Twitter::TargetUser.new(:id => 1, :name => "bar")
(saved_search == other).should be_true
end
it "returns false when objects IDs are different" do
saved_search = Twitter::TargetUser.new(:id => 1)
other = Twitter::TargetUser.new(:id => 2)
(saved_search == other).should be_false
end
it "returns false when classes are different" do
saved_search = Twitter::TargetUser.new(:id => 1)
other = Twitter::Identity.new(:id => 1)
(saved_search == other).should be_false
end
end

end

0 comments on commit 9d4f1e5

Please sign in to comment.