Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor into Twitter::Base#object_attr_reader

  • Loading branch information...
commit 11fa58a4307b32c21854992c2efc26cde5f87f60 1 parent 87206e7
@sferik authored
View
2  README.md
@@ -529,7 +529,7 @@ Here are some fun facts about this library:
* It is implemented in just 2,000 lines of Ruby code
* With over 5,000 lines of specs, the spec-to-code ratio is about 2.5:1
-* The spec suite contains over 750 examples and runs in about 5 seconds
+* The spec suite contains over 800 examples and runs in about 5 seconds
* It has 100% C0 code coverage (the tests execute every line of
source code at least once)
* It is comprehensive: you can request all documented Twitter REST API
View
60 lib/twitter/base.rb
@@ -36,6 +36,33 @@ def self.from_response(response={})
new(response[:body])
end
+ # Create a new object (or NullObject) from attributes
+ #
+ # @param klass [Class]
+ # @param key1 [Symbol]
+ # @param key2 [Symbol]
+ def self.object_attr_reader(klass, key1, key2=nil)
+ define_method key1 do
+ ivar = :"@#{key1}"
+ return instance_variable_get(ivar) if instance_variable_defined?(ivar)
+ object = if @attrs[key1]
+ if key2.nil?
+ Twitter.const_get(klass).new(@attrs[key1])
+ else
+ attrs = @attrs.dup
+ value = attrs.delete(key1)
+ Twitter.const_get(klass).new(value.update(key2 => attrs))
+ end
+ else
+ Twitter::NullObject.new
+ end
+ instance_variable_set(ivar, object)
+ end
+ define_method "#{key1}?" do
+ !!self.send(key1)
+ end
+ end
+
# Initializes a new object
#
# @param attrs [Hash]
@@ -53,39 +80,6 @@ def [](method)
nil
end
- # Create a new object from another object
- #
- # @param klass [Class]
- # @param key1 [Symbol]
- # @param key2 [Symbol]
- def new_without_self(klass, key1, key2)
- ivar = :"@#{key1}"
- return instance_variable_get(ivar) if instance_variable_defined?(ivar)
- object = if @attrs[key1]
- attrs = @attrs.dup
- value = attrs.delete(key1)
- klass.new(value.update(key2 => attrs))
- else
- Twitter::NullObject.new
- end
- instance_variable_set(ivar, object)
- end
-
- # Create a new object (or NullObject) from attributes
- #
- # @param klass [Class]
- # @param key [Symbol]
- def new_or_null_object(klass, key)
- ivar = :"@#{key}"
- return instance_variable_get(ivar) if instance_variable_defined?(ivar)
- object = if @attrs[key]
- klass.new(@attrs[key])
- else
- Twitter::NullObject.new
- end
- instance_variable_set(ivar, object)
- end
-
private
# @param attr [Symbol]
View
2  lib/twitter/creatable.rb
@@ -11,7 +11,7 @@ def created_at
end
def created?
- !@attrs[:created_at].nil?
+ !!@attrs[:created_at]
end
end
View
23 lib/twitter/direct_message.rb
@@ -6,26 +6,7 @@ class DirectMessage < Twitter::Identity
include Twitter::Creatable
attr_reader :text
alias full_text text
-
- # @return [Twitter::User, Twitter::NullObject]
- def recipient
- new_or_null_object(Twitter::User, :recipient)
- end
-
- # @return [Boolean]
- def recipient?
- !recipient.nil?
- end
-
- # @return [Twitter::User, Twitter::NullObject]
- def sender
- new_or_null_object(Twitter::User, :sender)
- end
-
- # @return [Boolean]
- def sender?
- !sender.nil?
- end
-
+ object_attr_reader :User, :recipient
+ object_attr_reader :User, :sender
end
end
View
11 lib/twitter/list.rb
@@ -6,6 +6,7 @@ class List < Twitter::Identity
include Twitter::Creatable
attr_reader :description, :following, :full_name, :member_count,
:mode, :name, :slug, :subscriber_count
+ object_attr_reader :User, :user
# @return [String] The URL to the list members.
def members_url(protocol="https")
@@ -25,15 +26,5 @@ def url(protocol="https")
end
alias uri url
- # @return [Twitter::User, Twitter::NullObject]
- def user
- new_or_null_object(Twitter::User, :user)
- end
-
- # @return [Boolean]
- def user?
- !user.nil?
- end
-
end
end
View
21 lib/twitter/place.rb
@@ -5,25 +5,8 @@ class Place < Twitter::Identity
attr_reader :attributes, :country, :full_name, :name, :url, :woeid
alias uri url
alias woe_id woeid
-
- # @return [Twitter::Geo, Twitter::NullObject]
- def bounding_box
- new_or_null_object(Twitter::GeoFactory, :bounding_box)
- end
-
- # @return [Boolean]
- def bounding_box?
- !bounding_box.nil?
- end
-
- def contained_within
- new_or_null_object(Twitter::Place, :contained_within)
- end
-
- # @return [Boolean]
- def contained_within?
- !contained_within.nil?
- end
+ object_attr_reader :GeoFactory, :bounding_box
+ object_attr_reader :Place, :contained_within
alias contained? contained_within?
# @return [String]
View
22 lib/twitter/relationship.rb
@@ -2,6 +2,8 @@
module Twitter
class Relationship < Twitter::Base
+ object_attr_reader :SourceUser, :source
+ object_attr_reader :TargetUser, :target
# Initializes a new object
#
@@ -11,26 +13,6 @@ def initialize(attrs={})
@attrs = attrs[:relationship]
end
- # @return [Twitter::SourceUser, Twitter::NullObject]
- def source
- new_or_null_object(Twitter::SourceUser, :source)
- end
-
- # @return [Boolean]
- def source?
- !source.nil?
- end
-
- # @return [Twitter::TargetUser, Twitter::NullObject]
- def target
- new_or_null_object(Twitter::TargetUser, :target)
- end
-
- # @return [Boolean]
- def target?
- !target.nil?
- end
-
# Update the attributes of a Relationship
#
# @param attrs [Hash]
View
2  lib/twitter/search_results.rb
@@ -55,7 +55,7 @@ def results_per_page
alias count results_per_page
def search_metadata?
- !@attrs[:search_metadata].nil?
+ !!@attrs[:search_metadata]
end
# @return [Integer]
View
12 lib/twitter/settings.rb
@@ -5,16 +5,6 @@ class Settings < Twitter::Base
attr_reader :always_use_https, :discoverable_by_email, :geo_enabled,
:language, :protected, :screen_name, :show_all_inline_media, :sleep_time,
:time_zone
-
- # @return [Twitter::Place, Twitter::NullObject]
- def trend_location
- new_or_null_object(Twitter::Place, :trend_location)
- end
-
- # @return [Boolean]
- def trend_location?
- !trend_location.nil?
- end
-
+ object_attr_reader :Place, :trend_location
end
end
View
6 lib/twitter/trend_results.rb
@@ -34,7 +34,11 @@ def initialize(attrs={})
#
# @return [Time]
def as_of
- @as_of ||= Time.parse(@attrs[:as_of]) unless @attrs[:as_of].nil?
+ @as_of ||= Time.parse(@attrs[:as_of]) if as_of?
+ end
+
+ def as_of?
+ !!@attrs[:as_of]
end
# @return [Twitter::Place, NullObject]
View
69 lib/twitter/tweet.rb
@@ -1,5 +1,4 @@
require 'twitter/creatable'
-require 'twitter/identity'
module Twitter
class Tweet < Twitter::Identity
@@ -16,6 +15,16 @@ class Tweet < Twitter::Identity
alias favourited? favorited?
alias in_reply_to_tweet_id in_reply_to_status_id
alias retweeters_count retweet_count
+ object_attr_reader :GeoFactory, :geo
+ object_attr_reader :Metadata, :metadata
+ object_attr_reader :Place, :place
+ object_attr_reader :Tweet, :retweeted_status
+ alias retweet retweeted_status
+ alias retweeted_tweet retweeted_status
+ alias retweet? retweeted_status?
+ alias retweeted? retweeted_status?
+ alias retweeted_tweet? retweeted_status?
+ object_attr_reader :User, :user, :status
# @return [Boolean]
def entities?
@@ -37,16 +46,6 @@ def full_text
end
end
- # @return [Twitter::Geo]
- def geo
- new_or_null_object(Twitter::GeoFactory, :geo)
- end
-
- # @return [Boolean]
- def geo?
- !geo.nil?
- end
-
# @note Must include entities in your request for this method to work
# @return [Array<Twitter::Entity::Hashtag>]
def hashtags
@@ -59,48 +58,11 @@ def media
@media ||= entities(Twitter::MediaFactory, :media)
end
- # @return [Twitter::Metadata, Twitter::NullObject]
- def metadata
- new_or_null_object(Twitter::Metadata, :metadata)
- end
-
- # @return [Boolean]
- def metadata?
- !metadata.nil?
- end
-
- # @return [Twitter::Place, Twitter::NullObject]
- def place
- new_or_null_object(Twitter::Place, :place)
- end
-
- # @return [Boolean]
- def place?
- !place.nil?
- end
-
# @return [Boolean]
def reply?
- !in_reply_to_status_id.nil?
+ !!in_reply_to_status_id
end
- # If this Tweet is a retweet, the original Tweet is available here.
- #
- # @return [Twitter::Tweet]
- def retweeted_status
- new_or_null_object(self.class, :retweeted_status)
- end
- alias retweet retweeted_status
- alias retweeted_tweet retweeted_status
-
- # @return [Boolean]
- def retweeted_status?
- !retweeted_status.nil?
- end
- alias retweet? retweeted_status?
- alias retweeted? retweeted_status?
- alias retweeted_tweet? retweeted_status?
-
# @note Must include entities in your request for this method to work
# @return [Array<Twitter::Entity::Symbol>]
def symbols
@@ -119,15 +81,6 @@ def urls
@urls ||= entities(Twitter::Entity::Url, :urls)
end
- # @return [Twitter::User]
- def user
- new_without_self(Twitter::User, :user, :status)
- end
-
- def user?
- !user.nil?
- end
-
# @note Must include entities in your request for this method to work
# @return [Array<Twitter::Entity::UserMention>]
def user_mentions
View
20 lib/twitter/user.rb
@@ -29,6 +29,10 @@ class User < Twitter::BasicUser
alias tweets_count statuses_count
alias update_count statuses_count
alias updates_count statuses_count
+ object_attr_reader :Tweet, :status, :user
+ alias tweet status
+ alias tweet? status?
+ alias tweeted? status?
# @return [Array<Twitter::Entity::Url>]
def description_urls
@@ -54,7 +58,7 @@ def profile_banner_url_https(size=:web)
end
def profile_banner_url?
- !@attrs[:profile_banner_url].nil?
+ !!@attrs[:profile_banner_url]
end
alias profile_banner_url_https? profile_banner_url?
@@ -81,22 +85,10 @@ def profile_image_url_https(size=:normal)
end
def profile_image_url?
- !@attrs[:profile_image_url_https].nil?
+ !!@attrs[:profile_image_url_https]
end
alias profile_image_url_https? profile_image_url?
- # @return [Twitter::Tweet]
- def status
- new_without_self(Twitter::Tweet, :status, :user)
- end
- alias tweet status
-
- def status?
- !status.nil?
- end
- alias tweet? status?
- alias tweeted? status?
-
# @return [String] The URL to the user.
def url(protocol="https")
"#{protocol}://twitter.com/#{screen_name}"
View
11 spec/twitter/trend_results_spec.rb
@@ -13,6 +13,17 @@
end
end
+ describe "#as_of?" do
+ it "returns true when as_of is set" do
+ trend_results = Twitter::TrendResults.new(:id => 1, :as_of => "2012-08-24T23:24:14Z")
+ expect(trend_results.as_of?).to be_true
+ end
+ it "returns false when as_of is not set" do
+ trend_results = Twitter::TrendResults.new(:id => 1)
+ expect(trend_results.as_of?).to be_false
+ end
+ end
+
describe "#created_at" do
it "returns a Time when created_at is set" do
trend_results = Twitter::TrendResults.new(:id => 1, :created_at => "2012-08-24T23:24:14Z")
Please sign in to comment.
Something went wrong with that request. Please try again.