twitter 4.8.1 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (215) hide show
  1. data/CHANGELOG.md +36 -0
  2. data/CONTRIBUTING.md +13 -15
  3. data/LICENSE.md +1 -1
  4. data/README.md +401 -261
  5. data/Rakefile +6 -0
  6. data/lib/twitter/arguments.rb +14 -0
  7. data/lib/twitter/base.rb +109 -89
  8. data/lib/twitter/client.rb +38 -115
  9. data/lib/twitter/configuration.rb +5 -2
  10. data/lib/twitter/core_ext/kernel.rb +5 -1
  11. data/lib/twitter/creatable.rb +7 -4
  12. data/lib/twitter/cursor.rb +57 -45
  13. data/lib/twitter/direct_message.rb +2 -11
  14. data/lib/twitter/entity/uri.rb +14 -0
  15. data/lib/twitter/enumerable.rb +15 -0
  16. data/lib/twitter/error/already_favorited.rb +1 -1
  17. data/lib/twitter/error/already_posted.rb +10 -0
  18. data/lib/twitter/error/already_retweeted.rb +1 -1
  19. data/lib/twitter/error/bad_gateway.rb +2 -3
  20. data/lib/twitter/error/bad_request.rb +2 -2
  21. data/lib/twitter/error/forbidden.rb +2 -2
  22. data/lib/twitter/error/gateway_timeout.rb +2 -3
  23. data/lib/twitter/error/internal_server_error.rb +2 -3
  24. data/lib/twitter/error/not_acceptable.rb +2 -2
  25. data/lib/twitter/error/not_found.rb +2 -2
  26. data/lib/twitter/error/service_unavailable.rb +2 -3
  27. data/lib/twitter/error/too_many_requests.rb +2 -2
  28. data/lib/twitter/error/unauthorized.rb +2 -2
  29. data/lib/twitter/error/unprocessable_entity.rb +2 -2
  30. data/lib/twitter/error.rb +65 -14
  31. data/lib/twitter/factory.rb +13 -12
  32. data/lib/twitter/geo.rb +2 -7
  33. data/lib/twitter/geo_factory.rb +11 -7
  34. data/lib/twitter/geo_results.rb +40 -0
  35. data/lib/twitter/identity.rb +4 -34
  36. data/lib/twitter/list.rb +21 -4
  37. data/lib/twitter/media/photo.rb +6 -4
  38. data/lib/twitter/media_factory.rb +11 -7
  39. data/lib/twitter/null_object.rb +25 -0
  40. data/lib/twitter/oembed.rb +3 -2
  41. data/lib/twitter/place.rb +15 -15
  42. data/lib/twitter/profile_banner.rb +3 -2
  43. data/lib/twitter/rate_limit.rb +4 -17
  44. data/lib/twitter/relationship.rb +2 -19
  45. data/lib/twitter/rest/api/direct_messages.rb +138 -0
  46. data/lib/twitter/rest/api/favorites.rb +115 -0
  47. data/lib/twitter/rest/api/friends_and_followers.rb +287 -0
  48. data/lib/twitter/rest/api/help.rb +58 -0
  49. data/lib/twitter/rest/api/lists.rb +498 -0
  50. data/lib/twitter/rest/api/oauth.rb +62 -0
  51. data/lib/twitter/rest/api/places_and_geo.rb +86 -0
  52. data/lib/twitter/rest/api/saved_searches.rb +93 -0
  53. data/lib/twitter/rest/api/search.rb +37 -0
  54. data/lib/twitter/rest/api/spam_reporting.rb +29 -0
  55. data/lib/twitter/rest/api/suggested_users.rb +51 -0
  56. data/lib/twitter/rest/api/timelines.rb +202 -0
  57. data/lib/twitter/rest/api/trends.rb +58 -0
  58. data/lib/twitter/rest/api/tweets.rb +291 -0
  59. data/lib/twitter/rest/api/undocumented.rb +52 -0
  60. data/lib/twitter/rest/api/users.rb +383 -0
  61. data/lib/twitter/rest/api/utils.rb +246 -0
  62. data/lib/twitter/rest/client.rb +175 -0
  63. data/lib/twitter/rest/request/multipart_with_file.rb +36 -0
  64. data/lib/twitter/rest/response/parse_error_json.rb +15 -0
  65. data/lib/twitter/rest/response/parse_json.rb +31 -0
  66. data/lib/twitter/{response → rest/response}/raise_error.rb +8 -11
  67. data/lib/twitter/search_results.rb +37 -21
  68. data/lib/twitter/settings.rb +1 -6
  69. data/lib/twitter/size.rb +2 -15
  70. data/lib/twitter/streaming/client.rb +89 -0
  71. data/lib/twitter/streaming/connection.rb +22 -0
  72. data/lib/twitter/streaming/event.rb +35 -0
  73. data/lib/twitter/streaming/friend_list.rb +13 -0
  74. data/lib/twitter/streaming/message_parser.rb +18 -0
  75. data/lib/twitter/streaming/response.rb +34 -0
  76. data/lib/twitter/suggestion.rb +5 -8
  77. data/lib/twitter/token.rb +11 -1
  78. data/lib/twitter/trend.rb +4 -8
  79. data/lib/twitter/trend_results.rb +65 -0
  80. data/lib/twitter/tweet.rb +43 -92
  81. data/lib/twitter/user.rb +72 -47
  82. data/lib/twitter/version.rb +4 -4
  83. data/lib/twitter.rb +4 -39
  84. data/spec/fixtures/already_posted.json +1 -0
  85. data/spec/fixtures/ids_list.json +1 -1
  86. data/spec/fixtures/ids_list2.json +1 -1
  87. data/spec/fixtures/request_token.txt +6 -0
  88. data/spec/fixtures/search.json +1 -1
  89. data/spec/fixtures/search_malformed.json +1 -1
  90. data/spec/fixtures/track_streaming.json +3 -0
  91. data/spec/fixtures/track_streaming_user.json +5 -0
  92. data/spec/helper.rb +8 -13
  93. data/spec/twitter/base_spec.rb +13 -103
  94. data/spec/twitter/basic_user_spec.rb +3 -3
  95. data/spec/twitter/configuration_spec.rb +1 -1
  96. data/spec/twitter/cursor_spec.rb +17 -35
  97. data/spec/twitter/direct_message_spec.rb +44 -11
  98. data/spec/twitter/entity/uri_spec.rb +75 -0
  99. data/spec/twitter/error_spec.rb +59 -11
  100. data/spec/twitter/geo/point_spec.rb +6 -6
  101. data/spec/twitter/geo/polygon_spec.rb +5 -5
  102. data/spec/twitter/geo_factory_spec.rb +4 -4
  103. data/spec/twitter/geo_results_spec.rb +35 -0
  104. data/spec/twitter/geo_spec.rb +6 -6
  105. data/spec/twitter/identifiable_spec.rb +5 -26
  106. data/spec/twitter/list_spec.rb +54 -11
  107. data/spec/twitter/media/photo_spec.rb +122 -6
  108. data/spec/twitter/media_factory_spec.rb +3 -3
  109. data/spec/twitter/null_object_spec.rb +27 -0
  110. data/spec/twitter/oembed_spec.rb +69 -45
  111. data/spec/twitter/place_spec.rb +84 -28
  112. data/spec/twitter/profile_banner_spec.rb +1 -1
  113. data/spec/twitter/rate_limit_spec.rb +8 -25
  114. data/spec/twitter/relationship_spec.rb +26 -12
  115. data/spec/twitter/{api → rest/api}/direct_messages_spec.rb +28 -15
  116. data/spec/twitter/{api → rest/api}/favorites_spec.rb +80 -7
  117. data/spec/twitter/{api → rest/api}/friends_and_followers_spec.rb +121 -152
  118. data/spec/twitter/{api → rest/api}/geo_spec.rb +9 -23
  119. data/spec/twitter/{api → rest/api}/help_spec.rb +6 -6
  120. data/spec/twitter/{api → rest/api}/lists_spec.rb +116 -95
  121. data/spec/twitter/{api → rest/api}/oauth_spec.rb +21 -10
  122. data/spec/twitter/{api → rest/api}/saved_searches_spec.rb +13 -13
  123. data/spec/twitter/{api → rest/api}/search_spec.rb +8 -9
  124. data/spec/twitter/{api → rest/api}/spam_reporting_spec.rb +3 -3
  125. data/spec/twitter/{api → rest/api}/suggested_users_spec.rb +5 -5
  126. data/spec/twitter/{api → rest/api}/timelines_spec.rb +9 -9
  127. data/spec/twitter/{api → rest/api}/trends_spec.rb +6 -6
  128. data/spec/twitter/rest/api/tweets_spec.rb +503 -0
  129. data/spec/twitter/{api → rest/api}/undocumented_spec.rb +19 -45
  130. data/spec/twitter/{api → rest/api}/users_spec.rb +64 -39
  131. data/spec/twitter/rest/client_spec.rb +193 -0
  132. data/spec/twitter/saved_search_spec.rb +14 -3
  133. data/spec/twitter/search_results_spec.rb +32 -45
  134. data/spec/twitter/settings_spec.rb +17 -6
  135. data/spec/twitter/size_spec.rb +5 -15
  136. data/spec/twitter/source_user_spec.rb +3 -3
  137. data/spec/twitter/streaming/client_spec.rb +92 -0
  138. data/spec/twitter/streaming/event_spec.rb +45 -0
  139. data/spec/twitter/suggestion_spec.rb +5 -15
  140. data/spec/twitter/target_user_spec.rb +3 -3
  141. data/spec/twitter/token_spec.rb +16 -0
  142. data/spec/twitter/trend_results_spec.rb +89 -0
  143. data/spec/twitter/trend_spec.rb +26 -13
  144. data/spec/twitter/tweet_spec.rb +137 -124
  145. data/spec/twitter/user_spec.rb +139 -80
  146. data/spec/twitter_spec.rb +0 -119
  147. data/twitter.gemspec +12 -6
  148. data.tar.gz.sig +0 -0
  149. metadata +197 -138
  150. metadata.gz.sig +0 -0
  151. data/lib/twitter/action/favorite.rb +0 -19
  152. data/lib/twitter/action/follow.rb +0 -30
  153. data/lib/twitter/action/list_member_added.rb +0 -39
  154. data/lib/twitter/action/mention.rb +0 -46
  155. data/lib/twitter/action/reply.rb +0 -27
  156. data/lib/twitter/action/retweet.rb +0 -27
  157. data/lib/twitter/action/tweet.rb +0 -20
  158. data/lib/twitter/action_factory.rb +0 -22
  159. data/lib/twitter/api/arguments.rb +0 -13
  160. data/lib/twitter/api/direct_messages.rb +0 -148
  161. data/lib/twitter/api/favorites.rb +0 -126
  162. data/lib/twitter/api/friends_and_followers.rb +0 -334
  163. data/lib/twitter/api/help.rb +0 -64
  164. data/lib/twitter/api/lists.rb +0 -618
  165. data/lib/twitter/api/oauth.rb +0 -44
  166. data/lib/twitter/api/places_and_geo.rb +0 -121
  167. data/lib/twitter/api/saved_searches.rb +0 -99
  168. data/lib/twitter/api/search.rb +0 -37
  169. data/lib/twitter/api/spam_reporting.rb +0 -30
  170. data/lib/twitter/api/suggested_users.rb +0 -55
  171. data/lib/twitter/api/timelines.rb +0 -214
  172. data/lib/twitter/api/trends.rb +0 -63
  173. data/lib/twitter/api/tweets.rb +0 -304
  174. data/lib/twitter/api/undocumented.rb +0 -97
  175. data/lib/twitter/api/users.rb +0 -439
  176. data/lib/twitter/api/utils.rb +0 -187
  177. data/lib/twitter/configurable.rb +0 -96
  178. data/lib/twitter/default.rb +0 -102
  179. data/lib/twitter/entity/url.rb +0 -9
  180. data/lib/twitter/error/client_error.rb +0 -35
  181. data/lib/twitter/error/decode_error.rb +0 -9
  182. data/lib/twitter/error/identity_map_key_error.rb +0 -9
  183. data/lib/twitter/error/server_error.rb +0 -28
  184. data/lib/twitter/exceptable.rb +0 -36
  185. data/lib/twitter/identity_map.rb +0 -22
  186. data/lib/twitter/request/multipart_with_file.rb +0 -34
  187. data/lib/twitter/response/parse_json.rb +0 -25
  188. data/spec/fixtures/about_me.json +0 -1
  189. data/spec/fixtures/activity_summary.json +0 -1
  190. data/spec/fixtures/bad_gateway.json +0 -1
  191. data/spec/fixtures/bad_request.json +0 -1
  192. data/spec/fixtures/by_friends.json +0 -1
  193. data/spec/fixtures/end_session.json +0 -1
  194. data/spec/fixtures/forbidden.json +0 -1
  195. data/spec/fixtures/internal_server_error.json +0 -1
  196. data/spec/fixtures/not_acceptable.json +0 -1
  197. data/spec/fixtures/phoenix_search.phoenix +0 -1
  198. data/spec/fixtures/resolve.json +0 -1
  199. data/spec/fixtures/service_unavailable.json +0 -1
  200. data/spec/fixtures/totals.json +0 -1
  201. data/spec/fixtures/trends.json +0 -1
  202. data/spec/fixtures/unauthorized.json +0 -1
  203. data/spec/fixtures/video_facets.json +0 -1
  204. data/spec/twitter/action/favorite_spec.rb +0 -29
  205. data/spec/twitter/action/follow_spec.rb +0 -29
  206. data/spec/twitter/action/list_member_added_spec.rb +0 -41
  207. data/spec/twitter/action/mention_spec.rb +0 -52
  208. data/spec/twitter/action/reply_spec.rb +0 -41
  209. data/spec/twitter/action/retweet_spec.rb +0 -41
  210. data/spec/twitter/action_factory_spec.rb +0 -35
  211. data/spec/twitter/action_spec.rb +0 -16
  212. data/spec/twitter/api/tweets_spec.rb +0 -285
  213. data/spec/twitter/client_spec.rb +0 -223
  214. data/spec/twitter/error/client_error_spec.rb +0 -23
  215. data/spec/twitter/error/server_error_spec.rb +0 -20
