Skip to content

Commit

Permalink
Use symbols as keys
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jun 24, 2012
1 parent 4c63a73 commit d5b5d87
Show file tree
Hide file tree
Showing 68 changed files with 307 additions and 304 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ wiki][apps]!
[apps]: https://github.com/jnunemaker/twitter/wiki/apps

## What's new in version 3?
### Hashes
All returned hashes now use symbols as keys instead of strings.

### Methods
The following methods now accept multiple users or ids as arguments and return
arrays:
Expand All @@ -57,18 +60,18 @@ The `Twitter::Client#direct_messages` method has been renamed to

The `Twitter::Client#profile_image` method has been removed.

Additionally, the `Twitter::Client#follow` method now checks to make sure the
user isn't already being followed. If you don't wish to perform that check
(which does require an extra HTTP request), you can use the new
`Twitter::Client#follow!` method instead. **Note**: This may re-send an email
notification to the user, even if they are already being followed.

The `Twitter::Client#search` now returns a `Twitter::SearchResult` object,
which contains metadata and a results array. In the previous major version,
this method returned an array of `Twitter::Status` objects, which is now
accessible by sending the `results` message to a `Twitter::SearchResults`
object.

Additionally, the `Twitter::Client#follow` method now checks to make sure the
user isn't already being followed. If you don't wish to perform that check
(which does require an extra HTTP request), you can use the new
`Twitter::Client#follow!` method instead. **Note**: This may re-send an email
notification to the user, even if they are already being followed.

##### Version 2
Twitter::Client.search("query").map(&:full_text)

Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/action/favorite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Favorite < Twitter::Action::Status
#
# @return [Array<Twitter::Status>]
def targets
@targets = Array(@attrs['targets']).map do |status|
@targets = Array(@attrs[:targets]).map do |status|
Twitter::Status.fetch_or_new(status)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/action/follow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Follow < Twitter::Base
#
# @return [Array<Twitter::User>]
def sources
@sources = Array(@attrs['sources']).map do |user|
@sources = Array(@attrs[:sources]).map do |user|
Twitter::User.fetch_or_new(user)
end
end
Expand All @@ -21,7 +21,7 @@ def sources
#
# @return [Array<Twitter::User>]
def targets
@targets = Array(@attrs['targets']).map do |user|
@targets = Array(@attrs[:targets]).map do |user|
Twitter::User.fetch_or_new(user)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/twitter/action/list_member_added.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ListMemberAdded < Twitter::Base
#
# @return [Array<Twitter::User>]
def sources
@sources = Array(@attrs['sources']).map do |user|
@sources = Array(@attrs[:sources]).map do |user|
Twitter::User.fetch_or_new(user)
end
end
Expand All @@ -22,7 +22,7 @@ def sources
#
# @return [Array<Twitter::List>]
def target_objects
@target_objects = Array(@attrs['target_objects']).map do |list|
@target_objects = Array(@attrs[:target_objects]).map do |list|
Twitter::List.fetch_or_new(list)
end
end
Expand All @@ -31,7 +31,7 @@ def target_objects
#
# @return [Array<Twitter::User>]
def targets
@targets = Array(@attrs['targets']).map do |user|
@targets = Array(@attrs[:targets]).map do |user|
Twitter::User.fetch_or_new(user)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/twitter/action/mention.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Mention < Twitter::Base
#
# @return [Array<Twitter::User>]
def sources
@sources = Array(@attrs['sources']).map do |user|
@sources = Array(@attrs[:sources]).map do |user|
Twitter::User.fetch_or_new(user)
end
end
Expand All @@ -29,7 +29,7 @@ def source
#
# @return [Array<Twitter::Status>]
def target_objects
@target_objects = Array(@attrs['target_objects']).map do |status|
@target_objects = Array(@attrs[:target_objects]).map do |status|
Twitter::Status.fetch_or_new(status)
end
end
Expand All @@ -38,7 +38,7 @@ def target_objects
#
# @return [Array<Twitter::User>]
def targets
@targets = Array(@attrs['targets']).map do |user|
@targets = Array(@attrs[:targets]).map do |user|
Twitter::User.fetch_or_new(user)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/action/reply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Reply < Twitter::Action::Status
#
# @return [Array<Twitter::Status>]
def target_objects
@target_objects = Array(@attrs['target_objects']).map do |status|
@target_objects = Array(@attrs[:target_objects]).map do |status|
Twitter::Status.fetch_or_new(status)
end
end
Expand All @@ -17,7 +17,7 @@ def target_objects
#
# @return [Array<Twitter::Status>]
def targets
@targets = Array(@attrs['targets']).map do |status|
@targets = Array(@attrs[:targets]).map do |status|
Twitter::Status.fetch_or_new(status)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/action/retweet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Retweet < Twitter::Action::Status
#
# @return [Array<Twitter::Status>]
def target_objects
@target_objects = Array(@attrs['target_objects']).map do |status|
@target_objects = Array(@attrs[:target_objects]).map do |status|
Twitter::Status.fetch_or_new(status)
end
end
Expand All @@ -17,7 +17,7 @@ def target_objects
#
# @return [Array<Twitter::User>]
def targets
@targets = Array(@attrs['targets']).map do |user|
@targets = Array(@attrs[:targets]).map do |user|
Twitter::User.fetch_or_new(user)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/action/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Status < Twitter::Base

