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,383 @@
1
+ require 'twitter/arguments'
2
+ require 'twitter/core_ext/enumerable'
3
+ require 'twitter/error/not_found'
4
+ require 'twitter/profile_banner'
5
+ require 'twitter/rest/api/utils'
6
+ require 'twitter/settings'
7
+ require 'twitter/user'
8
+
9
+ module Twitter
10
+ module REST
11
+ module API
12
+ module Users
13
+ include Twitter::REST::API::Utils
14
+ MAX_USERS_PER_REQUEST = 100
15
+
16
+ # Updates the authenticating user's settings.
17
+ # Or, if no options supplied, returns settings (including current trend, geo and sleep time information) for the authenticating user.
18
+ #
19
+ # @see https://dev.twitter.com/docs/api/1.1/post/account/settings
20
+ # @see https://dev.twitter.com/docs/api/1.1/get/account/settings
21
+ # @rate_limited Yes
22
+ # @authentication Requires user context
23
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
24
+ # @return [Twitter::Settings]
25
+ # @param options [Hash] A customizable set of options.
26
+ # @option options [Integer] :trend_location_woeid The Yahoo! Where On Earth ID to use as the user's default trend location. Global information is available by using 1 as the WOEID. The woeid must be one of the locations returned by {https://dev.twitter.com/docs/api/1.1/get/trends/available GET trends/available}.
27
+ # @option options [Boolean, String, Integer] :sleep_time_enabled When set to true, 't' or 1, will enable sleep time for the user. Sleep time is the time when push or SMS notifications should not be sent to the user.
28
+ # @option options [Integer] :start_sleep_time The hour that sleep time should begin if it is enabled. The value for this parameter should be provided in {http://en.wikipedia.org/wiki/ISO_8601 ISO8601} format (i.e. 00-23). The time is considered to be in the same timezone as the user's time_zone setting.
29
+ # @option options [Integer] :end_sleep_time The hour that sleep time should end if it is enabled. The value for this parameter should be provided in {http://en.wikipedia.org/wiki/ISO_8601 ISO8601} format (i.e. 00-23). The time is considered to be in the same timezone as the user's time_zone setting.
30
+ # @option options [String] :time_zone The timezone dates and times should be displayed in for the user. The timezone must be one of the {http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html Rails TimeZone} names.
31
+ # @option options [String] :lang The language which Twitter should render in for this user. The language must be specified by the appropriate two letter ISO 639-1 representation. Currently supported languages are provided by {https://dev.twitter.com/docs/api/1.1/get/help/languages GET help/languages}.
32
+ def settings(options={})
33
+ request_method = options.size.zero? ? :get : :post
34
+ response = send(request_method.to_sym, "/1.1/account/settings.json", options)
35
+ # https://dev.twitter.com/issues/59
36
+ response.update(:trend_location => Array(response[:trend_location]).first)
37
+ Twitter::Settings.from_response(response)
38
+ end
39
+
40
+ # Returns the requesting user if authentication was successful, otherwise raises {Twitter::Error::Unauthorized}
41
+ #
42
+ # @see https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials
43
+ # @rate_limited Yes
44
+ # @authentication Requires user context
45
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
46
+ # @return [Twitter::User] The authenticated user.
47
+ # @param options [Hash] A customizable set of options.
48
+ # @option options [Boolean, String, Integer] :skip_status Do not include user's Tweets when set to true, 't' or 1.
49
+ def verify_credentials(options={})
50
+ object_from_response(Twitter::User, :get, "/1.1/account/verify_credentials.json", options)
51
+ end
52
+ alias current_user verify_credentials
53
+
54
+ # Sets which device Twitter delivers updates to for the authenticating user
55
+ #
56
+ # @see https://dev.twitter.com/docs/api/1.1/post/account/update_delivery_device
57
+ # @rate_limited No
58
+ # @authentication Requires user context
59
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
60
+ # @return [Twitter::User] The authenticated user.
61
+ # @param device [String] Must be one of: 'sms', 'none'.
62
+ # @param options [Hash] A customizable set of options.
63
+ def update_delivery_device(device, options={})
64
+ object_from_response(Twitter::User, :post, "/1.1/account/update_delivery_device.json", options.merge(:device => device))
65
+ end
66
+
67
+ # Sets values that users are able to set under the "Account" tab of their settings page
68
+ #
69
+ # @see https://dev.twitter.com/docs/api/1.1/post/account/update_profile
70
+ # @note Only the options specified will be updated.
71
+ # @rate_limited No
72
+ # @authentication Requires user context
73
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
74
+ # @return [Twitter::User] The authenticated user.
75
+ # @param options [Hash] A customizable set of options.
76
+ # @option options [String] :name Full name associated with the profile. Maximum of 20 characters.
77
+ # @option options [String] :url URL associated with the profile. Will be prepended with "http://" if not present. Maximum of 100 characters.
78
+ # @option options [String] :location The city or country describing where the user of the account is located. The contents are not normalized or geocoded in any way. Maximum of 30 characters.
79
+ # @option options [String] :description A description of the user owning the account. Maximum of 160 characters.
80
+ def update_profile(options={})
81
+ object_from_response(Twitter::User, :post, "/1.1/account/update_profile.json", options)
82
+ end
83
+
84
+ # Updates the authenticating user's profile background image
85
+ #
86
+ # @see https://dev.twitter.com/docs/api/1.1/post/account/update_profile_background_image
87
+ # @rate_limited No
88
+ # @authentication Requires user context
89
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
90
+ # @return [Twitter::User] The authenticated user.
91
+ # @param image [File] The background image for the profile, base64-encoded. Must be a valid GIF, JPG, or PNG image of less than 800 kilobytes in size. Images with width larger than 2048 pixels will be forcibly scaled down. The image must be provided as raw multipart data, not a URL.
92
+ # @param options [Hash] A customizable set of options.
93
+ # @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.
94
+ def update_profile_background_image(image, options={})
95
+ object_from_response(Twitter::User, :post, "/1.1/account/update_profile_background_image.json", options.merge(:image => image))
96
+ end
97
+
98
+ # Sets one or more hex values that control the color scheme of the authenticating user's profile
99
+ #
100
+ # @see https://dev.twitter.com/docs/api/1.1/post/account/update_profile_colors
101
+ # @rate_limited No
102
+ # @authentication Requires user context
103
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
104
+ # @return [Twitter::User] The authenticated user.
105
+ # @param options [Hash] A customizable set of options.
106
+ # @option options [String] :profile_background_color Profile background color.
107
+ # @option options [String] :profile_text_color Profile text color.
108
+ # @option options [String] :profile_link_color Profile link color.
109
+ # @option options [String] :profile_sidebar_fill_color Profile sidebar's background color.
110
+ # @option options [String] :profile_sidebar_border_color Profile sidebar's border color.
111
+ def update_profile_colors(options={})
112
+ object_from_response(Twitter::User, :post, "/1.1/account/update_profile_colors.json", options)
113
+ end
114
+
115
+ # Updates the authenticating user's profile image
116
+ #
117
+ # @see https://dev.twitter.com/docs/api/1.1/post/account/update_profile_image
118
+ # @note Updates the authenticating user's profile image. Note that this method expects raw multipart data, not a URL to an image.
119
+ # @note This method asynchronously processes the uploaded file before updating the user's profile image URL. You can either update your local cache the next time you request the user's information, or, at least 5 seconds after uploading the image, ask for the updated URL using GET users/show.
120
+ # @rate_limited No
121
+ # @authentication Requires user context
122
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
123
+ # @return [Twitter::User] The authenticated user.
124
+ # @param image [File] The avatar image for the profile, base64-encoded. Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size. Images with width larger than 500 pixels will be scaled down. Animated GIFs will be converted to a static GIF of the first frame, removing the animation.
125
+ # @param options [Hash] A customizable set of options.
126
+ def update_profile_image(image, options={})
127
+ object_from_response(Twitter::User, :post, "/1.1/account/update_profile_image.json", options.merge(:image => image))
128
+ end
129
+
130
+ # Returns an array of user objects that the authenticating user is blocking
131
+ #
132
+ # @see https://dev.twitter.com/docs/api/1.1/get/blocks/list
133
+ # @rate_limited Yes
134
+ # @authentication Requires user context
135
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
136
+ # @return [Array<Twitter::User>] User objects that the authenticating user is blocking.
137
+ # @param options [Hash] A customizable set of options.
138
+ # @option options [Integer] :page Specifies the page of results to retrieve.
139
+ def blocking(options={})
140
+ cursor_from_response(:users, Twitter::User, :get, "/1.1/blocks/list.json", options)
141
+ end
142
+
143
+ # Returns an array of numeric user ids the authenticating user is blocking
144
+ #
145
+ # @see https://dev.twitter.com/docs/api/1.1/get/blocks/ids
146
+ # @rate_limited Yes
147
+ # @authentication Requires user context
148
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
149
+ # @return [Array] Numeric user ids the authenticating user is blocking.
150
+ # @overload block(options={})
151
+ # @param options [Hash] A customizable set of options.
152
+ def blocked_ids(*args)
153
+ arguments = Twitter::Arguments.new(args)
154
+ merge_user!(arguments.options, arguments.pop)
155
+ cursor_from_response(:ids, nil, :get, "/1.1/blocks/ids.json", arguments.options)
156
+ end
157
+
158
+ # Returns true if the authenticating user is blocking a target user
159
+ #
160
+ # @see https://dev.twitter.com/docs/api/1.1/get/blocks/ids
161
+ # @rate_limited Yes
162
+ # @authentication Requires user context
163
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
164
+ # @return [Boolean] true if the authenticating user is blocking the target user, otherwise false.
165
+ # @param user [Integer, String, URI, Twitter::User] A Twitter user ID, screen name, URI, or object.
166
+ # @param options [Hash] A customizable set of options.
167
+ def block?(user, options={})
168
+ merge_default_cursor!(options)
169
+ user_id = case user
170
+ when Integer
171
+ user
172
+ when String, URI
173
+ user(user).id
174
+ when Twitter::User
175
+ user.id
176
+ end
177
+ blocked_ids(options).map(&:to_i).include?(user_id)
178
+ end
179
+
180
+ # Blocks the users specified by the authenticating user
181
+ #
182
+ # @see https://dev.twitter.com/docs/api/1.1/post/blocks/create
183
+ # @note Destroys a friendship to the blocked user if it exists.
184
+ # @rate_limited Yes
185
+ # @authentication Requires user context
186
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
187
+ # @return [Array<Twitter::User>] The blocked users.
188
+ # @overload block(*users)
189
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
190
+ # @overload block(*users, options)
191
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
192
+ # @param options [Hash] A customizable set of options.
193
+ def block(*args)
194
+ threaded_user_objects_from_response(:post, "/1.1/blocks/create.json", args)
195
+ end
196
+
197
+ # Un-blocks the users specified by the authenticating user
198
+ #
199
+ # @see https://dev.twitter.com/docs/api/1.1/post/blocks/destroy
200
+ # @rate_limited No
201
+ # @authentication Requires user context
202
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
203
+ # @return [Array<Twitter::User>] The un-blocked users.
204
+ # @overload unblock(*users)
205
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
206
+ # @overload unblock(*users, options)
207
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
208
+ # @param options [Hash] A customizable set of options.
209
+ def unblock(*args)
210
+ threaded_user_objects_from_response(:post, "/1.1/blocks/destroy.json", args)
211
+ end
212
+
213
+ # Returns extended information for up to 100 users
214
+ #
215
+ # @see https://dev.twitter.com/docs/api/1.1/get/users/lookup
216
+ # @rate_limited Yes
217
+ # @authentication Requires user context
218
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
219
+ # @return [Array<Twitter::User>] The requested users.
220
+ # @overload users(*users)
221
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
222
+ # @overload users(*users, options)
223
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
224
+ # @param options [Hash] A customizable set of options.
225
+ # @option options [Symbol, String] :method Requests users via a GET request instead of the standard POST request if set to ':get'.
226
+ # @option options [Boolean] :include_entities The tweet entities node will be disincluded when set to false.
227
+ def users(*args)
228
+ arguments = Twitter::Arguments.new(args)
229
+ method = arguments.options.delete(:method) || :post
230
+ arguments.flatten.each_slice(MAX_USERS_PER_REQUEST).threaded_map do |users|
231
+ objects_from_response(Twitter::User, method, "/1.1/users/lookup.json", merge_users(arguments.options, users))
232
+ end.flatten
233
+ end
234
+
235
+ # @see https://dev.twitter.com/docs/api/1.1/get/users/show
236
+ # @rate_limited Yes
237
+ # @authentication Requires user context
238
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
239
+ # @return [Twitter::User] The requested user.
240
+ # @overload user(options={})
241
+ # Returns extended information for the authenticated user
242
+ #
243
+ # @param options [Hash] A customizable set of options.
244
+ # @option options [Boolean] :include_entities The tweet entities node will be disincluded when set to false.
245
+ # @option options [Boolean, String, Integer] :skip_status Do not include user's Tweets when set to true, 't' or 1.
246
+ # @overload user(user, options={})
247
+ # Returns extended information for a given user
248
+ #
249
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
250
+ # @param options [Hash] A customizable set of options.
251
+ # @option options [Boolean] :include_entities The tweet entities node will be disincluded when set to false.
252
+ # @option options [Boolean, String, Integer] :skip_status Do not include user's Tweets when set to true, 't' or 1.
253
+ def user(*args)
254
+ arguments = Twitter::Arguments.new(args)
255
+ if arguments.last
256
+ merge_user!(arguments.options, arguments.pop)
257
+ object_from_response(Twitter::User, :get, "/1.1/users/show.json", arguments.options)
258
+ else
259
+ verify_credentials(arguments.options)
260
+ end
261
+ end
262
+
263
+ # Returns true if the specified user exists
264
+ #
265
+ # @rate_limited Yes
266
+ # @authentication Requires user context
267
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
268
+ # @return [Boolean] true if the user exists, otherwise false.
269
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
270
+ def user?(user, options={})
271
+ merge_user!(options, user)
272
+ get("/1.1/users/show.json", options)
273
+ true
274
+ rescue Twitter::Error::NotFound
275
+ false
276
+ end
277
+
278
+ # Returns users that match the given query
279
+ #
280
+ # @see https://dev.twitter.com/docs/api/1.1/get/users/search
281
+ # @rate_limited Yes
282
+ # @authentication Requires user context
283
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
284
+ # @return [Array<Twitter::User>]
285
+ # @param query [String] The search query to run against people search.
286
+ # @param options [Hash] A customizable set of options.
287
+ # @option options [Integer] :count The number of people to retrieve. Maxiumum of 20 allowed per page.
288
+ # @option options [Integer] :page Specifies the page of results to retrieve.
289
+ def user_search(query, options={})
290
+ objects_from_response(Twitter::User, :get, "/1.1/users/search.json", options.merge(:q => query))
291
+ end
292
+
293
+ # Returns an array of users that the specified user can contribute to
294
+ #
295
+ # @see https://dev.twitter.com/docs/api/1.1/get/users/contributees
296
+ # @rate_limited Yes
297
+ # @authentication Requires user context
298
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
299
+ # @return [Array<Twitter::User>]
300
+ # @overload contributees(options={})
301
+ # @param options [Hash] A customizable set of options.
302
+ # @option options [Boolean, String, Integer] :skip_status Do not include contributee's Tweets when set to true, 't' or 1.
303
+ # @overload contributees(user, options={})
304
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
305
+ # @param options [Hash] A customizable set of options.
306
+ # @option options [Boolean, String, Integer] :skip_status Do not include contributee's Tweets when set to true, 't' or 1.
307
+ def contributees(*args)
308
+ user_objects_from_response(:get, "/1.1/users/contributees.json", args)
309
+ end
310
+
311
+ # Returns an array of users who can contribute to the specified account
312
+ #
313
+ # @see https://dev.twitter.com/docs/api/1.1/get/users/contributors
314
+ # @rate_limited Yes
315
+ # @authentication Requires user context
316
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
317
+ # @return [Array<Twitter::User>]
318
+ # @overload contributors(options={})
319
+ # @param options [Hash] A customizable set of options.
320
+ # @option options [Boolean, String, Integer] :skip_status Do not include contributee's Tweets when set to true, 't' or 1.
321
+ # @overload contributors(user, options={})
322
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
323
+ # @param options [Hash] A customizable set of options.
324
+ # @option options [Boolean, String, Integer] :skip_status Do not include contributee's Tweets when set to true, 't' or 1.
325
+ def contributors(*args)
326
+ user_objects_from_response(:get, "/1.1/users/contributors.json", args)
327
+ end
328
+
329
+ # Removes the authenticating user's profile banner image
330
+ #
331
+ # @see https://dev.twitter.com/docs/api/1.1/post/account/remove_profile_banner
332
+ # @rate_limited No
333
+ # @authentication Requires user context
334
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
335
+ # @return [nil]
336
+ # @param options [Hash] A customizable set of options.
337
+ def remove_profile_banner(options={})
338
+ post("/1.1/account/remove_profile_banner.json", options)[:body]
339
+ end
340
+ deprecate_alias :profile_banner_remove, :remove_profile_banner
341
+
342
+ # Updates the authenticating user's profile banner image
343
+ #
344
+ # @see https://dev.twitter.com/docs/api/1.1/post/account/update_profile_banner
345
+ # @note Uploads a profile banner on behalf of the authenticating user. For best results, upload an <5MB image that is exactly 1252px by 626px. Images will be resized for a number of display options. Users with an uploaded profile banner will have a profile_banner_url node in their Users objects. More information about sizing variations can be found in User Profile Images and Banners.
346
+ # @note Profile banner images are processed asynchronously. The profile_banner_url and its variant sizes will not necessary be available directly after upload.
347
+ # @rate_limited No
348
+ # @authentication Requires user context
349
+ # @raise [Twitter::Error::BadRequest] Error raised when either an image was not provided or the image data could not be processed.
350
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
351
+ # @raise [Twitter::Error::UnprocessableEntity] Error raised when the image could not be resized or is too large.
352
+ # @return [nil]
353
+ # @param banner [File] The Base64-encoded or raw image data being uploaded as the user's new profile banner.
354
+ # @param options [Hash] A customizable set of options.
355
+ # @option options [Integer] :width The width of the preferred section of the image being uploaded in pixels. Use with height, offset_left, and offset_top to select the desired region of the image to use.
356
+ # @option options [Integer] :height The height of the preferred section of the image being uploaded in pixels. Use with width, offset_left, and offset_top to select the desired region of the image to use.
357
+ # @option options [Integer] :offset_left The number of pixels by which to offset the uploaded image from the left. Use with height, width, and offset_top to select the desired region of the image to use.
358
+ # @option options [Integer] :offset_top The number of pixels by which to offset the uploaded image from the top. Use with height, width, and offset_left to select the desired region of the image to use.
359
+ def update_profile_banner(banner, options={})
360
+ post("/1.1/account/update_profile_banner.json", options.merge(:banner => banner))[:body]
361
+ end
362
+
363
+ # Returns the available size variations of the specified user's profile banner.
364
+ #
365
+ # @see https://dev.twitter.com/docs/api/1.1/get/users/profile_banner
366
+ # @note If the user has not uploaded a profile banner, a HTTP 404 will be served instead.
367
+ # @rate_limited Yes
368
+ # @authentication Requires user context
369
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
370
+ # @return [Twitter::ProfileBanner]
371
+ # @overload profile_banner(options={})
372
+ # @overload profile_banner(user, options={})
373
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
374
+ def profile_banner(*args)
375
+ arguments = Twitter::Arguments.new(args)
376
+ merge_user!(arguments.options, arguments.pop || screen_name) unless arguments.options[:user_id] || arguments.options[:screen_name]
377
+ object_from_response(Twitter::ProfileBanner, :get, "/1.1/users/profile_banner.json", arguments.options)
378
+ end
379
+
380
+ end
381
+ end
382
+ end
383
+ end
@@ -0,0 +1,246 @@
1
+ require 'twitter/arguments'
2
+ require 'twitter/cursor'
3
+ require 'twitter/user'
4
+ require 'uri'
5
+
6
+ module Twitter
7
+ module REST
8
+ module API
9
+ module Utils
10
+ DEFAULT_CURSOR = -1
11
+ URI_SUBSTRING = "://"
12
+
13
+ class << self
14
+
15
+ def included(base)
16
+ base.extend(ClassMethods)
17
+ end
18
+
19
+ end
20
+
21
+ module ClassMethods
22
+
23
+ private
24
+
25
+ def deprecate_alias(new_name, old_name)
26
+ define_method(new_name) do |*args, &block|
27
+ warn "#{Kernel.caller.first}: [DEPRECATION] ##{new_name} it deprecated. Use ##{old_name} instead."
28
+ send(old_name, *args, &block)
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ private
35
+
36
+ # Take a URI string or Twitter::Identity object and return its ID
37
+ #
38
+ # @param object [Integer, String, URI, Twitter::Identity] An ID, URI, or object.
39
+ # @return [Integer]
40
+ def extract_id(object)
41
+ case object
42
+ when ::Integer
43
+ object
44
+ when ::String
45
+ object.split("/").last.to_i
46
+ when URI
47
+ object.path.split("/").last.to_i
48
+ when Twitter::Identity
49
+ object.id
50
+ end
51
+ end
52
+
53
+ # @param request_method [Symbol]
54
+ # @param path [String]
55
+ # @param args [Array]
56
+ # @return [Array<Twitter::User>]
57
+ def threaded_user_objects_from_response(request_method, path, args)
58
+ arguments = Twitter::Arguments.new(args)
59
+ arguments.flatten.threaded_map do |user|
60
+ object_from_response(Twitter::User, request_method, path, merge_user(arguments.options, user))
61
+ end
62
+ end
63
+
64
+ # @param request_method [Symbol]
65
+ # @param path [String]
66
+ # @param args [Array]
67
+ # @return [Array<Twitter::User>]
68
+ def user_objects_from_response(request_method, path, args)
69
+ arguments = Twitter::Arguments.new(args)
70
+ merge_user!(arguments.options, arguments.pop || screen_name) unless arguments.options[:user_id] || arguments.options[:screen_name]
71
+ objects_from_response(Twitter::User, request_method, path, arguments.options)
72
+ end
73
+
74
+ # @param klass [Class]
75
+ # @param request_method [Symbol]
76
+ # @param path [String]
77
+ # @param args [Array]
78
+ # @return [Array]
79
+ def objects_from_response_with_user(klass, request_method, path, args)
80
+ arguments = Twitter::Arguments.new(args)
81
+ merge_user!(arguments.options, arguments.pop)
82
+ objects_from_response(klass, request_method, path, arguments.options)
83
+ end
84
+
85
+ # @param klass [Class]
86
+ # @param request_method [Symbol]
87
+ # @param path [String]
88
+ # @param options [Hash]
89
+ # @return [Array]
90
+ def objects_from_response(klass, request_method, path, options={})
91
+ response = send(request_method.to_sym, path, options)[:body]
92
+ objects_from_array(klass, response)
93
+ end
94
+
95
+ # @param klass [Class]
96
+ # @param array [Array]
97
+ # @return [Array]
98
+ def objects_from_array(klass, array)
99
+ array.map do |element|
100
+ klass.new(element)
101
+ end
102
+ end
103
+
104
+ # @param klass [Class]
105
+ # @param request_method [Symbol]
106
+ # @param path [String]
107
+ # @param args [Array]
108
+ # @return [Array]
109
+ def threaded_objects_from_response(klass, request_method, path, args)
110
+ arguments = Twitter::Arguments.new(args)
111
+ arguments.flatten.threaded_map do |object|
112
+ id = extract_id(object)
113
+ object_from_response(klass, request_method, path, arguments.options.merge(:id => id))
114
+ end
115
+ end
116
+
117
+ # @param klass [Class]
118
+ # @param request_method [Symbol]
119
+ # @param path [String]
120
+ # @param options [Hash]
121
+ # @return [Object]
122
+ def object_from_response(klass, request_method, path, options={})
123
+ response = send(request_method.to_sym, path, options)
124
+ klass.from_response(response)
125
+ end
126
+
127
+ # @param collection_name [Symbol]
128
+ # @param klass [Class]
129
+ # @param request_method [Symbol]
130
+ # @param path [String]
131
+ # @param args [Array]
132
+ # @return [Twitter::Cursor]
133
+ def cursor_from_response_with_user(collection_name, klass, request_method, path, args)
134
+ arguments = Twitter::Arguments.new(args)
135
+ merge_user!(arguments.options, arguments.pop || screen_name) unless arguments.options[:user_id] || arguments.options[:screen_name]
136
+ cursor_from_response(collection_name, klass, request_method, path, arguments.options)
137
+ end
138
+
139
+ # @param collection_name [Symbol]
140
+ # @param klass [Class]
141
+ # @param request_method [Symbol]
142
+ # @param path [String]
143
+ # @param options [Hash]
144
+ # @return [Twitter::Cursor]
145
+ def cursor_from_response(collection_name, klass, request_method, path, options)
146
+ merge_default_cursor!(options)
147
+ response = send(request_method.to_sym, path, options)
148
+ Twitter::Cursor.from_response(response, collection_name.to_sym, klass, self, request_method, path, options)
149
+ end
150
+
151
+ def handle_forbidden_error(klass, error)
152
+ error = error.message == klass::MESSAGE ? klass.new : error
153
+ raise error
154
+ end
155
+
156
+ def merge_default_cursor!(options)
157
+ options[:cursor] = DEFAULT_CURSOR unless options[:cursor]
158
+ end
159
+
160
+ def screen_name
161
+ @screen_name ||= verify_credentials.screen_name
162
+ end
163
+
164
+ # Take a user and merge it into the hash with the correct key
165
+ #
166
+ # @param hash [Hash]
167
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
168
+ # @return [Hash]
169
+ def merge_user(hash, user, prefix=nil)
170
+ merge_user!(hash.dup, user, prefix)
171
+ end
172
+
173
+ # Take a user and merge it into the hash with the correct key
174
+ #
175
+ # @param hash [Hash]
176
+ # @param user [Integer, String, URI, Twitter::User] A Twitter user ID, screen name, URI, or object.
177
+ # @return [Hash]
178
+ def merge_user!(hash, user, prefix=nil)
179
+ case user
180
+ when Integer
181
+ set_compound_key("user_id", user, hash, prefix)
182
+ when String
183
+ if user[URI_SUBSTRING]
184
+ set_compound_key("screen_name", user.split("/").last, hash, prefix)
185
+ else
186
+ set_compound_key("screen_name", user, hash, prefix)
187
+ end
188
+ when URI
189
+ set_compound_key("screen_name", user.path.split("/").last, hash, prefix)
190
+ when Twitter::User
191
+ set_compound_key("user_id", user.id, hash, prefix)
192
+ end
193
+ end
194
+
195
+ def set_compound_key(key, value, hash, prefix=nil)
196
+ compound_key = [prefix, key].compact.join("_").to_sym
197
+ hash[compound_key] = value
198
+ hash
199
+ end
200
+
201
+ # Take a multiple users and merge them into the hash with the correct keys
202
+ #
203
+ # @param hash [Hash]
204
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen_names, or objects.
205
+ # @return [Hash]
206
+ def merge_users(hash, users)
207
+ merge_users!(hash.dup, users)
208
+ end
209
+
210
+ # Take a multiple users and merge them into the hash with the correct keys
211
+ #
212
+ # @param hash [Hash]
213
+ # @param users [Enumerable<Integer, String, URI, Twitter::User>] A collection of Twitter user IDs, screen_names, URIs, or objects.
214
+ # @return [Hash]
215
+ def merge_users!(hash, users)
216
+ user_ids, screen_names = collect_user_ids_and_screen_names(users)
217
+ hash[:user_id] = user_ids.join(',') unless user_ids.empty?
218
+ hash[:screen_name] = screen_names.join(',') unless screen_names.empty?
219
+ hash
220
+ end
221
+
222
+ def collect_user_ids_and_screen_names(users)
223
+ user_ids, screen_names = [], []
224
+ users.flatten.each do |user|
225
+ case user
226
+ when Integer
227
+ user_ids << user
228
+ when String
229
+ if user[URI_SUBSTRING]
230
+ screen_names << user.split("/").last
231
+ else
232
+ screen_names << user
233
+ end
234
+ when URI
235
+ screen_names << user.path.split("/").last
236
+ when Twitter::User
237
+ user_ids << user.id
238
+ end
239
+ end
240
+ [user_ids, screen_names]
241
+ end
242
+
243
+ end
244
+ end
245
+ end
246
+ end