@@ -0,0 +1,15 @@
1
+ module Twitter
2
+ module Enumerable
3
+ include ::Enumerable
4
+
5
+ # @return [Enumerator]
6
+ def each(start=0, &block)
7
+ return to_enum(:each) unless block_given?
8
+ Array(@collection[start..-1]).each do |element|
9
+ yield element
10
+ end
11
+ self
12
+ end
13
+
14
+ end
15
+ end
@@ -3,7 +3,7 @@ require 'twitter/error/forbidden'
3
3
  module Twitter
4
4
  class Error
5
5
  # Raised when a Tweet has already been favorited
6
- class AlreadyFavorited < Twitter::Error
6
+ class AlreadyFavorited < Twitter::Error::Forbidden
7
7
  MESSAGE = "You have already favorited this status"
8
8
  end
9
9
  end
@@ -0,0 +1,10 @@
1
+ require 'twitter/error/forbidden'
2
+
3
+ module Twitter
4
+ class Error
5
+ # Raised when a Tweet has already been posted
6
+ class AlreadyPosted < Twitter::Error::Forbidden
7
+ MESSAGE = "Status is a duplicate"
8
+ end
9
+ end
10
+ end
@@ -3,7 +3,7 @@ require 'twitter/error/forbidden'
3
3
  module Twitter