# @return [Array<Twitter::User>]
def sources
@sources = Array(@attrs['sources']).map do |user|
@sources = Array(@attrs[:sources]).map do |user|
Twitter::User.fetch_or_new(user)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/twitter/action_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class ActionFactory
# Instantiates a new action object
#
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing an 'action' key.
# @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.new(attrs={})
if type = attrs.delete('action')
if type = attrs.delete(:action)
Twitter::Action.const_get(camelize(type).to_sym).fetch_or_new(attrs)
else
raise ArgumentError, "argument must have an 'action' key"
raise ArgumentError, "argument must have an :action key"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.attr_reader(*attrs)
attrs.each do |attribute|
class_eval do
define_method attribute do
@attrs[attribute.to_s]
@attrs[attribute.to_sym]
end
end
end
Expand Down
32 changes: 16 additions & 16 deletions lib/twitter/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def update_profile(options={})
# @option options [Boolean] :tile Whether or not to tile the background image. If set to true the background image will be displayed tiled. The image will not be tiled otherwise.
# @example Update the authenticating user's profile background image
# Twitter.update_profile_background_image(File.new("we_concept_bg2.png"))
# Twitter.update_profile_background_image('io' => StringIO.new(pic), 'type' => 'jpg')
# Twitter.update_profile_background_image(:io => StringIO.new(pic), :type => 'jpg')
def update_profile_background_image(image, options={})
response = post("/1/account/update_profile_background_image.json", options.merge(:image => image))
Twitter::User.from_response(response)
Expand Down Expand Up @@ -182,7 +182,7 @@ def update_profile_colors(options={})
# @param options [Hash] A customizable set of options.
# @example Update the authenticating user's profile image
# Twitter.update_profile_image(File.new("me.jpeg"))
# Twitter.update_profile_image('io' => StringIO.new(pic), 'type' => 'jpg')
# Twitter.update_profile_image(:io => StringIO.new(pic), :type => 'jpg')
def update_profile_image(image, options={})
response = post("/1/account/update_profile_image.json", options.merge(:image => image))
Twitter::User.from_response(response)
Expand Down Expand Up @@ -938,7 +938,7 @@ def languages(options={})
# @example Return {https://twitter.com/privacy Twitter's Privacy Policy}
# Twitter.privacy
def privacy(options={})
get("/1/legal/privacy.json", options)[:body]['privacy']
get("/1/legal/privacy.json", options)[:body][:privacy]
end

