Permalink
Browse files

Don't cache dynamic attributes

  • Loading branch information...
1 parent 7600cc3 commit 4bcd6782b24a23c4b2919b00ae7d736e3ee19a17 @sferik committed Jul 23, 2012
View
@@ -162,9 +162,9 @@ critical security vulnerabilities are discovered.
Here are some fun facts about the 3.0 release:
-* The entire library is implemented in just 2,125 lines of code
+* The entire library is implemented in just 2,133 lines of code
* With over 5,000 lines of specs, the spec-to-code ratio is over 2.5:1
-* The spec suite contains 667 examples and runs in under 2 seconds on a MacBook
+* The spec suite contains 669 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
@@ -15,6 +15,7 @@ class ActionFactory
# @raise [ArgumentError] Error raised when supplied argument is missing an :action key.
# @return [Twitter::Action::Favorite, Twitter::Action::Follow, Twitter::Action::ListMemberAdded, Twitter::Action::Mention, Twitter::Action::Reply, Twitter::Action::Retweet]
def self.fetch_or_new(attrs={})
+ return unless attrs
if type = attrs.delete(:action)
Twitter::Action.const_get(type.camelize.to_sym).fetch_or_new(attrs)
else
View
@@ -7,9 +7,9 @@ class Base
# Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
#
- # @overload self. attr_reader(attr)
+ # @overload self.attr_reader(attr)
# @param attr [Symbol]
- # @overload self. attr_reader(attrs)
+ # @overload self.attr_reader(attrs)
# @param attrs [Array<Symbol>]
def self.attr_reader(*attrs)
attrs.each do |attribute|
@@ -56,7 +56,7 @@ def self.store(object)
# @param response [Hash]
# @return [Twitter::Base]
def self.from_response(response={})
- self.fetch_or_new(response[:body])
+ fetch_or_new(response[:body])
end
# Retrieves an object from the identity map, or stores it in the
@@ -65,11 +65,12 @@ def self.from_response(response={})
# @param attrs [Hash]
# @return [Twitter::Base]
def self.fetch_or_new(attrs={})
- return self.new(attrs) unless identity_map
+ return unless attrs
+ return new(attrs) unless identity_map
- self.fetch(attrs) do
- object = self.new(attrs)
- self.store(object)
+ fetch(attrs) do
+ object = new(attrs)
+ store(object)
end
end
@@ -85,7 +86,7 @@ def initialize(attrs={})
#
# @param method [String, Symbol] Message to send to the object
def [](method)
- self.send(method.to_sym)
+ send(method.to_sym)
rescue NoMethodError
nil
end
@@ -105,13 +106,13 @@ def update(attrs)
# @param other [Twitter::Base]
# @return [Boolean]
def attr_equal(attr, other)
- self.class == other.class && !other.send(attr).nil? && self.send(attr) == other.send(attr)
+ self.class == other.class && !other.send(attr).nil? && send(attr) == other.send(attr)
end
# @param other [Twitter::Base]
# @return [Boolean]
def attrs_equal(other)
- self.class == other.class && !other.attrs.empty? && self.attrs == other.attrs
+ self.class == other.class && !other.attrs.empty? && attrs == other.attrs
end
end
View
@@ -62,7 +62,7 @@ def request(method, path, params={}, options={})
uri = URI(uri) unless uri.respond_to?(:host)
uri += path
request_headers = {}
- if self.credentials?
+ if credentials?
authorization = auth_header(method, uri, params)
request_headers[:authorization] = authorization.to_s
end
View
@@ -3,7 +3,6 @@
module Twitter
class Cursor
attr_reader :attrs, :collection
- alias body attrs
alias to_hash attrs
# Initializes a new Cursor object
@@ -13,7 +12,7 @@ class Cursor
# @param klass [Class] The class to instantiate object in the collection
# @return [Twitter::Cursor]
def self.from_response(response={}, method=:ids, klass=nil)
- self.new(response[:body], method, klass)
+ new(response[:body], method, klass)
end
# Initializes a new Cursor
@@ -9,12 +9,12 @@ class DirectMessage < Twitter::Identity
# @return [Twitter::User]
def recipient
- @recipient ||= Twitter::User.fetch_or_new(@attrs[:recipient]) unless @attrs[:recipient].nil?
+ @recipient ||= Twitter::User.fetch_or_new(@attrs[:recipient])
end
# @return [Twitter::User]
def sender
- @sender ||= Twitter::User.fetch_or_new(@attrs[:sender]) unless @attrs[:sender].nil?
+ @sender ||= Twitter::User.fetch_or_new(@attrs[:sender])
end
end
View
@@ -8,7 +8,7 @@ class Geo < Twitter::Base
# @param other [Twitter::Geo]
# @return [Boolean]
def ==(other)
- super || self.attr_equal(:coordinates, other) || self.attrs_equal(other)
+ super || attr_equal(:coordinates, other) || attrs_equal(other)
end
end
@@ -11,6 +11,7 @@ class GeoFactory
# @raise [ArgumentError] Error raised when supplied argument is missing a :type key.
# @return [Twitter::Geo::Point, Twitter::Geo::Polygon]
def self.fetch_or_new(attrs={})
+ return unless attrs
if type = attrs.delete(:type)
Twitter::Geo.const_get(type.camelize.to_sym).fetch_or_new(attrs)
else
View
@@ -31,13 +31,13 @@ def self.store(object)
# @return [Twitter::Identity]
def initialize(attrs={})
super
- raise ArgumentError, "argument must have an :id key" unless self.id
+ raise ArgumentError, "argument must have an :id key" unless id
end
# @param other [Twitter::Identity]
# @return [Boolean]
def ==(other)
- super || self.attr_equal(:id, other) || self.attrs_equal(other)
+ super || attr_equal(:id, other) || attrs_equal(other)
end
# @return [Integer]
View
@@ -11,7 +11,7 @@ class List < Twitter::Identity
# @return [Twitter::User]
def user
- @user ||= Twitter::User.fetch_or_new(@attrs[:user]) unless @attrs[:user].nil?
+ @user ||= Twitter::User.fetch_or_new(@attrs[:user])
end
end
@@ -10,6 +10,7 @@ class MediaFactory
# @raise [ArgumentError] Error raised when supplied argument is missing a :type key.
# @return [Twitter::Media::Photo]
def self.fetch_or_new(attrs={})
+ return unless attrs
if type = attrs.delete(:type)
Twitter::Media.const_get(type.camelize.to_sym).fetch_or_new(attrs)
else
View
@@ -8,7 +8,7 @@ class Place < Twitter::Identity
# @return [Twitter::Geo::Point, Twitter::Geo::Polygon]
def bounding_box
- @bounding_box ||= Twitter::GeoFactory.fetch_or_new(@attrs[:bounding_box]) unless @attrs[:bounding_box].nil?
+ @bounding_box ||= Twitter::GeoFactory.fetch_or_new(@attrs[:bounding_box])
end
# @return [String]
@@ -15,12 +15,12 @@ def initialize(attrs={})
# @return [Twitter::User]
def source
- @source ||= Twitter::SourceUser.fetch_or_new(@attrs[:source]) unless @attrs[:source].nil?
+ @source ||= Twitter::SourceUser.fetch_or_new(@attrs[:source])
end
# @return [Twitter::User]
def target
- @target ||= Twitter::TargetUser.fetch_or_new(@attrs[:target]) unless @attrs[:target].nil?
+ @target ||= Twitter::TargetUser.fetch_or_new(@attrs[:target])
end
# Update the attributes of a Relationship
View
@@ -10,7 +10,7 @@ class Settings < Twitter::Base
# @return [Twitter::Place]
def trend_location
- @trend_location ||= Twitter::Place.fetch_or_new(@attrs[:trend_location].first) unless @attrs[:trend_location].nil? || @attrs[:trend_location].empty?
+ @trend_location ||= Twitter::Place.fetch_or_new(Array(@attrs[:trend_location]).first)
end
end
View
@@ -9,15 +9,15 @@ class Size < Twitter::Base
# @param other [Twitter::Size]
# @return [Boolean]
def ==(other)
- super || self.size_equal(other) || self.attrs_equal(other)
+ super || size_equal(other) || attrs_equal(other)
end
protected
# @param other [Twitter::Size]
# @return [Boolean]
def size_equal(other)
- self.class == other.class && !other.h.nil? && self.h == other.h && !other.w.nil? && self.w == other.w
+ self.class == other.class && !other.h.nil? && h == other.h && !other.w.nil? && w == other.w
end
end
View
@@ -26,54 +26,56 @@ class Status < Twitter::Identity
# @return [Integer]
def favoriters_count
- @favoriters_count ||= @attrs[:favoriters_count]
- @favoriters_count.to_i if @favoriters_count
+ favoriters_count = @attrs[:favoriters_count]
+ favoriters_count.to_i if favoriters_count
end
alias favorite_count favoriters_count
# @return [String]
def from_user
- @attrs[:from_user] || self.user && self.user.screen_name
+ @attrs[:from_user] || user && user.screen_name
end
# @return [String]
# @note May be > 140 characters.
def full_text
- self.retweeted_status && self.retweeted_status.user ? "RT @#{self.retweeted_status.user.screen_name}: #{self.retweeted_status.text}" : self.text
+ retweeted_status && retweeted_status.user ? "RT @#{retweeted_status.user.screen_name}: #{retweeted_status.text}" : text
end
# @return [Twitter::Geo::Point, Twitter::Geo::Polygon]
def geo
- @geo ||= Twitter::GeoFactory.fetch_or_new(@attrs[:geo]) unless @attrs[:geo].nil?
+ @geo ||= Twitter::GeoFactory.fetch_or_new(@attrs[:geo])
end
# @note Must include entities in your request for this method to work
# @return [Array<Twitter::Entity::Hashtag>]
def hashtags
- @hashtags ||= unless @attrs[:entities].nil?
- Array(@attrs[:entities][:hashtags]).map do |hashtag|
+ if @attrs[:entities].nil?
+ warn "#{Kernel.caller.first}: To get hashtags, you must pass `:include_entities => true` when requesting the Twitter::Status."
+ []
+ else
+ @hashtags ||= Array(@attrs[:entities][:hashtags]).map do |hashtag|
Twitter::Entity::Hashtag.fetch_or_new(hashtag)
end
- else
- warn "#{Kernel.caller.first}: To get hashtags, you must pass `:include_entities => true` when requesting the Twitter::Status."
end
end
# @note Must include entities in your request for this method to work
- # @return [Array]
+ # @return [Array<Twitter::Media>]
def media
- @media ||= unless @attrs[:entities].nil?
- Array(@attrs[:entities][:media]).map do |media|
+ if @attrs[:entities].nil?
+ warn "#{Kernel.caller.first}: To get media, you must pass `:include_entities => true` when requesting the Twitter::Status."
+ []
+ else
+ @media ||= Array(@attrs[:entities][:media]).map do |media|
Twitter::MediaFactory.fetch_or_new(media)
end
- else
- warn "#{Kernel.caller.first}: To get media, you must pass `:include_entities => true` when requesting the Twitter::Status."
end
end
# @return [Twitter::Metadata]
def metadata
- @metadata ||= Twitter::Metadata.fetch_or_new(@attrs[:metadata]) unless @attrs[:metadata].nil?
+ @metadata ||= Twitter::Metadata.fetch_or_new(@attrs[:metadata])
end
# @return [Twitter::OEmbed]
@@ -84,56 +86,59 @@ def oembed(options={})
# @return [Twitter::Place]
def place
- @place ||= Twitter::Place.fetch_or_new(@attrs[:place]) unless @attrs[:place].nil?
+ @place ||= Twitter::Place.fetch_or_new(@attrs[:place])
end
# @return [Integer]
def repliers_count
- @repliers_count ||= @attrs[:repliers_count]
- @repliers_count.to_i if @repliers_count
+ repliers_count = @attrs[:repliers_count]
+ repliers_count.to_i if repliers_count
end
alias reply_count repliers_count
# If this status is itself a retweet, the original tweet is available here.
#
# @return [Twitter::Status]
def retweeted_status
- @retweeted_status ||= self.class.fetch_or_new(@attrs[:retweeted_status]) unless @attrs[:retweeted_status].nil?
+ @retweeted_status ||= self.class.fetch_or_new(@attrs[:retweeted_status])
end
# @return [String]
def retweeters_count
- @retweeters_count ||= (@attrs[:retweet_count] || @attrs[:retweeters_count])
- @retweeters_count.to_i if @retweeters_count
+ retweeters_count = (@attrs[:retweet_count] || @attrs[:retweeters_count])
+ retweeters_count.to_i if retweeters_count
end
alias retweet_count retweeters_count
# @note Must include entities in your request for this method to work
# @return [Array<Twitter::Entity::Url>]
def urls
- @urls ||= unless @attrs[:entities].nil?
- Array(@attrs[:entities][:urls]).map do |url|
+ if @attrs[:entities].nil?
+ warn "#{Kernel.caller.first}: To get URLs, you must pass `:include_entities => true` when requesting the Twitter::Status."
+ []
+ else
+ @urls ||= Array(@attrs[:entities][:urls]).map do |url|
Twitter::Entity::Url.fetch_or_new(url)
end
- else
- warn "#{Kernel.caller.first}: To get URLs, you must pass `:include_entities => true` when requesting the Twitter::Status."
end
end
# @return [Twitter::User]
def user
- @user ||= Twitter::User.fetch_or_new(@attrs.dup[:user].merge(:status => @attrs.except(:user))) unless @attrs[:user].nil?
+ return if @attrs[:user].nil?
+ @user ||= Twitter::User.fetch_or_new(@attrs.dup[:user].merge(:status => @attrs.except(:user)))
end
# @note Must include entities in your request for this method to work
# @return [Array<Twitter::Entity::UserMention>]
def user_mentions
- @user_mentions ||= unless @attrs[:entities].nil?
- Array(@attrs[:entities][:user_mentions]).map do |user_mention|
+ if @attrs[:entities].nil?
+ warn "#{Kernel.caller.first}: To get user mentions, you must pass `:include_entities => true` when requesting the Twitter::Status."
+ []
+ else
+ @user_mentions ||= Array(@attrs[:entities][:user_mentions]).map do |user_mention|
Twitter::Entity::UserMention.fetch_or_new(user_mention)
end
- else
- warn "#{Kernel.caller.first}: To get user mentions, you must pass `:include_entities => true` when requesting the Twitter::Status."
end
end
@@ -8,14 +8,14 @@ class Suggestion < Twitter::Base
# @param other [Twitter::Suggestion]
# @return [Boolean]
def ==(other)
- super || self.attr_equal(:slug, other) || self.attrs_equal(other)
+ super || attr_equal(:slug, other) || attrs_equal(other)
end
# @return [Array<Twitter::User>]
def users
@users ||= Array(@attrs[:users]).map do |user|
Twitter::User.fetch_or_new(user)
- end unless @attrs[:users].nil?
+ end
end
end
View
@@ -7,7 +7,7 @@ class Trend < Twitter::Base
# @param other [Twitter::Trend]
# @return [Boolean]
def ==(other)
- super || self.attr_equal(:name, other) || self.attrs_equal(other)
+ super || attr_equal(:name, other) || attrs_equal(other)
end
end
Oops, something went wrong.

0 comments on commit 4bcd678

Please sign in to comment.