4
4
  class Error
5
5
  # Raised when a Tweet has already been retweeted
6
- class AlreadyRetweeted < Twitter::Error
6
+ class AlreadyRetweeted < Twitter::Error::Forbidden
7
7
  MESSAGE = "sharing is not permissible for this status (Share validations failed)"
8
8
  end
9
9
  end
@@ -1,11 +1,10 @@
1
- require 'twitter/error/server_error'
1
+ require 'twitter/error'
2
2
 
3
3
  module Twitter
4
4
  class Error
5
5
  # Raised when Twitter returns the HTTP status code 502
6
- class BadGateway < Twitter::Error::ServerError
6
+ class BadGateway < Twitter::Error
7
7
  HTTP_STATUS_CODE = 502
8
- MESSAGE = "Twitter is down or being upgraded."
9
8
  end
10
9
  end
11
10
  end
@@ -1,9 +1,9 @@
1
- require 'twitter/error/client_error'
1
+ require 'twitter/error'
2
2
 
3
3
  module Twitter
4
4
  class Error
5
5
  # Raised when Twitter returns the HTTP status code 400
6
- class BadRequest < Twitter::Error::ClientError
6
+ class BadRequest < Twitter::Error
7
7
  HTTP_STATUS_CODE = 400
8
8
  end