# Returns {https://twitter.com/tos Twitter's Terms of Service}
Expand All @@ -950,7 +950,7 @@ def privacy(options={})
# @example Return {https://twitter.com/tos Twitter's Terms of Service}
# Twitter.tos
def tos(options={})
get("/1/legal/tos.json", options)[:body]['tos']
get("/1/legal/tos.json", options)[:body][:tos]
end

# Returns all lists the authenticating or specified user subscribes to, including their own
Expand Down Expand Up @@ -1598,7 +1598,7 @@ def list(*args)
# Twitter.local_trends(2487956)
def local_trends(woeid=1, options={})
response = get("/1/trends/#{woeid}.json", options)
response[:body].first['trends'].map do |trend|
response[:body].first[:trends].map do |trend|
Twitter::Trend.fetch_or_new(trend)
end
end
Expand Down Expand Up @@ -1685,7 +1685,7 @@ def disable_notifications(*args)
# Twitter.places_nearby(:ip => "74.125.19.104")
def places_nearby(options={})
response = get("/1/geo/search.json", options)
response[:body]['result']['places'].map do |place|
response[:body][:result][:places].map do |place|
Twitter::Place.fetch_or_new(place)
end
end
Expand All @@ -1708,7 +1708,7 @@ def places_nearby(options={})
# Twitter.places_similar(:lat => "37.7821120598956", :long => "-122.400612831116", :name => "Twitter HQ")
def places_similar(options={})
response = get("/1/geo/similar_places.json", options)
response[:body]['result']['places'].map do |place|
response[:body][:result][:places].map do |place|
Twitter::Place.fetch_or_new(place)
end
end
Expand All @@ -1730,7 +1730,7 @@ def places_similar(options={})
# Twitter.reverse_geocode(:lat => "37.7821120598956", :long => "-122.400612831116")
def reverse_geocode(options={})
response = get("/1/geo/reverse_geocode.json", options)
response[:body]['result']['places'].map do |place|
response[:body][:result][:places].map do |place|
Twitter::Place.fetch_or_new(place)
end
end
Expand Down Expand Up @@ -1907,7 +1907,7 @@ def search(q, options={})
# Twitter.phoenix_search('twitter')
def phoenix_search(q, options={})
response = get("/phoenix_search.phoenix", options.merge(:q => q))
response[:body]['statuses'].map do |status|
response[:body][:statuses].map do |status|
Twitter::Status.fetch_or_new(status)
end
end
Expand Down Expand Up @@ -2226,7 +2226,7 @@ def network_timeline(options={})
def trends_daily(date=Date.today, options={})
response = get("/1/trends/daily.json", options.merge(:date => date.strftime('%Y-%m-%d')))
trends = {}
response[:body]['trends'].each do |key, value|
response[:body][:trends].each do |key, value|
trends[key] = []
value.each do |trend|
trends[key] << Twitter::Trend.fetch_or_new(trend)
Expand All @@ -2249,7 +2249,7 @@ def trends_daily(date=Date.today, options={})
def trends_weekly(date=Date.today, options={})
response = get("/1/trends/weekly.json", options.merge(:date => date.strftime('%Y-%m-%d')))
trends = {}
response[:body]['trends'].each do |key, value|
response[:body][:trends].each do |key, value|
trends[key] = []
value.each do |trend|
trends[key] << Twitter::Trend.fetch_or_new(trend)
Expand Down Expand Up @@ -2358,7 +2358,7 @@ def statuses(*args)
# Twitter.status_activity(25938088801)
def status_activity(id, options={})
response = get("/i/statuses/#{id}/activity/summary.json", options)
response[:body].merge!('id' => id) if response[:body]
response[:body].merge!(:id => id) if response[:body]
Twitter::Status.from_response(response)
end

