twitter 4.8.1 → 5.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (197) hide show
  1. data.tar.gz.sig +0 -0
  2. data/CHANGELOG.md +30 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +386 -266
  5. data/lib/twitter.rb +4 -39
  6. data/lib/twitter/arguments.rb +11 -0
  7. data/lib/twitter/base.rb +89 -68
  8. data/lib/twitter/client.rb +69 -110
  9. data/lib/twitter/configuration.rb +7 -3
  10. data/lib/twitter/creatable.rb +2 -4
  11. data/lib/twitter/cursor.rb +50 -42
  12. data/lib/twitter/direct_message.rb +2 -11
  13. data/lib/twitter/entity/uri.rb +13 -0
  14. data/lib/twitter/enumerable.rb +15 -0
  15. data/lib/twitter/error.rb +55 -7
  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/factory.rb +2 -8
  31. data/lib/twitter/geo_factory.rb +2 -2
  32. data/lib/twitter/geo_results.rb +36 -0
  33. data/lib/twitter/identity.rb +0 -22
  34. data/lib/twitter/list.rb +18 -4
  35. data/lib/twitter/media/photo.rb +3 -3
  36. data/lib/twitter/media_factory.rb +2 -2
  37. data/lib/twitter/null_object.rb +24 -0
  38. data/lib/twitter/oembed.rb +3 -2
  39. data/lib/twitter/place.rb +15 -9
  40. data/lib/twitter/profile_banner.rb +5 -3
  41. data/lib/twitter/rate_limit.rb +1 -17
  42. data/lib/twitter/relationship.rb +2 -10
  43. data/lib/twitter/rest/api/direct_messages.rb +135 -0
  44. data/lib/twitter/rest/api/favorites.rb +120 -0
  45. data/lib/twitter/rest/api/friends_and_followers.rb +290 -0
  46. data/lib/twitter/rest/api/help.rb +58 -0
  47. data/lib/twitter/rest/api/lists.rb +491 -0
  48. data/lib/twitter/rest/api/oauth.rb +45 -0
  49. data/lib/twitter/rest/api/places_and_geo.rb +104 -0
  50. data/lib/twitter/rest/api/saved_searches.rb +91 -0
  51. data/lib/twitter/rest/api/search.rb +37 -0
  52. data/lib/twitter/rest/api/spam_reporting.rb +29 -0
  53. data/lib/twitter/rest/api/suggested_users.rb +51 -0
  54. data/lib/twitter/rest/api/timelines.rb +202 -0
  55. data/lib/twitter/rest/api/trends.rb +58 -0
  56. data/lib/twitter/rest/api/tweets.rb +293 -0
  57. data/lib/twitter/rest/api/undocumented.rb +52 -0
  58. data/lib/twitter/rest/api/users.rb +383 -0
  59. data/lib/twitter/rest/api/utils.rb +219 -0
  60. data/lib/twitter/rest/client.rb +193 -0
  61. data/lib/twitter/rest/request/multipart_with_file.rb +36 -0
  62. data/lib/twitter/rest/response/parse_json.rb +27 -0
  63. data/lib/twitter/{response → rest/response}/raise_error.rb +8 -11
  64. data/lib/twitter/search_results.rb +33 -21
  65. data/lib/twitter/settings.rb +1 -6
  66. data/lib/twitter/size.rb +1 -1
  67. data/lib/twitter/streaming/client.rb +77 -0
  68. data/lib/twitter/streaming/connection.rb +22 -0
  69. data/lib/twitter/streaming/response.rb +30 -0
  70. data/lib/twitter/suggestion.rb +4 -2
  71. data/lib/twitter/token.rb +8 -0
  72. data/lib/twitter/trend.rb +2 -1
  73. data/lib/twitter/trend_results.rb +59 -0
  74. data/lib/twitter/tweet.rb +41 -85
  75. data/lib/twitter/user.rb +51 -41
  76. data/lib/twitter/version.rb +4 -4
  77. data/spec/fixtures/already_posted.json +1 -0
  78. data/spec/fixtures/ids_list.json +1 -1
  79. data/spec/fixtures/ids_list2.json +1 -1
  80. data/spec/fixtures/search.json +1 -1
  81. data/spec/fixtures/search_malformed.json +1 -1
  82. data/spec/fixtures/track_streaming.json +3 -0
  83. data/spec/helper.rb +8 -13
  84. data/spec/twitter/base_spec.rb +25 -99
  85. data/spec/twitter/configuration_spec.rb +1 -1
  86. data/spec/twitter/cursor_spec.rb +13 -31
  87. data/spec/twitter/direct_message_spec.rb +41 -8
  88. data/spec/twitter/entity/uri_spec.rb +74 -0
  89. data/spec/twitter/error_spec.rb +59 -11
  90. data/spec/twitter/geo/point_spec.rb +1 -1
  91. data/spec/twitter/geo_factory_spec.rb +3 -3
  92. data/spec/twitter/geo_results_spec.rb +35 -0
  93. data/spec/twitter/identifiable_spec.rb +0 -21
  94. data/spec/twitter/list_spec.rb +51 -8
  95. data/spec/twitter/media/photo_spec.rb +118 -3
  96. data/spec/twitter/media_factory_spec.rb +2 -2
  97. data/spec/twitter/null_object_spec.rb +26 -0
  98. data/spec/twitter/oembed_spec.rb +69 -45
  99. data/spec/twitter/place_spec.rb +68 -12
  100. data/spec/twitter/profile_banner_spec.rb +1 -1
  101. data/spec/twitter/rate_limit_spec.rb +12 -12
  102. data/spec/twitter/relationship_spec.rb +31 -9
  103. data/spec/twitter/{api → rest/api}/direct_messages_spec.rb +22 -9
  104. data/spec/twitter/{api → rest/api}/favorites_spec.rb +80 -7
  105. data/spec/twitter/{api → rest/api}/friends_and_followers_spec.rb +104 -65
  106. data/spec/twitter/{api → rest/api}/geo_spec.rb +10 -10
  107. data/spec/twitter/{api → rest/api}/help_spec.rb +6 -6
  108. data/spec/twitter/{api → rest/api}/lists_spec.rb +77 -56
  109. data/spec/twitter/{api → rest/api}/oauth_spec.rb +6 -6
  110. data/spec/twitter/{api → rest/api}/saved_searches_spec.rb +7 -7
  111. data/spec/twitter/{api → rest/api}/search_spec.rb +8 -9
  112. data/spec/twitter/{api → rest/api}/spam_reporting_spec.rb +3 -3
  113. data/spec/twitter/{api → rest/api}/suggested_users_spec.rb +5 -5
  114. data/spec/twitter/{api → rest/api}/timelines_spec.rb +9 -9
  115. data/spec/twitter/{api → rest/api}/trends_spec.rb +6 -6
  116. data/spec/twitter/rest/api/tweets_spec.rb +503 -0
  117. data/spec/twitter/{api → rest/api}/undocumented_spec.rb +19 -45
  118. data/spec/twitter/{api → rest/api}/users_spec.rb +60 -35
  119. data/spec/twitter/rest/client_spec.rb +193 -0
  120. data/spec/twitter/saved_search_spec.rb +11 -0
  121. data/spec/twitter/search_results_spec.rb +29 -42
  122. data/spec/twitter/settings_spec.rb +17 -6
  123. data/spec/twitter/streaming/client_spec.rb +75 -0
  124. data/spec/twitter/token_spec.rb +16 -0
  125. data/spec/twitter/trend_results_spec.rb +89 -0
  126. data/spec/twitter/trend_spec.rb +23 -0
  127. data/spec/twitter/tweet_spec.rb +122 -115
  128. data/spec/twitter/user_spec.rb +136 -77
  129. data/spec/twitter_spec.rb +0 -119
  130. data/twitter.gemspec +8 -5
  131. metadata +148 -141
  132. metadata.gz.sig +0 -0
  133. data/lib/twitter/action/favorite.rb +0 -19
  134. data/lib/twitter/action/follow.rb +0 -30
  135. data/lib/twitter/action/list_member_added.rb +0 -39
  136. data/lib/twitter/action/mention.rb +0 -46
  137. data/lib/twitter/action/reply.rb +0 -27
  138. data/lib/twitter/action/retweet.rb +0 -27
  139. data/lib/twitter/action/tweet.rb +0 -20
  140. data/lib/twitter/action_factory.rb +0 -22
  141. data/lib/twitter/api/arguments.rb +0 -13
  142. data/lib/twitter/api/direct_messages.rb +0 -148
  143. data/lib/twitter/api/favorites.rb +0 -126
  144. data/lib/twitter/api/friends_and_followers.rb +0 -334
  145. data/lib/twitter/api/help.rb +0 -64
  146. data/lib/twitter/api/lists.rb +0 -618
  147. data/lib/twitter/api/oauth.rb +0 -44
  148. data/lib/twitter/api/places_and_geo.rb +0 -121
  149. data/lib/twitter/api/saved_searches.rb +0 -99
  150. data/lib/twitter/api/search.rb +0 -37
  151. data/lib/twitter/api/spam_reporting.rb +0 -30
  152. data/lib/twitter/api/suggested_users.rb +0 -55
  153. data/lib/twitter/api/timelines.rb +0 -214
  154. data/lib/twitter/api/trends.rb +0 -63
  155. data/lib/twitter/api/tweets.rb +0 -304
  156. data/lib/twitter/api/undocumented.rb +0 -97
  157. data/lib/twitter/api/users.rb +0 -439
  158. data/lib/twitter/api/utils.rb +0 -187
  159. data/lib/twitter/configurable.rb +0 -96
  160. data/lib/twitter/default.rb +0 -102
  161. data/lib/twitter/entity/url.rb +0 -9
  162. data/lib/twitter/error/client_error.rb +0 -35
  163. data/lib/twitter/error/decode_error.rb +0 -9
  164. data/lib/twitter/error/identity_map_key_error.rb +0 -9
  165. data/lib/twitter/error/server_error.rb +0 -28
  166. data/lib/twitter/exceptable.rb +0 -36
  167. data/lib/twitter/identity_map.rb +0 -22
  168. data/lib/twitter/request/multipart_with_file.rb +0 -34
  169. data/lib/twitter/response/parse_json.rb +0 -25
  170. data/spec/fixtures/about_me.json +0 -1
  171. data/spec/fixtures/activity_summary.json +0 -1
  172. data/spec/fixtures/bad_gateway.json +0 -1
  173. data/spec/fixtures/bad_request.json +0 -1
  174. data/spec/fixtures/by_friends.json +0 -1
  175. data/spec/fixtures/end_session.json +0 -1
  176. data/spec/fixtures/forbidden.json +0 -1
  177. data/spec/fixtures/internal_server_error.json +0 -1
  178. data/spec/fixtures/not_acceptable.json +0 -1
  179. data/spec/fixtures/phoenix_search.phoenix +0 -1
  180. data/spec/fixtures/resolve.json +0 -1
  181. data/spec/fixtures/service_unavailable.json +0 -1
  182. data/spec/fixtures/totals.json +0 -1
  183. data/spec/fixtures/trends.json +0 -1
  184. data/spec/fixtures/unauthorized.json +0 -1
  185. data/spec/fixtures/video_facets.json +0 -1
  186. data/spec/twitter/action/favorite_spec.rb +0 -29
  187. data/spec/twitter/action/follow_spec.rb +0 -29
  188. data/spec/twitter/action/list_member_added_spec.rb +0 -41
  189. data/spec/twitter/action/mention_spec.rb +0 -52
  190. data/spec/twitter/action/reply_spec.rb +0 -41
  191. data/spec/twitter/action/retweet_spec.rb +0 -41
  192. data/spec/twitter/action_factory_spec.rb +0 -35
  193. data/spec/twitter/action_spec.rb +0 -16
  194. data/spec/twitter/api/tweets_spec.rb +0 -285
  195. data/spec/twitter/client_spec.rb +0 -223
  196. data/spec/twitter/error/client_error_spec.rb +0 -23
  197. data/spec/twitter/error/server_error_spec.rb +0 -20