9
9
  end
@@ -1,9 +1,9 @@
1
- require 'twitter/error/client_error'
1
+ require 'twitter/error'
2
2
 
3
3
  module Twitter
4
4
  class Error
5
5
  # Raised when Twitter returns the HTTP status code 403
6
- class Forbidden < Twitter::Error::ClientError
6
+ class Forbidden < Twitter::Error
7
7
  HTTP_STATUS_CODE = 403
8
8
  end
9
9
  end
@@ -1,11 +1,10 @@
1
- require 'twitter/error/server_error'
1
+ require 'twitter/error'
2
2
 
3
3
  module Twitter
4
4
  class Error
5
5
  # Raised when Twitter returns the HTTP status code 504
6
- class GatewayTimeout < Twitter::Error::ServerError
6
+ class GatewayTimeout < Twitter::Error
7
7
  HTTP_STATUS_CODE = 504
8
- MESSAGE = "The Twitter servers are up, but the request couldn't be serviced due to some failure within our stack. Try again later."
9
8
  end
10
9
  end
11
10
  end
@@ -1,11 +1,10 @@
1
- require 'twitter/error/server_error'
1
+ require 'twitter/error'
2
2
 
3
3
  module Twitter
4
4
  class Error
5
5
  # Raised when Twitter returns the HTTP status code 500
6
- class InternalServerError < Twitter::Error::ServerError
6
+ class InternalServerError < Twitter::Error
7
7
  HTTP_STATUS_CODE = 500
8
- MESSAGE = "Something is technically wrong."
9
8
  end
10
9
  end
11
10
  end
@@ -1,9 +1,9 @@
1
- require 'twitter/error/client_error'
1
+ require 'twitter/error'
2
2
 
3
3
  module Twitter
4
4
  class Error
5
5
  # Raised when Twitter returns the HTTP status code 406
6
- class NotAcceptable < Twitter::Error::ClientError
6
+ class NotAcceptable < Twitter::Error
7
7
  HTTP_STATUS_CODE = 406
8
8
  end
9
9
  end
