Permalink
Browse files

Assign instance variables dynamically, in the base class

  • Loading branch information...
1 parent 7353661 commit b74770ef19524a925012c1d2ae7181e47fd49205 @sferik committed Oct 18, 2011
View
9 lib/twitter/configuration.rb
@@ -9,15 +9,10 @@ class Configuration < Twitter::Base
:short_url_length_https
def initialize(configuration={})
- @characters_reserved_per_media = configuration['characters_reserved_per_media']
- @max_media_per_upload = configuration['max_media_per_upload']
- @non_username_paths = configuration['non_username_paths']
- @photo_size_limit = configuration['photo_size_limit']
- @photo_sizes = configuration['photo_sizes'].each_with_object({}) do |(key, value), object|
+ @photo_sizes = configuration.delete('photo_sizes').each_with_object({}) do |(key, value), object|
object[key] = Twitter::Size.new(value)
end unless configuration['photo_sizes'].nil?
- @short_url_length = configuration['short_url_length']
- @short_url_length_https = configuration['short_url_length_https']
+ super(configuration)
end
end
View
9 lib/twitter/creatable.rb
@@ -2,12 +2,11 @@
module Twitter
module Creatable
+ attr_reader :created_at
- # Time when the user was created
- #
- # @return [Time]
- def created_at
- @created_at = Time.parse(@created_at) unless @created_at.nil? || @created_at.is_a?(Time)
+ def initialize(hash={})
+ @created_at = Time.parse(hash.delete('created_at')) unless hash['created_at'].nil?
+ super(hash)
end
end
View
5 lib/twitter/cursor.rb
@@ -8,18 +8,17 @@ class Cursor < Twitter::Base
alias :previous :previous_cursor
def initialize(cursor, method, klass=nil)
- @collection = cursor[method.to_s].map do |item|
+ @collection = cursor.delete(method.to_s).map do |item|
if klass
klass.new(item)
else
item
end
end unless cursor[method.to_s].nil?
- @next_cursor = cursor['next_cursor']
- @previous_cursor = cursor['previous_cursor']
singleton_class.class_eval do
alias_method method.to_sym, :collection
end
+ super(cursor)
end
def first?
View
8 lib/twitter/direct_message.rb
@@ -8,11 +8,9 @@ class DirectMessage < Twitter::Base
attr_reader :id, :recipient, :sender, :text
def initialize(direct_message={})
- @created_at = direct_message['created_at']
- @id = direct_message['id']
- @recipient = Twitter::User.new(direct_message['recipient']) unless direct_message['recipient'].nil?
- @sender = Twitter::User.new(direct_message['sender']) unless direct_message['sender'].nil?
- @text = direct_message['text']
+ @recipient = Twitter::User.new(direct_message.delete('recipient')) unless direct_message['recipient'].nil?
+ @sender = Twitter::User.new(direct_message.delete('sender')) unless direct_message['sender'].nil?
+ super(direct_message)
end
def ==(other)
View
13 lib/twitter/list.rb
@@ -8,17 +8,8 @@ class List < Twitter::Base
alias :following? :following
def initialize(list={})
- @description = list['description']
- @following = list['following']
- @full_name = list['full_name']
- @id = list['id']
- @member_count = list['member_count']
- @mode = list['mode']
- @name = list['name']
- @slug = list['slug']
- @subscriber_count = list['subscriber_count']
- @uri = list['uri']
- @user = Twitter::User.new(list['user']) unless list['user'].nil?
+ @user = Twitter::User.new(list.delete('user')) unless list['user'].nil?
+ super(list)
end
def ==(other)
View
10 lib/twitter/photo.rb
@@ -8,16 +8,10 @@ class Photo < Twitter::Base
:media_url_https, :sizes, :url
def initialize(photo={})
- @display_url = photo['display_url']
- @expanded_url = photo['expanded_url']
- @id = photo['id']
- @indices = photo['indices']
- @media_url = photo['media_url']
- @media_url_https = photo['media_url_https']
- @sizes = photo['sizes'].each_with_object({}) do |(key, value), object|
+ @sizes = photo.delete('sizes').each_with_object({}) do |(key, value), object|
object[key] = Twitter::Size.new(value)
end unless photo['sizes'].nil?
- @url = photo['url']
+ super(photo)
end
def ==(other)
View
15 lib/twitter/place.rb
@@ -7,17 +7,10 @@ class Place < Twitter::Base
:full_name, :id, :name, :place_type, :parent_id, :url, :woeid
def initialize(place={})
- @attributes = place['attributes']
- @bounding_box = Twitter::GeoFactory.new(place['bounding_box']) unless place['bounding_box'].nil?
- @country = place['country']
- @country_code = place['country_code'] || place['countryCode']
- @full_name = place['full_name']
- @id = place['id']
- @name = place['name']
- @parent_id = place['parentid']
- @place_type = place['place_type'] || place['placeType'] && place['placeType']['name']
- @url = place['url']
- @woeid = place['woeid']
+ @bounding_box = Twitter::GeoFactory.new(place.delete('bounding_box')) unless place['bounding_box'].nil?
+ @country_code = place.delete('country_code') || place.delete('countryCode')
+ @place_type = place.delete('place_type') || place['placeType'] && place['placeType'].delete('name')
+ super(place)
end
def ==(other)
View
10 lib/twitter/rate_limit_status.rb
@@ -2,13 +2,11 @@
module Twitter
class RateLimitStatus < Twitter::Base
- attr_reader :hourly_limit, :remaining_hits, :reset_time_in_seconds
+ attr_reader :hourly_limit, :remaining_hits, :reset_time, :reset_time_in_seconds
- # Time when the rate limit resets
- #
- # @return [Time]
- def reset_time
- @reset_time = Time.parse(@reset_time) unless @reset_time.nil? || @reset_time.is_a?(Time)
+ def initialize(rate_limit_status={})
+ @reset_time = Time.parse(rate_limit_status.delete('reset_time')) unless rate_limit_status['reset_time'].nil?
+ super(rate_limit_status)
end
end
View
5 lib/twitter/relationship.rb
@@ -6,8 +6,9 @@ class Relationship < Twitter::Base
attr_reader :source, :target
def initialize(relationship={})
- @source = Twitter::User.new(relationship['source']) unless relationship['source'].nil?
- @target = Twitter::User.new(relationship['target']) unless relationship['target'].nil?
+ @source = Twitter::User.new(relationship.delete('source')) unless relationship['source'].nil?
+ @target = Twitter::User.new(relationship.delete('target')) unless relationship['target'].nil?
+ super(relationship)
end
end
View
12 lib/twitter/settings.rb
@@ -9,16 +9,8 @@ class Settings < Twitter::Base
alias :protected? :protected
def initialize(settings={})
- @always_use_https = settings['always_use_https']
- @discoverable_by_email = settings['discoverable_by_email']
- @geo_enabled = settings['geo_enabled']
- @language = settings['language']
- @protected = settings['protected']
- @screen_name = settings['screen_name']
- @show_all_inline_media = settings['show_all_inline_media']
- @sleep_time = settings['sleep_time']
- @time_zone = settings['time_zone']
- @trend_location = Twitter::Place.new(settings['trend_location'].first) unless settings['trend_location'].nil? || settings['trend_location'].empty?
+ @trend_location = Twitter::Place.new(settings.delete('trend_location').first) unless settings['trend_location'].nil? || settings['trend_location'].empty?
+ super(settings)
end
end
View
30 lib/twitter/status.rb
@@ -21,33 +21,17 @@ class Status < Twitter::Base
alias :truncated? :truncated
def initialize(status={})
- @created_at = status['created_at']
- @favorited = status['favorited']
- @from_user = status['from_user']
- @from_user_id = status['from_user_id']
- @geo = Twitter::GeoFactory.new(status['geo']) unless status['geo'].nil?
+ @geo = Twitter::GeoFactory.new(status.delete('geo')) unless status['geo'].nil?
@hashtags = Twitter::Extractor.extract_hashtags(status['text']) unless status['text'].nil?
- @id = status['id']
- @in_reply_to_screen_name = status['in_reply_to_screen_name']
- @in_reply_to_status_id = status['in_reply_to_status_id']
- @in_reply_to_user_id = status['in_reply_to_user_id']
- @iso_language_code = status['iso_language_code']
- @media = status['entities']['media'].map do |media|
- Twitter::MediaFactory.new(media)
+ @media = status['entities'].delete('media').map do |medium|
+ Twitter::MediaFactory.new(medium)
end unless status['entities'].nil? || status['entities']['media'].nil?
- @metadata = Twitter::Metadata.new(status['metadata']) unless status['metadata'].nil?
- @place = Twitter::Place.new(status['place']) unless status['place'].nil?
- @profile_image_url = status['profile_image_url']
- @retweet_count = status['retweet_count']
- @retweeted = status['retweeted']
- @source = status['source']
- @text = status['text']
- @to_user = status['to_user']
- @to_user_id = status['to_user_id']
- @truncated = status['truncated']
+ @metadata = Twitter::Metadata.new(status.delete('metadata')) unless status['metadata'].nil?
+ @place = Twitter::Place.new(status.delete('place')) unless status['place'].nil?
@urls = Twitter::Extractor.extract_urls(status['text']) unless status['text'].nil?
- @user = Twitter::User.new(status['user'].merge('status' => self.to_hash.delete('user'))) unless status['user'].nil?
+ @user = Twitter::User.new(status.delete('user').merge('status' => self.to_hash.delete('user'))) unless status['user'].nil?
@user_mentions = Twitter::Extractor.extract_mentioned_screen_names(status['text']) unless status['text'].nil?
+ super(status)
end
def ==(other)
View
6 lib/twitter/suggestion.rb
@@ -6,12 +6,10 @@ class Suggestion < Twitter::Base
attr_reader :name, :size, :slug, :users
def initialize(suggestion={})
- @name = suggestion['name']
- @size = suggestion['size']
- @slug = suggestion['slug']
- @users = suggestion['users'].map do |user|
+ @users = suggestion.delete('users').map do |user|
Twitter::User.new(user)
end unless suggestion['users'].nil?
+ super(suggestion)
end
def ==(other)
View
47 lib/twitter/user.rb
@@ -50,51 +50,8 @@ class User < Twitter::Base
alias :want_retweets? :want_retweets
def initialize(user={})
- @all_replies = user['all_replies']
- @blocking = user['blocking']
- @can_dm = user['can_dm']
- @connections = user['connections']
- @contributors_enabled = user['contributors_enabled']
- @created_at = user['created_at']
- @default_profile = user['default_profile']
- @default_profile_image = user['default_profile_image']
- @description = user['description']
- @favourites_count = user['favourites_count']
- @follow_request_sent = user['follow_request_sent']
- @followed_by = user['followed_by']
- @followers_count = user['followers_count']
- @following = user['following']
- @friends_count = user['friends_count']
- @geo_enabled = user['geo_enabled']
- @id = user['id']
- @is_translator = user['is_translator']
- @lang = user['lang']
- @listed_count = user['listed_count']
- @location = user['location']
- @marked_spam = user['marked_spam']
- @name = user['name']
- @notifications_enabled = user['notifications_enabled']
- @notifications = user['notifications']
- @profile_background_color = user['profile_background_color']
- @profile_background_image_url = user['profile_background_image_url']
- @profile_background_image_url_https = user['profile_background_image_url_https']
- @profile_background_tile = user['profile_background_tile']
- @profile_image_url = user['profile_image_url']
- @profile_image_url_https = user['profile_image_url_https']
- @profile_link_color = user['profile_link_color']
- @profile_sidebar_border_color = user['profile_sidebar_border_color']
- @profile_sidebar_fill_color = user['profile_sidebar_fill_color']
- @profile_text_color = user['profile_text_color']
- @profile_use_background_image = user['profile_use_background_image']
- @protected = user['protected']
- @screen_name = user['screen_name']
- @status = Twitter::Status.new(user['status'].merge('user' => self.to_hash.delete('status'))) unless user['status'].nil?
- @statuses_count = user['statuses_count']
- @time_zone = user['time_zone']
- @url = user['url']
- @utc_offset = user['utc_offset']
- @verified = user['verified']
- @want_retweets = user['want_retweets']
+ @status = Twitter::Status.new(user.delete('status').merge('user' => self.to_hash.delete('status'))) unless user['status'].nil?
+ super(user)
end
def ==(other)

5 comments on commit b74770e

@dkubb

@sferik you asked for feedback on this commit via twitter, so ...

One thing I would caution against is mutating the arguments to a method, including a constructor. I've seen tons of subtle bugs pop by using Hash#delete on Hash arguments because you're modifying the object in the caller's scope, an object which may be used elsewhere.

@phiggins

Why not store the hash and delegate a bunch of the methods calls to it?

@sferik
Owner

Okay, here's my implementation of your suggestions: 6fb7b3f.

Seems to be faster!

The only disadvantage is I couldn't figure out how to implement Twitter::Base#to_hash with lazy attributes.

@laserlemon
Collaborator

What's the advantage of lazy attributes when they're just fetched from a hash? Is there a big savings there?

And wouldn't Twitter::Base#to_hash just return @attributes?

@sferik
Owner

@laserlemon, no discernible advantage. I removed the instance variables in 6732706 and restored Twitter::Base#to_hash in 1de9147.

Please sign in to comment.