Permalink
Browse files

Use symbols as keys

  • Loading branch information...
sferik committed Jun 24, 2012
1 parent 4c63a73 commit d5b5d8788dc0c0cef6f2c28e6fa2dc6ffcf389eb
Showing with 307 additions and 304 deletions.
  1. +9 −6 README.md
  2. +1 −1 lib/twitter/action/favorite.rb
  3. +2 −2 lib/twitter/action/follow.rb
  4. +3 −3 lib/twitter/action/list_member_added.rb
  5. +3 −3 lib/twitter/action/mention.rb
  6. +2 −2 lib/twitter/action/reply.rb
  7. +2 −2 lib/twitter/action/retweet.rb
  8. +1 −1 lib/twitter/action/status.rb
  9. +3 −3 lib/twitter/action_factory.rb
  10. +1 −1 lib/twitter/base.rb
  11. +16 −16 lib/twitter/client.rb
  12. +1 −1 lib/twitter/configuration.rb
  13. +1 −1 lib/twitter/creatable.rb
  14. +5 −5 lib/twitter/cursor.rb
  15. +2 −2 lib/twitter/direct_message.rb
  16. +5 −5 lib/twitter/error/client_error.rb
  17. +3 −3 lib/twitter/geo_factory.rb
  18. +4 −4 lib/twitter/identifiable.rb
  19. +1 −1 lib/twitter/list.rb
  20. +3 −3 lib/twitter/media_factory.rb
  21. +1 −1 lib/twitter/photo.rb
  22. +4 −4 lib/twitter/place.rb
  23. +1 −1 lib/twitter/rate_limit_status.rb
  24. +3 −3 lib/twitter/relationship.rb
  25. +2 −2 lib/twitter/request/multipart_with_file.rb
  26. +2 −2 lib/twitter/requestable.rb
  27. +2 −2 lib/twitter/response/parse_json.rb
  28. +1 −1 lib/twitter/search_results.rb
  29. +1 −1 lib/twitter/settings.rb
  30. +18 −18 lib/twitter/status.rb
  31. +1 −1 lib/twitter/suggestion.rb
  32. +1 −1 lib/twitter/user.rb
  33. +2 −2 spec/twitter/action/favorite_spec.rb
  34. +2 −2 spec/twitter/action/follow_spec.rb
  35. +3 −3 spec/twitter/action/list_member_added_spec.rb
  36. +4 −4 spec/twitter/action/mention_spec.rb
  37. +3 −3 spec/twitter/action/reply_spec.rb
  38. +3 −3 spec/twitter/action/retweet_spec.rb
  39. +7 −7 spec/twitter/action_factory_spec.rb
  40. +1 −1 spec/twitter/action_spec.rb
  41. +3 −3 spec/twitter/base_spec.rb
  42. +1 −1 spec/twitter/client/accounts_spec.rb
  43. +8 −8 spec/twitter/client/friends_and_followers_spec.rb
  44. +12 −12 spec/twitter/client/lists_spec.rb
  45. +6 −6 spec/twitter/client/trends_spec.rb
  46. +2 −2 spec/twitter/client/tweets_spec.rb
  47. +8 −8 spec/twitter/client/users_spec.rb
  48. +2 −2 spec/twitter/configuration_spec.rb
  49. +4 −4 spec/twitter/cursor_spec.rb
  50. +9 −9 spec/twitter/direct_message_spec.rb
  51. +3 −3 spec/twitter/geo_factory_spec.rb
  52. +8 −8 spec/twitter/list_spec.rb
  53. +2 −2 spec/twitter/media_factory_spec.rb
  54. +13 −13 spec/twitter/oembed_spec.rb
  55. +8 −8 spec/twitter/photo_spec.rb
  56. +12 −12 spec/twitter/place_spec.rb
  57. +3 −3 spec/twitter/point_spec.rb
  58. +3 −3 spec/twitter/polygon_spec.rb
  59. +1 −1 spec/twitter/rate_limit_status_spec.rb
  60. +2 −2 spec/twitter/relationship_spec.rb
  61. +7 −7 spec/twitter/saved_search_spec.rb
  62. +1 −1 spec/twitter/search_results_spec.rb
  63. +1 −1 spec/twitter/settings_spec.rb
  64. +4 −4 spec/twitter/size_spec.rb
  65. +39 −39 spec/twitter/status_spec.rb
  66. +3 −3 spec/twitter/suggestion_spec.rb
  67. +3 −3 spec/twitter/trend_spec.rb
  68. +9 −9 spec/twitter/user_spec.rb
View
@@ -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:
@@ -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)
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
View
@@ -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
View
@@ -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)
@@ -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)
@@ -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}
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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)
@@ -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)
@@ -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
@@ -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
@@ -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)
@@ -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
@@ -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
View
@@ -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
View
@@ -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
@@ -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
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit d5b5d87

Please sign in to comment.