@@ -1,9 +1,9 @@
1
- require 'twitter/error/client_error'
1
+ require 'twitter/error'
2
2
 
3
3
  module Twitter
4
4
  class Error
5
5
  # Raised when Twitter returns the HTTP status code 404
6
- class NotFound < Twitter::Error::ClientError
6
+ class NotFound < Twitter::Error
7
7
  HTTP_STATUS_CODE = 404
8
8
  end
9
9
  end
@@ -1,11 +1,10 @@
1
- require 'twitter/error/server_error'
1
+ require 'twitter/error'
2
2
 
3
3
  module Twitter
4
4
  class Error
5
5
  # Raised when Twitter returns the HTTP status code 503
6
- class ServiceUnavailable < Twitter::Error::ServerError
6
+ class ServiceUnavailable < Twitter::Error
7
7
  HTTP_STATUS_CODE = 503
8
- MESSAGE = "(__-){ Twitter is over capacity."
9
8
  end
10
9
  end
11
10
  end
@@ -1,9 +1,9 @@
1
- require 'twitter/error/client_error'
1
+ require 'twitter/error'
2
2
 
3
3
  module Twitter
4
4
  class Error
5
5
  # Raised when Twitter returns the HTTP status code 429
6
- class TooManyRequests < Twitter::Error::ClientError
6
+ class TooManyRequests < Twitter::Error
7
7
  HTTP_STATUS_CODE = 429
8
8
  end
9
9
  EnhanceYourCalm = TooManyRequests
@@ -1,9 +1,9 @@
1
- require 'twitter/error/client_error'
1
+ require 'twitter/error'
2
2
 
3
3
  module Twitter
4
4
  class Error
5
5
  # Raised when Twitter returns the HTTP status code 401
6
- class Unauthorized < Twitter::Error::ClientError
6
+ class Unauthorized < Twitter::Error
7
7
  HTTP_STATUS_CODE = 401
8
8
  end
9
9
  end
@@ -1,9 +1,9 @@
1
- require 'twitter/error/client_error'
1
+ require 'twitter/error'
2
2
 
3
3
  module Twitter
4
4
  class Error
5
5
  # Raised when Twitter returns the HTTP status code 422
6
- class UnprocessableEntity < Twitter::Error::ClientError
6
+ class UnprocessableEntity < Twitter::Error
7
7
  HTTP_STATUS_CODE = 422
8
8
  end
9
9
  end
data/lib/twitter/error.rb CHANGED
@@ -1,33 +1,84 @@
1
+ require 'descendants_tracker'
1
2
  require 'twitter/rate_limit'
2
3
 
3
4
  module Twitter
4
5
  # Custom error class for rescuing from all Twitter errors
5
6
  class Error < StandardError
6
- attr_reader :rate_limit, :wrapped_exception
7
+ extend DescendantsTracker
8
+ attr_reader :rate_limit, :wrapped_exception, :code
7
9
 
8
- # @return [Hash]
9
- def self.errors
10
- @errors ||= Hash[descendants.map{|klass| [klass.const_get(:HTTP_STATUS_CODE), klass]}]
10
+ # If error code is missing see https://dev.twitter.com/docs/error-codes-responses
11
+ module Codes
12
+ AUTHENTICATION_PROBLEM = 32
13
+ RESOURCE_NOT_FOUND = 34
14
+ SUSPENDED_ACCOUNT = 64
15
+ DEPRECATED_CALL = 68
16
+ RATE_LIMIT_EXCEEDED = 88
17
+ INVALID_OR_EXPIRED_TOKEN = 89
18
+ OVER_CAPACITY = 130
19
+ INTERNAL_ERROR = 131
20
+ OAUTH_TIMESTAMP_OUT_OF_RANGE = 135
21
+ FOLLOW_LIMIT_EXCEEDED = 161
22
+ PROTECTED_STATUS = 179
23
+ DUPLICATE_STATUS = 187
24
+ BAD_AUTHENTICATION_DATA = 215
25
+ LOGIN_VERIFICATION_NEEDED = 231
26
+ ENDPOINT_RETIRED = 251
11
27
  end
12
28
 
13
- # @return [Array]
14
- def self.descendants
15
- ObjectSpace.each_object(::Class).select{|klass| klass < self}
29
+ class << self
30
+
31
+ # Create a new error from an HTTP response
32
+ #
33
+ # @param response [Hash]
34
+ # @return [Twitter::Error]
35
+ def from_response(response={})
36
+ error, code = parse_error(response[:body])
37
+ new(error, response[:response_headers], code)
38
+ end
39
+
40
+ # @return [Hash]
41
+ def errors
42
+ @errors ||= descendants.inject({}) do |hash, klass|
43
+ hash[klass::HTTP_STATUS_CODE] = klass
44
+ hash
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def parse_error(body)
51
+ if body.nil?
52
+ ['', nil]
53
+ elsif body[:error]
54
+ [body[:error], nil]
55
+ elsif body[:errors]
56
+ extract_message_from_errors(body)
57
+ end
58
+ end
59
+
60
+ def extract_message_from_errors(body)
61
+ first = Array(body[:errors]).first
62
+ if first.is_a?(Hash)
63
+ [first[:message].chomp, first[:code]]
64
+ else
65
+ [first.chomp, nil]
66
+ end
67
+ end
68
+
16
69
  end
