Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make all accessors lazy

  • Loading branch information...
commit 1de9147ca909510b17d71871a9c455eaed488d79 1 parent 6732706
@sferik authored
View
2  lib/twitter/base.rb
@@ -1,5 +1,7 @@
module Twitter
class Base
+ attr_accessor :attributes
+ alias :to_hash :attributes
def self.lazy_attr_reader(*attributes)
attributes.each do |attribute|
View
9 lib/twitter/configuration.rb
@@ -4,16 +4,13 @@
module Twitter
class Configuration < Twitter::Base
- attr_reader :photo_sizes
lazy_attr_reader :characters_reserved_per_media, :max_media_per_upload,
:non_username_paths, :photo_size_limit, :short_url_length, :short_url_length_https
- def initialize(attributes={})
- attributes = attributes.dup
- @photo_sizes = attributes.delete('photo_sizes').each_with_object({}) do |(key, value), object|
+ def photo_sizes
+ @photo_sizes ||= Array(@attributes['photo_sizes']).each_with_object({}) do |(key, value), object|
object[key] = Twitter::Size.new(value)
- end unless attributes['photo_sizes'].nil?
- super(attributes)
+ end
end
end
View
7 lib/twitter/creatable.rb
@@ -2,12 +2,9 @@
module Twitter
module Creatable
- attr_reader :created_at
- def initialize(attributes={})
- attributes = attributes.dup
- @created_at = Time.parse(attributes.delete('created_at')) unless attributes['created_at'].nil?
- super(attributes)
+ def created_at
+ @created_at ||= Time.parse(@attributes['created_at']) unless @attributes['created_at'].nil?
end
end
View
7 lib/twitter/cursor.rb
@@ -9,18 +9,17 @@ class Cursor < Twitter::Base
alias :previous :previous_cursor
def initialize(attributes, method, klass=nil)
- attributes = attributes.dup
- @collection = attributes.delete(method.to_s).map do |item|
+ super(attributes)
+ @collection = Array(attributes[method.to_s]).map do |item|
if klass
klass.new(item)
else
item
end
- end unless attributes[method.to_s].nil?
+ end
singleton_class.class_eval do
alias_method method.to_sym, :collection
end
- super(attributes)
end
def first?
View
16 lib/twitter/direct_message.rb
@@ -5,19 +5,19 @@
module Twitter
class DirectMessage < Twitter::Base
include Twitter::Creatable
- attr_reader :recipient, :sender
lazy_attr_reader :id, :text
- def initialize(attributes={})
- attributes = attributes.dup
- @recipient = Twitter::User.new(attributes.delete('recipient')) unless attributes['recipient'].nil?
- @sender = Twitter::User.new(attributes.delete('sender')) unless attributes['sender'].nil?
- super(attributes)
- end
-
def ==(other)
super || (other.class == self.class && other.id == self.id)
end
+ def recipient
+ @recipient ||= Twitter::User.new(@attributes['recipient']) unless @attributes['recipient'].nil?
+ end
+
+ def sender
+ @sender ||= Twitter::User.new(@attributes['sender']) unless @attributes['sender'].nil?
+ end
+
end
end
View
11 lib/twitter/list.rb
@@ -3,20 +3,17 @@
module Twitter
class List < Twitter::Base
- attr_reader :user
lazy_attr_reader :description, :following, :full_name, :id, :member_count,
:mode, :name, :slug, :subscriber_count, :uri
alias :following? :following
- def initialize(attributes={})
- attributes = attributes.dup
- @user = Twitter::User.new(attributes.delete('user')) unless attributes['user'].nil?
- super(attributes)
- end
-
def ==(other)
super || (other.class == self.class && other.id == self.id)
end
+ def user
+ @user ||= Twitter::User.new(@attributes['user']) unless @attributes['user'].nil?
+ end
+
end
end
View
15 lib/twitter/photo.rb
@@ -4,21 +4,18 @@
module Twitter
class Photo < Twitter::Base
- attr_reader :sizes
lazy_attr_reader :display_url, :expanded_url, :id, :indices, :media_url,
:media_url_https, :url
- def initialize(attributes={})
- attributes = attributes.dup
- @sizes = attributes['sizes'].each_with_object({}) do |(key, value), object|
- object[key] = Twitter::Size.new(value)
- end unless attributes['sizes'].nil?
- super(attributes)
- end
-
def ==(other)
super || (other.class == self.class && other.id == self.id)
end
+ def sizes
+ @sizes ||= Array(@attributes['sizes']).each_with_object({}) do |(key, value), object|
+ object[key] = Twitter::Size.new(value)
+ end
+ end
+
end
end
View
26 lib/twitter/place.rb
@@ -3,22 +3,28 @@
module Twitter
class Place < Twitter::Base
- attr_reader :bounding_box, :country_code, :parent_id, :place_type
lazy_attr_reader :attributes, :country, :full_name, :id, :name, :url,
:woeid
- def initialize(attributes={})
- attributes = attributes.dup
- @bounding_box = Twitter::GeoFactory.new(attributes['bounding_box']) unless attributes['bounding_box'].nil?
- @country_code = attributes['country_code'] || attributes['countryCode']
- @parent_id = attributes['parentid']
- @place_type = attributes['place_type'] || attributes['placeType'] && attributes['placeType']['name']
- super(attributes)
- end
-
def ==(other)
super || (other.class == self.class && other.id == self.id)
end
+ def bounding_box
+ @bounding_box ||= Twitter::GeoFactory.new(@attributes['bounding_box']) unless @attributes['bounding_box'].nil?
+ end
+
+ def country_code
+ @country_code ||= @attributes['country_code'] || @attributes['countryCode']
+ end
+
+ def parent_id
+ @parent_id ||= @attributes['parentid']
+ end
+
+ def place_type
+ @place_type ||= @attributes['place_type'] || @attributes['placeType'] && @attributes['placeType']['name']
+ end
+
end
end
View
7 lib/twitter/rate_limit_status.rb
@@ -2,13 +2,10 @@
module Twitter
class RateLimitStatus < Twitter::Base
- attr_reader :reset_time
lazy_attr_reader :hourly_limit, :remaining_hits, :reset_time_in_seconds
- def initialize(attributes={})
- attributes = attributes.dup
- @reset_time = Time.parse(attributes['reset_time']) unless attributes['reset_time'].nil?
- super(attributes)
+ def reset_time
+ @reset_time ||= Time.parse(@attributes['reset_time']) unless @attributes['reset_time'].nil?
end
end
View
12 lib/twitter/relationship.rb
@@ -3,13 +3,13 @@
module Twitter
class Relationship < Twitter::Base
- attr_reader :source, :target
- def initialize(attributes={})
- attributes = attributes.dup
- @source = Twitter::User.new(attributes.delete('source')) unless attributes['source'].nil?
- @target = Twitter::User.new(attributes.delete('target')) unless attributes['target'].nil?
- super(attributes)
+ def source
+ @source ||= Twitter::User.new(@attributes['source']) unless @attributes['source'].nil?
+ end
+
+ def target
+ @target ||= Twitter::User.new(@attributes['target']) unless @attributes['target'].nil?
end
end
View
7 lib/twitter/settings.rb
@@ -3,16 +3,13 @@
module Twitter
class Settings < Twitter::Base
- attr_reader :trend_location
lazy_attr_reader :always_use_https, :discoverable_by_email, :geo_enabled,
:language, :protected, :screen_name, :show_all_inline_media, :sleep_time,
:time_zone
alias :protected? :protected
- def initialize(attributes={})
- attributes = attributes.dup
- @trend_location = Twitter::Place.new(attributes.delete('trend_location').first) unless attributes['trend_location'].nil? || attributes['trend_location'].empty?
- super(attributes)
+ def trend_location
+ @trend_location ||= Twitter::Place.new(@attributes['trend_location'].first) unless @attributes['trend_location'].nil? || @attributes['trend_location'].empty?
end
end
View
51 lib/twitter/status.rb
@@ -10,35 +10,52 @@
module Twitter
class Status < Twitter::Base
include Twitter::Creatable
- attr_reader :geo, :hashtags, :media, :metadata, :place, :urls, :user,
- :user_mentions
lazy_attr_reader :favorited, :from_user, :from_user_id, :id,
:in_reply_to_screen_name, :in_reply_to_attributes_id, :in_reply_to_user_id,
:iso_language_code, :profile_image_url, :retweet_count, :retweeted,
:source, :text, :to_user, :to_user_id, :truncated
alias :favorited? :favorited
- alias :mentions :user_mentions
alias :retweeted? :retweeted
alias :truncated? :truncated
- def initialize(attributes={})
- attributes = attributes.dup
- @geo = Twitter::GeoFactory.new(attributes.delete('geo')) unless attributes['geo'].nil?
- @hashtags = Twitter::Extractor.extract_hashtags(attributes['text']) unless attributes['text'].nil?
- @media = attributes['entities'].delete('media').map do |media|
+ def ==(other)
+ super || (other.class == self.class && other.id == self.id)
+ end
+
+ def geo
+ @geo ||= Twitter::GeoFactory.new(@attributes['geo']) unless @attributes['geo'].nil?
+ end
+
+ def hashtags
+ @hashtags ||= Twitter::Extractor.extract_hashtags(@attributes['text']) unless @attributes['text'].nil?
+ end
+
+ def media
+ @media ||= Array(@attributes['entities']['media']).map do |media|
Twitter::MediaFactory.new(media)
- end unless attributes['entities'].nil? || attributes['entities']['media'].nil?
- @metadata = Twitter::Metadata.new(attributes.delete('metadata')) unless attributes['metadata'].nil?
- @place = Twitter::Place.new(attributes.delete('place')) unless attributes['place'].nil?
- @urls = Twitter::Extractor.extract_urls(attributes['text']) unless attributes['text'].nil?
- @user = Twitter::User.new(attributes.delete('user')) unless attributes['user'].nil?
- @user_mentions = Twitter::Extractor.extract_mentioned_screen_names(attributes['text']) unless attributes['text'].nil?
- super(attributes)
+ end unless @attributes['entities'].nil?
end
- def ==(other)
- super || (other.class == self.class && other.id == self.id)
+ def metadata
+ @metadata ||= Twitter::Metadata.new(@attributes['metadata']) unless @attributes['metadata'].nil?
+ end
+
+ def place
+ @place ||= Twitter::Place.new(@attributes['place']) unless @attributes['place'].nil?
+ end
+
+ def urls
+ @urls ||= Twitter::Extractor.extract_urls(@attributes['text']) unless @attributes['text'].nil?
end
+ def user
+ @user ||= Twitter::User.new(@attributes['user']) unless @attributes['user'].nil?
+ end
+
+ def user_mentions
+ @user_mentions ||= Twitter::Extractor.extract_mentioned_screen_names(@attributes['text']) unless @attributes['text'].nil?
+ end
+ alias :mentions :user_mentions
+
end
end
View
15 lib/twitter/suggestion.rb
@@ -3,20 +3,17 @@
module Twitter
class Suggestion < Twitter::Base
- attr_reader :users
lazy_attr_reader :name, :size, :slug
- def initialize(attributes={})
- attributes = attributes.dup
- @users = attributes.delete('users').map do |user|
- Twitter::User.new(user)
- end unless attributes['users'].nil?
- super(attributes)
- end
-
def ==(other)
super || (other.class == self.class && other.slug == self.slug)
end
+ def users
+ @users = Array(@attributes['users']).map do |user|
+ Twitter::User.new(user)
+ end
+ end
+
end
end
View
11 lib/twitter/user.rb
@@ -7,7 +7,6 @@ module Twitter
class User < Twitter::Base
include Twitter::Authenticatable
include Twitter::Creatable
- attr_reader :status
lazy_attr_reader :all_replies, :blocking, :can_dm, :connections,
:contributors_enabled, :default_profile, :default_profile_image,
:description, :favourites_count, :follow_request_sent, :followed_by,
@@ -50,15 +49,13 @@ class User < Twitter::Base
alias :verified? :verified
alias :want_retweets? :want_retweets
- def initialize(attributes={})
- attributes = attributes.dup
- @status = Twitter::Status.new(attributes.delete('status')) unless attributes['status'].nil?
- super(attributes)
- end
-
def ==(other)
super || (other.class == self.class && other.id == self.id)
end
+ def status
+ @status ||= Twitter::Status.new(@attributes['status']) unless @attributes['status'].nil?
+ end
+
end
end
View
2  spec/twitter/configuration_spec.rb
@@ -10,7 +10,7 @@
end
it "should return nil when photo_sizes is not set" do
photo_sizes = Twitter::Configuration.new.photo_sizes
- photo_sizes.should be_nil
+ photo_sizes.should be_empty
end
end
View
2  spec/twitter/photo_spec.rb
@@ -28,7 +28,7 @@
end
it "should return nil when sizes is not set" do
sizes = Twitter::Photo.new.sizes
- sizes.should be_nil
+ sizes.should be_empty
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.