@@ -1,439 +0,0 @@
1
- require 'twitter/api/arguments'
2
- require 'twitter/api/utils'
3
- require 'twitter/core_ext/enumerable'
4
- require 'twitter/error/not_found'
5
- require 'twitter/profile_banner'
6
- require 'twitter/settings'
7
- require 'twitter/user'
8
-
9
- module Twitter
10
- module API
11
- module Users
12
- include Twitter::API::Utils
13
- MAX_USERS_PER_REQUEST = 100
14
-
15
- # Updates the authenticating user's settings.
16
- # Or, if no options supplied, returns settings (including current trend, geo and sleep time information) for the authenticating user.
17
- #
18
- # @see https://dev.twitter.com/docs/api/1.1/post/account/settings
19
- # @see https://dev.twitter.com/docs/api/1.1/get/account/settings
20
- # @rate_limited Yes
21
- # @authentication Requires user context
22
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
23
- # @return [Twitter::Settings]
24
- # @param options [Hash] A customizable set of options.
25
- # @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}.
26
- # @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.
27
- # @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.
28
- # @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.
29
- # @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.
30
- # @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}.
31
- # @example Return the settings for the authenticating user.
32
- # Twitter.settings
33
- def settings(options={})
34
- request_method = options.size.zero? ? :get : :post
35
- object_from_response(Twitter::Settings, request_method, "/1.1/account/settings.json", options)
36
- end
37
-
38
- # Returns the requesting user if authentication was successful, otherwise raises {Twitter::Error::Unauthorized}
39
- #
40
- # @see https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials
41
- # @rate_limited Yes
42
- # @authentication Requires user context
43
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
44
- # @return [Twitter::User] The authenticated user.
45
- # @param options [Hash] A customizable set of options.
46
- # @option options [Boolean, String, Integer] :skip_status Do not include user's Tweets when set to true, 't' or 1.
47
- # @example Return the requesting user if authentication was successful
48
- # Twitter.verify_credentials
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
- # @example Turn SMS updates on for the authenticating user
64
- # Twitter.update_delivery_device('sms')
65
- def update_delivery_device(device, options={})
66
- object_from_response(Twitter::User, :post, "/1.1/account/update_delivery_device.json", options.merge(:device => device))
67
- end
68
-
69
- # Sets values that users are able to set under the "Account" tab of their settings page
70
- #
71
- # @see https://dev.twitter.com/docs/api/1.1/post/account/update_profile
72
- # @note Only the options specified will be updated.
73
- # @rate_limited No
74
- # @authentication Requires user context
75
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
76
- # @return [Twitter::User] The authenticated user.
77
- # @param options [Hash] A customizable set of options.
78
- # @option options [String] :name Full name associated with the profile. Maximum of 20 characters.
79
- # @option options [String] :url URL associated with the profile. Will be prepended with "http://" if not present. Maximum of 100 characters.
80
- # @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.
81
- # @option options [String] :description A description of the user owning the account. Maximum of 160 characters.
82
- # @example Set authenticating user's name to Erik Michaels-Ober
83
- # Twitter.update_profile(:name => "Erik Michaels-Ober")
84
- def update_profile(options={})
85
- object_from_response(Twitter::User, :post, "/1.1/account/update_profile.json", options)
86
- end
87
-
88
- # Updates the authenticating user's profile background image
89
- #
90
- # @see https://dev.twitter.com/docs/api/1.1/post/account/update_profile_background_image
91
- # @rate_limited No
92
- # @authentication Requires user context
93
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
94
- # @return [Twitter::User] The authenticated user.
95
- # @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.
96
- # @param options [Hash] A customizable set of options.
97
- # @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.
98
- # @example Update the authenticating user's profile background image
99
- # Twitter.update_profile_background_image(File.new("we_concept_bg2.png"))
100
- def update_profile_background_image(image, options={})
101
- object_from_response(Twitter::User, :post, "/1.1/account/update_profile_background_image.json", options.merge(:image => image))
102
- end
103
-
104
- # Sets one or more hex values that control the color scheme of the authenticating user's profile
105
- #
106
- # @see https://dev.twitter.com/docs/api/1.1/post/account/update_profile_colors
107
- # @rate_limited No
108
- # @authentication Requires user context
109
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
110
- # @return [Twitter::User] The authenticated user.
111
- # @param options [Hash] A customizable set of options.
112
- # @option options [String] :profile_background_color Profile background color.
113
- # @option options [String] :profile_text_color Profile text color.
114
- # @option options [String] :profile_link_color Profile link color.
115
- # @option options [String] :profile_sidebar_fill_color Profile sidebar's background color.
116
- # @option options [String] :profile_sidebar_border_color Profile sidebar's border color.
117
- # @example Set authenticating user's profile background to black
118
- # Twitter.update_profile_colors(:profile_background_color => '000000')
119
- def update_profile_colors(options={})
120
- object_from_response(Twitter::User, :post, "/1.1/account/update_profile_colors.json", options)
121
- end
122
-
123
- # Updates the authenticating user's profile image
124
- #
125
- # @see https://dev.twitter.com/docs/api/1.1/post/account/update_profile_image
126
- # @note Updates the authenticating user's profile image. Note that this method expects raw multipart data, not a URL to an image.
127
- # @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.
128
- # @rate_limited No
129
- # @authentication Requires user context
130
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
131
- # @return [Twitter::User] The authenticated user.
132
- # @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.
133
- # @param options [Hash] A customizable set of options.
134
- # @example Update the authenticating user's profile image
135
- # Twitter.update_profile_image(File.new("me.jpeg"))
136
- def update_profile_image(image, options={})
137
- object_from_response(Twitter::User, :post, "/1.1/account/update_profile_image.json", options.merge(:image => image))
138
- end
139
-
140
- # Returns an array of user objects that the authenticating user is blocking
141
- #
142
- # @see https://dev.twitter.com/docs/api/1.1/get/blocks/list
143
- # @rate_limited Yes
144
- # @authentication Requires user context
145
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
146
- # @return [Array<Twitter::User>] User objects that the authenticating user is blocking.
147
- # @param options [Hash] A customizable set of options.
148
- # @option options [Integer] :page Specifies the page of results to retrieve.
149
- # @example Return an array of user objects that the authenticating user is blocking
150
- # Twitter.blocking
151
- def blocking(options={})
152
- cursor_from_response(:users, Twitter::User, :get, "/1.1/blocks/list.json", options, :blocking)
153
- end
154
-
155
- # Returns an array of numeric user ids the authenticating user is blocking
156
- #
157
- # @see https://dev.twitter.com/docs/api/1.1/get/blocks/ids
158
- # @rate_limited Yes
159
- # @authentication Requires user context
160
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
161
- # @return [Array] Numeric user ids the authenticating user is blocking.
162
- # @example Return an array of numeric user ids the authenticating user is blocking
163
- # Twitter.blocking_ids
164
- # @overload block(options={})
165
- # @param options [Hash] A customizable set of options.
166
- def blocked_ids(*args)
167
- arguments = Twitter::API::Arguments.new(args)
168
- merge_user!(arguments.options, arguments.pop)
169
- cursor_from_response(:ids, nil, :get, "/1.1/blocks/ids.json", arguments.options, :blocked_ids)
170
- end
171
-
172
- # Returns true if the authenticating user is blocking a target user
173
- #
174
- # @see https://dev.twitter.com/docs/api/1.1/get/blocks/ids
175
- # @rate_limited Yes
176
- # @authentication Requires user context
177
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
178
- # @return [Boolean] true if the authenticating user is blocking the target user, otherwise false.
179
- # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
180
- # @param options [Hash] A customizable set of options.
181
- # @example Check whether the authenticating user is blocking @sferik
182
- # Twitter.block?('sferik')
183
- # Twitter.block?(7505382) # Same as above
184
- def block?(user, options={})
185
- merge_default_cursor!(options)
186
- user_id = case user
187
- when Integer
188
- user
189
- when String
190
- user(user).id
191
- when Twitter::User
192
- user.id
193
- end
194
- blocked_ids(options).all.map(&:to_i).include?(user_id)
195
- end
196
-
197
- # Blocks the users specified by the authenticating user
198
- #
199
- # @see https://dev.twitter.com/docs/api/1.1/post/blocks/create
200
- # @note Destroys a friendship to the blocked user if it exists.
201
- # @rate_limited Yes
202
- # @authentication Requires user context
203
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
204
- # @return [Array<Twitter::User>] The blocked users.
205
- # @overload block(*users)
206
- # @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen names, or objects.
207
- # @example Block and unfriend @sferik as the authenticating user
208
- # Twitter.block('sferik')
209
- # Twitter.block(7505382) # Same as above
210
- # @overload block(*users, options)
211
- # @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen names, or objects.
212
- # @param options [Hash] A customizable set of options.
213
- def block(*args)
214
- threaded_user_objects_from_response(:post, "/1.1/blocks/create.json", args)
215
- end
216
-
217
- # Un-blocks the users specified by the authenticating user
218
- #
219
- # @see https://dev.twitter.com/docs/api/1.1/post/blocks/destroy
220
- # @rate_limited No
221
- # @authentication Requires user context
222
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
223
- # @return [Array<Twitter::User>] The un-blocked users.
224
- # @overload unblock(*users)
225
- # @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen names, or objects.
226
- # @example Un-block @sferik as the authenticating user
227
- # Twitter.unblock('sferik')
228
- # Twitter.unblock(7505382) # Same as above
229
- # @overload unblock(*users, options)
230
- # @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen names, or objects.
231
- # @param options [Hash] A customizable set of options.
232
- def unblock(*args)
233
- threaded_user_objects_from_response(:post, "/1.1/blocks/destroy.json", args)
234
- end
235
-
236
- # Returns extended information for up to 100 users
237
- #
238
- # @see https://dev.twitter.com/docs/api/1.1/get/users/lookup
239
- # @rate_limited Yes
240
- # @authentication Requires user context
241
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
242
- # @return [Array<Twitter::User>] The requested users.
243
- # @overload users(*users)
244
- # @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen names, or objects.
245
- # @example Return extended information for @sferik and @pengwynn
246
- # Twitter.users('sferik', 'pengwynn') # Retrieve users with a POST request using screen_names
247
- # Twitter.users(7505382, 14100886) # Same as above using twitter_ids
248
- # @overload users(*users, options)
249
- # @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen names, or objects.
250
- # @param options [Hash] A customizable set of options.
251
- # @option options [Symbol, String] :method Requests users via a GET request instead of the standard POST request if set to ':get'.
252
- # @option options [Boolean] :include_entities The tweet entities node will be disincluded when set to false.
253
- # @example Return extended information for @sferik and @pengwynn
254
- # Twitter.users('sferik', 'pengwynn', :method => :get) # Retrieve users with a GET request
255
- # Twitter.users(7505382, 14100886, :method => :get) # Same as above
256
- def users(*args)
257
- arguments = Twitter::API::Arguments.new(args)
258
- method = arguments.options.delete(:method) || :post
259
- arguments.flatten.each_slice(MAX_USERS_PER_REQUEST).threaded_map do |users|
260
- objects_from_response(Twitter::User, method, "/1.1/users/lookup.json", merge_users(arguments.options, users))
261
- end.flatten
262
- end
263
-
264
- # @see https://dev.twitter.com/docs/api/1.1/get/users/show
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 [Twitter::User] The requested user.
269
- # @overload user(options={})
270
- # Returns extended information for the authenticated user
271
- #
272
- # @param options [Hash] A customizable set of options.
273
- # @option options [Boolean] :include_entities The tweet entities node will be disincluded when set to false.
274
- # @option options [Boolean, String, Integer] :skip_status Do not include user's Tweets when set to true, 't' or 1.
275
- # @example Return extended information for the authenticated user
276
- # Twitter.user
277
- # @overload user(user, options={})
278
- # Returns extended information for a given user
279
- #
280
- # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
281
- # @param options [Hash] A customizable set of options.
282
- # @option options [Boolean] :include_entities The tweet entities node will be disincluded when set to false.
283
- # @option options [Boolean, String, Integer] :skip_status Do not include user's Tweets when set to true, 't' or 1.
284
- # @example Return extended information for @sferik
285
- # Twitter.user('sferik')
286
- # Twitter.user(7505382) # Same as above
287
- def user(*args)
288
- arguments = Twitter::API::Arguments.new(args)
289
- if user = arguments.pop
290
- merge_user!(arguments.options, user)
291
- object_from_response(Twitter::User, :get, "/1.1/users/show.json", arguments.options)
292
- else
293
- verify_credentials(arguments.options)
294
- end
295
- end
296
-
297
- # Returns true if the specified user exists
298
- #
299
- # @rate_limited Yes
300
- # @authentication Requires user context
301
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
302
- # @return [Boolean] true if the user exists, otherwise false.
303
- # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
304
- # @example Return true if @sferik exists
305
- # Twitter.user?('sferik')
306
- # Twitter.user?(7505382) # Same as above
307
- def user?(user, options={})
308
- merge_user!(options, user)
309
- get("/1.1/users/show.json", options)
310
- true
311
- rescue Twitter::Error::NotFound
312
- false
313
- end
314
-
315
- # Returns users that match the given query
316
- #
317
- # @see https://dev.twitter.com/docs/api/1.1/get/users/search
318
- # @rate_limited Yes
319
- # @authentication Requires user context
320
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
321
- # @return [Array<Twitter::User>]
322
- # @param query [String] The search query to run against people search.
323
- # @param options [Hash] A customizable set of options.
324
- # @option options [Integer] :count The number of people to retrieve. Maxiumum of 20 allowed per page.
325
- # @option options [Integer] :page Specifies the page of results to retrieve.
326
- # @example Return users that match "Erik Michaels-Ober"
327
- # Twitter.user_search("Erik Michaels-Ober")
328
- def user_search(query, options={})
329
- objects_from_response(Twitter::User, :get, "/1.1/users/search.json", options.merge(:q => query))
330
- end
331
-
332
- # Returns an array of users that the specified user can contribute to
333
- #
334
- # @see https://dev.twitter.com/docs/api/1.1/get/users/contributees
335
- # @rate_limited Yes
336
- # @authentication Requires user context
337
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
338
- # @return [Array<Twitter::User>]
339
- # @overload contributees(options={})
340
- # @param options [Hash] A customizable set of options.
341
- # @option options [Boolean, String, Integer] :skip_status Do not include contributee's Tweets when set to true, 't' or 1.
342
- # @example Return the authenticated user's contributees
343
- # Twitter.contributees
344
- # @overload contributees(user, options={})
345
- # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
346
- # @param options [Hash] A customizable set of options.
347
- # @option options [Boolean, String, Integer] :skip_status Do not include contributee's Tweets when set to true, 't' or 1.
348
- # @example Return users @sferik can contribute to
349
- # Twitter.contributees('sferik')
350
- # Twitter.contributees(7505382) # Same as above
351
- def contributees(*args)
352
- user_objects_from_response(:get, "/1.1/users/contributees.json", args)
353
- end
354
-
355
- # Returns an array of users who can contribute to the specified account
356
- #
357
- # @see https://dev.twitter.com/docs/api/1.1/get/users/contributors
358
- # @rate_limited Yes
359
- # @authentication Requires user context
360
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
361
- # @return [Array<Twitter::User>]
362
- # @overload contributors(options={})
363
- # @param options [Hash] A customizable set of options.
364
- # @option options [Boolean, String, Integer] :skip_status Do not include contributee's Tweets when set to true, 't' or 1.
365
- # @example Return the authenticated user's contributors
366
- # Twitter.contributors
367
- # @overload contributors(user, options={})
368
- # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
369
- # @param options [Hash] A customizable set of options.
370
- # @option options [Boolean, String, Integer] :skip_status Do not include contributee's Tweets when set to true, 't' or 1.
371
- # @example Return users who can contribute to @sferik's account
372
- # Twitter.contributors('sferik')
373
- # Twitter.contributors(7505382) # Same as above
374
- def contributors(*args)
375
- user_objects_from_response(:get, "/1.1/users/contributors.json", args)
376
- end
377
-
378
- # Removes the authenticating user's profile banner image
379
- #
380
- # @see https://dev.twitter.com/docs/api/1.1/post/account/remove_profile_banner
381
- # @rate_limited No
382
- # @authentication Requires user context
383
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
384
- # @return [nil]
385
- # @param options [Hash] A customizable set of options.
386
- # @example Remove the authenticating user's profile banner image
387
- # Twitter.remove_profile_banner
388
- def remove_profile_banner(options={})
389
- post("/1.1/account/remove_profile_banner.json", options)[:body]
390
- end
391
-
392
- # Updates the authenticating user's profile banner image
393
- #
394
- # @see https://dev.twitter.com/docs/api/1.1/post/account/update_profile_banner
395
- # @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.
396
- # @note Profile banner images are processed asynchronously. The profile_banner_url and its variant sizes will not necessary be available directly after upload.
397
- # @rate_limited No
398
- # @authentication Requires user context
399
- # @raise [Twitter::Error::BadRequest] Error raised when either an image was not provided or the image data could not be processed.
400
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
401
- # @raise [Twitter::Error::UnprocessableEntity] Error raised when the image could not be resized or is too large.
402
- # @return [nil]
403
- # @param banner [File] The Base64-encoded or raw image data being uploaded as the user's new profile banner.
404
- # @param options [Hash] A customizable set of options.
405
- # @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.
406
- # @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.
407
- # @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.
408
- # @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.
409
- # @example Update the authenticating user's profile banner
410
- # Twitter.update_profile_banner(File.new("me.jpeg"))
411
- def update_profile_banner(banner, options={})
412
- post("/1.1/account/update_profile_banner.json", options.merge(:banner => banner))[:body]
413
- end
414
-
415
- # Returns the available size variations of the specified user's profile banner.
416
- #
417
- # @see https://dev.twitter.com/docs/api/1.1/get/users/profile_banner
418
- # @note If the user has not uploaded a profile banner, a HTTP 404 will be served instead.
419
- # @rate_limited Yes
420
- # @authentication Requires user context
421
- # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
422
- # @return [Twitter::ProfileBanner]
423
- # @overload profile_banner(options={})
424
- # @example Return the authenticated user's profile banner
425
- # Twitter.profile_banner
426
- # @overload profile_banner(user, options={})
427
- # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
428
- # @example Return the specified user's profile banner
429
- # Twitter.profile_banner('sferik')
430
- # Twitter.profile_banner(7505382) # Same as above
431
- def profile_banner(*args)
432
- arguments = Twitter::API::Arguments.new(args)
433
- merge_user!(arguments.options, arguments.pop || screen_name) unless arguments.options[:user_id] || arguments.options[:screen_name]
434
- object_from_response(Twitter::ProfileBanner, :get, "/1.1/users/profile_banner.json", arguments.options)
435
- end
436
-
437
- end
438
- end
439
- end
@@ -1,187 +0,0 @@
1
- require 'twitter/api/arguments'
2
- require 'twitter/cursor'
3
- require 'twitter/user'
4
-
5
- module Twitter
6
- module API
7
- module Utils
8
-
9
- DEFAULT_CURSOR = -1
10
-
11
- private
12
-
13
- # @param request_method [Symbol]
14
- # @param path [String]
15
- # @param args [Array]
16
- # @return [Array<Twitter::User>]
17
- def threaded_user_objects_from_response(request_method, path, args)
18
- arguments = Twitter::API::Arguments.new(args)
19
- arguments.flatten.threaded_map do |user|
20
- object_from_response(Twitter::User, request_method, path, merge_user(arguments.options, user))
21
- end
22
- end
23
-
24
- # @param request_method [Symbol]
25
- # @param path [String]
26
- # @param args [Array]
27
- # @return [Array<Twitter::User>]
28
- def user_objects_from_response(request_method, path, args)
29
- arguments = Twitter::API::Arguments.new(args)
30
- merge_user!(arguments.options, arguments.pop || screen_name) unless arguments.options[:user_id] || arguments.options[:screen_name]
31
- objects_from_response(Twitter::User, request_method, path, arguments.options)
32
- end
33
-
34
- # @param klass [Class]
35
- # @param request_method [Symbol]
36
- # @param path [String]
37
- # @param args [Array]
38
- # @return [Array]
39
- def objects_from_response_with_user(klass, request_method, path, args)
40
- arguments = Twitter::API::Arguments.new(args)
41
- merge_user!(arguments.options, arguments.pop)
42
- objects_from_response(klass, request_method, path, arguments.options)
43
- end
44
-
45
- # @param klass [Class]
46
- # @param request_method [Symbol]
47
- # @param path [String]
48
- # @param options [Hash]
49
- # @return [Array]
50
- def objects_from_response(klass, request_method, path, options={})
51
- response = send(request_method.to_sym, path, options)[:body]
52
- objects_from_array(klass, response)
53
- end
54
-
55
- # @param klass [Class]
56
- # @param array [Array]
57
- # @return [Array]
58
- def objects_from_array(klass, array)
59
- array.map do |element|
60
- klass.fetch_or_new(element)
61
- end
62
- end
63
-
64
- # @param klass [Class]
65
- # @param request_method [Symbol]
66
- # @param path [String]
67
- # @param args [Array]
68
- # @return [Array]
69
- def threaded_object_from_response(klass, request_method, path, args)
70
- arguments = Twitter::API::Arguments.new(args)
71
- arguments.flatten.threaded_map do |id|
72
- object_from_response(klass, request_method, path, arguments.options.merge(:id => id))
73
- end
74
- end
75
-
76
- # @param klass [Class]
77
- # @param request_method [Symbol]
78
- # @param path [String]
79
- # @param options [Hash]
80
- # @return [Object]
81
- def object_from_response(klass, request_method, path, options={})
82
- response = send(request_method.to_sym, path, options)
83
- klass.from_response(response)
84
- end
85
-
86
- # @param collection_name [Symbol]
87
- # @param klass [Class]
88
- # @param request_method [Symbol]
89
- # @param path [String]
90
- # @param args [Array]
91
- # @param method_name [Symbol]
92
- # @return [Twitter::Cursor]
93
- def cursor_from_response_with_user(collection_name, klass, request_method, path, args, method_name)
94
- arguments = Twitter::API::Arguments.new(args)
95
- merge_user!(arguments.options, arguments.pop || screen_name) unless arguments.options[:user_id] || arguments.options[:screen_name]
96
- cursor_from_response(collection_name, klass, request_method, path, arguments.options, method_name)
97
- end
98
-
99
- # @param collection_name [Symbol]
100
- # @param klass [Class]
101
- # @param request_method [Symbol]
102
- # @param path [String]
103
- # @param options [Hash]
104
- # @param method_name [Symbol]
105
- # @return [Twitter::Cursor]
106
- def cursor_from_response(collection_name, klass, request_method, path, options, method_name)
107
- merge_default_cursor!(options)
108
- response = send(request_method.to_sym, path, options)
109
- Twitter::Cursor.from_response(response, collection_name.to_sym, klass, self, method_name, options)
110
- end
111
-
112
- def handle_forbidden_error(klass, error)
113
- if error.message == klass::MESSAGE
114
- raise klass.new
115
- else
116
- raise error
117
- end
118
- end
119
-
120
- def merge_default_cursor!(options)
121
- options[:cursor] = DEFAULT_CURSOR unless options[:cursor]
122
- end
123
-
124
- def screen_name
125
- @screen_name ||= verify_credentials.screen_name
126
- end
127
-
128
- # Take a user and merge it into the hash with the correct key
129
- #
130
- # @param hash [Hash]
131
- # @param user [Integer, String, Twitter::User] A Twitter user ID, screen_name, or object.
132
- # @return [Hash]
133
- def merge_user(hash, user, prefix=nil)
134
- merge_user!(hash.dup, user, prefix)
135
- end
136
-
137
- # Take a user and merge it into the hash with the correct key
138
- #
139
- # @param hash [Hash]
140
- # @param user [Integer, String, Twitter::User] A Twitter user ID, screen_name, or object.
141
- # @return [Hash]
142
- def merge_user!(hash, user, prefix=nil)
143
- case user
144
- when Integer
145
- hash[[prefix, "user_id"].compact.join("_").to_sym] = user
146
- when String
147
- hash[[prefix, "screen_name"].compact.join("_").to_sym] = user
148
- when Twitter::User
149
- hash[[prefix, "user_id"].compact.join("_").to_sym] = user.id
150
- end
151
- hash
152
- end
153
-
154
- # Take a multiple users and merge them into the hash with the correct keys
155
- #
156
- # @param hash [Hash]
157
- # @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen_names, or objects.
158
- # @return [Hash]
159
- def merge_users(hash, users)
160
- merge_users!(hash.dup, users)
161
- end
162
-
163
- # Take a multiple users and merge them into the hash with the correct keys
164
- #
165
- # @param hash [Hash]
166
- # @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen_names, or objects.
167
- # @return [Hash]
168
- def merge_users!(hash, users)
169
- user_ids, screen_names = [], []
170
- users.flatten.each do |user|
171
- case user
172
- when Integer
173
- user_ids << user
174
- when String
175
- screen_names << user
176
- when Twitter::User
177
- user_ids << user.id
178
- end
179
- end
180
- hash[:user_id] = user_ids.join(',') unless user_ids.empty?
181
- hash[:screen_name] = screen_names.join(',') unless screen_names.empty?
182
- hash
183
- end
184
-
185
- end
186
- end
187
- end