17
70
 
18
71
  # Initializes a new Error object
19
72
  #
20
73
  # @param exception [Exception, String]
21
74
  # @param response_headers [Hash]
75
+ # @param code [Integer]
22
76
  # @return [Twitter::Error]
23
- def initialize(exception=$!, response_headers={})
24
- @rate_limit = Twitter::RateLimit.new(response_headers)
77
+ def initialize(exception=$!, response_headers={}, code=nil)
78
+ @rate_limit = RateLimit.new(response_headers)
25
79
  @wrapped_exception = exception
26
- exception.respond_to?(:backtrace) ? super(exception.message) : super(exception.to_s)
27
- end
28
-
29
- def backtrace
30
- @wrapped_exception.respond_to?(:backtrace) ? @wrapped_exception.backtrace : super
80
+ @code = code
81
+ exception.respond_to?(:message) ? super(exception.message) : super(exception.to_s)
31
82
  end
32
83
 
33
84
  end
@@ -1,20 +1,21 @@
1
1
  module Twitter
2
2
  class Factory
3
3
 
4
- # Instantiates a new action object
5
- #
6
- # @param attrs [Hash]
7
- # @raise [ArgumentError] Error raised when supplied argument is missing an :action key.
8
- # @return [Twitter::Action::Favorite, Twitter::Action::Follow, Twitter::Action::ListMemberAdded, Twitter::Action::Mention, Twitter::Action::Reply, Twitter::Action::Retweet]
9
- def self.fetch_or_new(method, klass, attrs={})
10
- return unless attrs
11
- type = attrs.delete(method.to_sym)
12
- if type
4
+ class << self
5
+
6
+ # Construct a new object
7
+ #
8
+ # @param method [Symbol]
9
+ # @param klass [Class]
10
+ # @param attrs [Hash]
11
+ # @raise [IndexError] Error raised when supplied argument is missing a key.
12
+ # @return [Twitter::Base]
13
+ def new(method, klass, attrs={})
14
+ type = attrs.fetch(method.to_sym)
13
15
  const_name = type.gsub(/\/(.?)/){"::#{$1.upcase}"}.gsub(/(?:^|_)(.)/){$1.upcase}
14
- klass.const_get(const_name.to_sym).fetch_or_new(attrs)
15
- else
16
- raise ArgumentError, "argument must have :#{method} key"
16
+ klass.const_get(const_name.to_sym).new(attrs)
17
17
  end
18
+
18
19
  end
19
20
 
20
21
  end
data/lib/twitter/geo.rb CHANGED
@@ -1,15 +1,10 @@
1
+ require 'equalizer'
1
2
  require 'twitter/base'
2
3
 
3
4
  module Twitter
4
5
  class Geo < Twitter::Base
6
+ include Equalizer.new(:coordinates)
5
7
  attr_reader :coordinates
6
8
  alias coords coordinates
7
-
8
- # @param other [Twitter::Geo]
9
- # @return [Boolean]
10
- def ==(other)
11
- super || attr_equal(:coordinates, other) || attrs_equal(other)
12
- end
13
-
14
9
  end
15
10
  end
@@ -5,13 +5,17 @@ require 'twitter/geo/polygon'
5
5
  module Twitter
6
6
  class GeoFactory < Twitter::Factory
7
7
 
8
- # Instantiates a new geo object
9
- #
10
- # @param attrs [Hash]
11
- # @raise [ArgumentError] Error raised when supplied argument is missing a :type key.
12
- # @return [Twitter::Geo]
13
- def self.fetch_or_new(attrs={})
14
- super(:type, Twitter::Geo, attrs)
8
+ class << self
9
+
10
+ # Construct a new geo object
11
+ #
12
+ # @param attrs [Hash]
13
+ # @raise [IndexError] Error raised when supplied argument is missing a :type key.
14
+ # @return [Twitter::Geo]
15
+ def new(attrs={})
16
+ super(:type, Geo, attrs)
17
+ end
18
+
15
19
  end
16
20
 
17
21
  end