Expand Down Expand Up @@ -2477,8 +2477,8 @@ def retweet(*args)
args.flatten.threaded_map do |id|
response = post("/1/statuses/retweet/#{id}.json", options)
retweeted_status = response.dup
retweeted_status[:body] = response[:body].delete('retweeted_status')
retweeted_status[:body]['retweeted_status'] = response[:body]
retweeted_status[:body] = response[:body].delete(:retweeted_status)
retweeted_status[:body][:retweeted_status] = response[:body]
Twitter::Status.from_response(retweeted_status)
end
end
Expand Down Expand Up @@ -2525,7 +2525,7 @@ def update(status, options={})
# @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.
# @example Update the authenticating user's status
# Twitter.update_with_media("I'm tweeting with @gem!", File.new('my_awesome_pic.jpeg'))
# Twitter.update_with_media("I'm tweeting with @gem!", {'io' => StringIO.new(pic), 'type' => 'jpg'})
# Twitter.update_with_media("I'm tweeting with @gem!", {:io => StringIO.new(pic), :type => 'jpg'})
def update_with_media(status, media, options={})
response = post("/1/statuses/update_with_media.json", options.merge('media[]' => media, 'status' => status), :endpoint => media_endpoint)
Twitter::Status.from_response(response)
Expand Down Expand Up @@ -2713,7 +2713,7 @@ def recommendations(*args)
response = get("/1/users/recommendations.json", options)
Twitter::RateLimit.instance.update(response[:response_headers])
response[:body].map do |recommendation|
Twitter::User.fetch_or_new(recommendation['user'])
Twitter::User.fetch_or_new(recommendation[:user])
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Configuration < Twitter::Base
#
# @return [Array<Twitter::Size>]
def photo_sizes
@photo_sizes ||= Array(@attrs['photo_sizes']).inject({}) do |object, (key, value)|
@photo_sizes ||= Array(@attrs[:photo_sizes]).inject({}) do |object, (key, value)|
object[key] = Twitter::Size.fetch_or_new(value)
object
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/creatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Creatable
#
# @return [Time]
def created_at
@created_at ||= Time.parse(@attrs['created_at']) unless @attrs['created_at'].nil?
@created_at ||= Time.parse(@attrs[:created_at]) unless @attrs[:created_at].nil?
end

end
Expand Down
10 changes: 5 additions & 5 deletions lib/twitter/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Cursor
# @params method [String, Symbol] The name of the method to return the collection
# @params klass [Class] The class to instantiate object in the collection
# @return [Twitter::Cursor]
def self.from_response(response={}, method='ids', klass=nil)
def self.from_response(response={}, method=:ids, klass=nil)
self.new(response[:body], response[:response_headers], method, klass)
end

Expand All @@ -22,10 +22,10 @@ def self.from_response(response={}, method='ids', klass=nil)
# @param attrs [Hash]
# @param response_headers [Hash]
# @return [Twitter::Base]
def initialize(attrs={}, response_headers={}, method='ids', klass=nil)
def initialize(attrs={}, response_headers={}, method=:ids, klass=nil)
self.update(attrs)
self.update_rate_limit(response_headers) unless response_headers.empty?
@collection = Array(attrs[method.to_s]).map do |item|
@collection = Array(attrs[method.to_sym]).map do |item|
if klass
klass.fetch_or_new(item)
else
Expand All @@ -38,12 +38,12 @@ def initialize(attrs={}, response_headers={}, method='ids', klass=nil)
end

def next_cursor
@attrs['next_cursor']
@attrs[:next_cursor]
end
alias next next_cursor

def previous_cursor
@attrs['previous_cursor']
@attrs[:previous_cursor]
end
alias previous previous_cursor

Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/direct_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class DirectMessage < Twitter::Identifiable

# @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]) unless @attrs[:recipient].nil?
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]) unless @attrs[:sender].nil?
end

end
Expand Down
Loading

0 comments on commit d5b5d87

Please sign in to comment.