@@ -0,0 +1,40 @@
1
+ require 'twitter/enumerable'
2
+
3
+ module Twitter
4
+ class GeoResults
5
+ include Twitter::Enumerable
6
+ attr_reader :attrs
7
+ alias to_h attrs
8
+ alias to_hash attrs
9
+ alias to_hsh attrs
10
+
11
+ class << self
12
+
13
+ # Construct a new GeoResults object from a response hash
14
+ #
15
+ # @param response [Hash]
16
+ # @return [Twitter::Base]
17
+ def from_response(response={})
18
+ new(response[:body])
19
+ end
20
+
21
+ end
22
+
23
+ # Initializes a new GeoResults object
24
+ #
25
+ # @param attrs [Hash]
26
+ # @return [Twitter::GeoResults]
27
+ def initialize(attrs={})
28
+ @attrs = attrs
29
+ @collection = Array(@attrs[:result][:places]).map do |place|
30
+ Place.new(place)
31
+ end
32
+ end
33
+
34
+ # @return [String]
35
+ def token
36
+ @attrs[:token]
37
+ end
38
+
39
+ end
40
+ end
@@ -1,29 +1,10 @@
1
+ require 'equalizer'
1
2
  require 'twitter/base'
2
- require 'twitter/error/identity_map_key_error'
3
3
 
4
4
  module Twitter
5
5
  class Identity < Twitter::Base
6
-
7
- def self.fetch(attrs)
8
- return unless identity_map
9
-
10
- id = attrs[:id]
11
- if id && object = identity_map.fetch(id)
12
- return object.update(attrs)
13
- end
14
-
15
- return yield if block_given?
16
- raise Twitter::Error::IdentityMapKeyError, "key not found"
17
- end
18
-
19
- # Stores an object in the identity map.
20
- #
21
- # @param object [Object]
22
- # @return [Twitter::Identity]
23
- def self.store(object)
24
- return object unless identity_map
25
- identity_map.store(object.id, object)
26
- end
6
+ include Equalizer.new(:id)
7
+ attr_reader :id
27
8
 
28
9
  # Initializes a new object
29
10
  #
@@ -31,19 +12,8 @@ module Twitter
31
12
  # @raise [ArgumentError] Error raised when supplied argument is missing an :id key.
32
13
  # @return [Twitter::Identity]
33
14
  def initialize(attrs={})
15
+ attrs.fetch(:id)
34
16
  super
35
- raise ArgumentError, "argument must have an :id key" unless id
36
- end
37
-
38
- # @param other [Twitter::Identity]
39
- # @return [Boolean]
40
- def ==(other)
41
- super || attr_equal(:id, other) || attrs_equal(other)
42
- end
43
-
44
- # @return [Integer]
45
- def id
46
- @attrs[:id]
47
17
  end
48
18
 
49
19
  end
data/lib/twitter/list.rb CHANGED
@@ -5,12 +5,29 @@ module Twitter
5
5
  class List < Twitter::Identity
6
6
  include Twitter::Creatable
7
7
  attr_reader :description, :following, :full_name, :member_count,
8
- :mode, :name, :slug, :subscriber_count, :uri
8
+ :mode, :name, :slug, :subscriber_count
9
+ object_attr_reader :User, :user
9
10
 
10
- # @return [Twitter::User]
11
- def user
12
- @user ||= Twitter::User.fetch_or_new(@attrs[:user])
11
+ # @return [URI] The URI to the list members.
12
+ def members_uri
13
+ URI.parse("https://twitter.com/#{user.screen_name}/#{slug}/members")
13
14
  end
15
+ memoize :members_uri
16
+ alias members_url members_uri
17
+
18
+ # @return [URI] The URI to the list subscribers.
19
+ def subscribers_uri
20
+ URI.parse("https://twitter.com/#{user.screen_name}/#{slug}/subscribers")
21
+ end
22
+ memoize :subscribers_uri
23
+ alias subscribers_url subscribers_uri
24
+
25
+ # @return [URI] The URI to the list.
26
+ def uri
27
+ URI.parse("https://twitter.com/#{user.screen_name}/#{slug}")
28
+ end
29
+ memoize :uri
30
+ alias url uri
14
31
 
15
32
  end
16
33
  end
@@ -3,18 +3,20 @@ require 'twitter/identity'
3
3
  module Twitter
4
4
  module Media
5
5
  class Photo < Twitter::Identity
6
- attr_reader :display_url, :expanded_url, :indices, :media_url,
7
- :media_url_https, :url
6
+ attr_reader :indices
7
+ display_uri_attr_reader
8
+ uri_attr_reader :expanded_uri, :media_uri, :media_uri_https, :uri
8
9
 
9
10
  # Returns an array of photo sizes
10
11
  #
11
12
  # @return [Array<Twitter::Size>]
12
13
  def sizes
13
- @sizes ||= Array(@attrs[:sizes]).inject({}) do |object, (key, value)|
14
- object[key] = Twitter::Size.fetch_or_new(value)
14
+ Array(@attrs[:sizes]).inject({}) do |object, (key, value)|
15
+ object[key] = Size.new(value)
15
16
  object
16
17
  end
17
18
  end
19
+ memoize :sizes
18
20
 
19
21
  end
20
22
  end
@@ -4,13 +4,17 @@ require 'twitter/media/photo'
4
4
  module Twitter
5
5
  class MediaFactory < Twitter::Factory
6
6
 
7
- # Instantiates a new media object
8
- #
9
- # @param attrs [Hash]
10
- # @raise [ArgumentError] Error raised when supplied argument is missing a :type key.
11
- # @return [Twitter::Media]
12
- def self.fetch_or_new(attrs={})
13
- super(:type, Twitter::Media, attrs)
7
+ class << self
8
+
9
+ # Construct a new media object
10
+ #
11
+ # @param attrs [Hash]
12
+ # @raise [IndexError] Error raised when supplied argument is missing a :type key.
13
+ # @return [Twitter::Media]
14
+ def new(attrs={})
15
+ super(:type, Media, attrs)
16
+ end
17
+
14
18
  end
15
19
 
16
20
  end
@@ -0,0 +1,25 @@
1
+ module Twitter
2
+ class NullObject
3
+
4
+ # @return [TrueClass] This method always returns true.
5
+ def nil?
6
+ true
7
+ end
8
+
9
+ # @return [Twitter::NullObject] This method always returns self.
10
+ def method_missing(*args, &block)
11
+ self
12
+ end
13
+
14
+ # @return [TrueClass] This method always returns true.
15
+ def respond_to?(method_name, include_private=false)
16
+ true
17
+ end if RUBY_VERSION < "1.9"
18
+
19
+ # @return [TrueClass] This method always returns true.
20
+ def respond_to_missing?(method_name, include_private=false)
21
+ true
22
+ end if RUBY_VERSION >= "1.9"
23
+
24
+ end
25
+ end
@@ -2,7 +2,8 @@ require 'twitter/base'
2
2
 
3
3
  module Twitter
4
4
  class OEmbed < Twitter::Base
5
- attr_reader :author_name, :author_url, :cache_age, :height, :html,
6
- :provider_name, :provider_url, :type, :url, :version, :width
5
+ attr_reader :author_name, :cache_age, :height, :html, :provider_name,
6
+ :type, :version, :width
7
+ uri_attr_reader :author_uri, :provider_uri, :uri
7
8
  end
8
9
  end
data/lib/twitter/place.rb CHANGED
@@ -1,33 +1,33 @@
1
- require 'twitter/identity'
1
+ require 'equalizer'
2
+ require 'twitter/base'
2
3
 
3
4
  module Twitter
4
- class Place < Twitter::Identity
5
- attr_reader :attributes, :country, :full_name, :name, :url, :woeid
5
+ class Place < Twitter::Base
6
+ include Equalizer.new(:woeid)
7
+ attr_reader :attributes, :country, :full_name, :name, :woeid
6
8
  alias woe_id woeid
7
-
8
- # @return [Twitter::Geo]
9
- def bounding_box
10
- @bounding_box ||= Twitter::GeoFactory.fetch_or_new(@attrs[:bounding_box])
11
- end
9
+ object_attr_reader :GeoFactory, :bounding_box
10
+ object_attr_reader :Place, :contained_within
11
+ alias contained? contained_within?
12
+ uri_attr_reader :uri
12
13
 
13
14
  # @return [String]
14
15
  def country_code
15
- @country_code ||= @attrs[:country_code] || @attrs[:countryCode]
16
+ @attrs[:country_code] || @attrs[:countryCode]
16
17
  end
18
+ memoize :country_code
17
19
 
18
20
  # @return [Integer]
19
21
  def parent_id
20
- @parent_id ||= @attrs[:parentid]
22
+ @attrs[:parentid]
21
23
  end
24
+ memoize :parent_id
22
25
 
23
26
  # @return [String]
24
27
  def place_type
25
- @place_type ||= @attrs[:place_type] || @attrs[:placeType] && @attrs[:placeType][:name]
26
- end
27
-
28
- def id
29
- @attrs[:id] || @attrs[:woeid]
28
+ @attrs[:place_type] || @attrs[:placeType] && @attrs[:placeType][:name]
30
29
  end
30
+ memoize :place_type
31
31
 
32
32
  end
33
33
  end
@@ -7,11 +7,12 @@ module Twitter
7
7
  #
8
8
  # @return [Array<Twitter::Size>]
9
9
  def sizes
10
- @sizes ||= Array(@attrs[:sizes]).inject({}) do |object, (key, value)|
11
- object[key] = Twitter::Size.fetch_or_new(value)
10
+ Array(@attrs[:sizes]).inject({}) do |object, (key, value)|
11
+ object[key] = Size.new(value)
12
12
  object
13
13
  end
14
14
  end
15
+ memoize :sizes
15
16
 
16
17
  end
17
18
  end