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,58 @@
1
+ require 'twitter/rest/api/utils'
2
+ require 'twitter/configuration'
3
+ require 'twitter/language'
4
+
5
+ module Twitter
6
+ module REST
7
+ module API
8
+ module Help
9
+ include Twitter::REST::API::Utils
10
+
11
+ # Returns the current configuration used by Twitter
12
+ #
13
+ # @see https://dev.twitter.com/docs/api/1.1/get/help/configuration
14
+ # @rate_limited Yes
15
+ # @authentication Requires user context
16
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
17
+ # @return [Twitter::Configuration] Twitter's configuration.
18
+ def configuration(options={})
19
+ object_from_response(Twitter::Configuration, :get, "/1.1/help/configuration.json", options)
20
+ end
21
+
22
+ # Returns the list of languages supported by Twitter
23
+ #
24
+ # @see https://dev.twitter.com/docs/api/1.1/get/help/languages
25
+ # @rate_limited Yes
26
+ # @authentication Requires user context
27
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
28
+ # @return [Array<Twitter::Language>]
29
+ def languages(options={})
30
+ objects_from_response(Twitter::Language, :get, "/1.1/help/languages.json", options)
31
+ end
32
+
33
+ # Returns {https://twitter.com/privacy Twitter's Privacy Policy}
34
+ #
35
+ # @see https://dev.twitter.com/docs/api/1.1/get/help/privacy
36
+ # @rate_limited Yes
37
+ # @authentication Requires user context
38
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
39
+ # @return [String]
40
+ def privacy(options={})
41
+ get("/1.1/help/privacy.json", options)[:body][:privacy]
42
+ end
43
+
44
+ # Returns {https://twitter.com/tos Twitter's Terms of Service}
45
+ #
46
+ # @see https://dev.twitter.com/docs/api/1.1/get/help/tos
47
+ # @rate_limited Yes
48
+ # @authentication Requires user context
49
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
50
+ # @return [String]
51
+ def tos(options={})
52
+ get("/1.1/help/tos.json", options)[:body][:tos]
53
+ end
54
+
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,498 @@
1
+ require 'twitter/arguments'
2
+ require 'twitter/core_ext/enumerable'
3
+ require 'twitter/cursor'
4
+ require 'twitter/error/forbidden'
5
+ require 'twitter/error/not_found'
6
+ require 'twitter/list'
7
+ require 'twitter/rest/api/utils'
8
+ require 'twitter/tweet'
9
+ require 'twitter/user'
10
+
11
+ module Twitter
12
+ module REST
13
+ module API
14
+ module Lists
15
+ include Twitter::REST::API::Utils
16
+ MAX_USERS_PER_REQUEST = 100
17
+ URI_SUBSTRING = "://"
18
+
19
+ # Returns all lists the authenticating or specified user subscribes to, including their own
20
+ #
21
+ # @see https://dev.twitter.com/docs/api/1.1/get/lists/list
22
+ # @rate_limited Yes
23
+ # @authentication Requires user context
24
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
25
+ # @return [Array<Twitter::List>]
26
+ # @overload lists(options={})
27
+ # @param options [Hash] A customizable set of options.
28
+ # @option options [Boolean] :reverse Set this to true if you would like owned lists to be returned first.
29
+ # @overload lists(user, options={})
30
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
31
+ # @param options [Hash] A customizable set of options.
32
+ # @option options [Boolean] :reverse Set this to true if you would like owned lists to be returned first.
33
+ def lists(*args)
34
+ objects_from_response_with_user(Twitter::List, :get, "/1.1/lists/list.json", args)
35
+ end
36
+ alias lists_subscribed_to lists
37
+
38
+ # Show tweet timeline for members of the specified list
39
+ #
40
+ # @see https://dev.twitter.com/docs/api/1.1/get/lists/statuses
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 [Array<Twitter::Tweet>]
45
+ # @overload list_timeline(list, options={})
46
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
47
+ # @param options [Hash] A customizable set of options.
48
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
49
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
50
+ # @option options [Integer] :count The number of results to retrieve.
51
+ # @overload list_timeline(user, list, options={})
52
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
53
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
54
+ # @param options [Hash] A customizable set of options.
55
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
56
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
57
+ # @option options [Integer] :count The number of results to retrieve.
58
+ def list_timeline(*args)
59
+ arguments = Twitter::Arguments.new(args)
60
+ merge_list!(arguments.options, arguments.pop)
61
+ merge_owner!(arguments.options, arguments.pop)
62
+ objects_from_response(Twitter::Tweet, :get, "/1.1/lists/statuses.json", arguments.options)
63
+ end
64
+
65
+ # Removes the specified member from the list
66
+ #
67
+ # @see https://dev.twitter.com/docs/api/1.1/post/lists/members/destroy
68
+ # @rate_limited No
69
+ # @authentication Requires user context
70
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
71
+ # @return [Twitter::List] The list.
72
+ # @overload remove_list_member(list, user_to_remove, options={})
73
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
74
+ # @param user_to_remove [Integer, String] The user id or screen name of the list member to remove.
75
+ # @param options [Hash] A customizable set of options.
76
+ # @overload remove_list_member(user, list, user_to_remove, options={})
77
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
78
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
79
+ # @param user_to_remove [Integer, String] The user id or screen name of the list member to remove.
80
+ # @param options [Hash] A customizable set of options.
81
+ def remove_list_member(*args)
82
+ list_from_response_with_user(:post, "/1.1/lists/members/destroy.json", args)
83
+ end
84
+ deprecate_alias :list_remove_member, :remove_list_member
85
+
86
+ # List the lists the specified user has been added to
87
+ #
88
+ # @see https://dev.twitter.com/docs/api/1.1/get/lists/memberships
89
+ # @rate_limited Yes
90
+ # @authentication Requires user context
91
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
92
+ # @return [Twitter::Cursor]
93
+ # @overload memberships(options={})
94
+ # @param options [Hash] A customizable set of options.
95
+ # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
96
+ # @option options [Boolean, String, Integer] :filter_to_owned_lists When set to true, t or 1, will return just lists the authenticating user owns, and the user represented by user_id or screen_name is a member of.
97
+ # @overload memberships(user, options={})
98
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
99
+ # @param options [Hash] A customizable set of options.
100
+ # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
101
+ # @option options [Boolean, String, Integer] :filter_to_owned_lists When set to true, t or 1, will return just lists the authenticating user owns, and the user represented by user_id or screen_name is a member of.
102
+ def memberships(*args)
103
+ cursor_from_response_with_user(:lists, Twitter::List, :get, "/1.1/lists/memberships.json", args)
104
+ end
105
+
106
+ # Returns the subscribers of the specified list
107
+ #
108
+ # @see https://dev.twitter.com/docs/api/1.1/get/lists/subscribers
109
+ # @rate_limited Yes
110
+ # @authentication Requires user context
111
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
112
+ # @return [Twitter::Cursor] The subscribers of the specified list.
113
+ # @overload list_subscribers(list, options={})
114
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
115
+ # @param options [Hash] A customizable set of options.
116
+ # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
117
+ # @overload list_subscribers(user, list, options={})
118
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
119
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
120
+ # @param options [Hash] A customizable set of options.
121
+ # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
122
+ def list_subscribers(*args)
123
+ cursor_from_response_with_list(:get, "/1.1/lists/subscribers.json", args)
124
+ end
125
+
126
+ # Make the authenticated user follow the specified list
127
+ #
128
+ # @see https://dev.twitter.com/docs/api/1.1/post/lists/subscribers/create
129
+ # @rate_limited No
130
+ # @authentication Requires user context
131
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
132
+ # @return [Twitter::List] The specified list.
133
+ # @overload list_subscribe(list, options={})
134
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
135
+ # @param options [Hash] A customizable set of options.
136
+ # @overload list_subscribe(user, list, options={})
137
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
138
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
139
+ # @param options [Hash] A customizable set of options.
140
+ def list_subscribe(*args)
141
+ list_from_response(:post, "/1.1/lists/subscribers/create.json", args)
142
+ end
143
+
144
+ # Check if a user is a subscriber of the specified list
145
+ #
146
+ # @see https://dev.twitter.com/docs/api/1.1/get/lists/subscribers/show
147
+ # @rate_limited Yes
148
+ # @authentication Requires user context
149
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
150
+ # @return [Boolean] true if user is a subscriber of the specified list, otherwise false.
151
+ # @overload list_subscriber?(list, user_to_check, options={})
152
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
153
+ # @param user_to_check [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
154
+ # @param options [Hash] A customizable set of options.
155
+ # @overload list_subscriber?(user, list, user_to_check, options={})
156
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
157
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
158
+ # @param user_to_check [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
159
+ # @param options [Hash] A customizable set of options.
160
+ # @return [Boolean] true if user is a subscriber of the specified list, otherwise false.
161
+ def list_subscriber?(*args)
162
+ list_user?(:get, "/1.1/lists/subscribers/show.json", args)
163
+ end
164
+
165
+ # Unsubscribes the authenticated user form the specified list
166
+ #
167
+ # @see https://dev.twitter.com/docs/api/1.1/post/lists/subscribers/destroy
168
+ # @rate_limited No
169
+ # @authentication Requires user context
170
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
171
+ # @return [Twitter::List] The specified list.
172
+ # @overload list_unsubscribe(list, options={})
173
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
174
+ # @param options [Hash] A customizable set of options.
175
+ # @overload list_unsubscribe(user, list, options={})
176
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
177
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
178
+ # @param options [Hash] A customizable set of options.
179
+ def list_unsubscribe(*args)
180
+ list_from_response(:post, "/1.1/lists/subscribers/destroy.json", args)
181
+ end
182
+
183
+ # Adds specified members to a list
184
+ #
185
+ # @see https://dev.twitter.com/docs/api/1.1/post/lists/members/create_all
186
+ # @note Lists are limited to having 500 members, and you are limited to adding up to 100 members to a list at a time with this method.
187
+ # @rate_limited No
188
+ # @authentication Requires user context
189
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
190
+ # @return [Twitter::List] The list.
191
+ # @overload add_list_members(list, users, options={})
192
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
193
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
194
+ # @param options [Hash] A customizable set of options.
195
+ # @overload add_list_members(user, list, users, options={})
196
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
197
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
198
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
199
+ # @param options [Hash] A customizable set of options.
200
+ def add_list_members(*args)
201
+ list_from_response_with_users(:post, "/1.1/lists/members/create_all.json", args)
202
+ end
203
+ deprecate_alias :list_add_members, :add_list_members
204
+
205
+ # Check if a user is a member of the specified list
206
+ #
207
+ # @see https://dev.twitter.com/docs/api/1.1/get/lists/members/show
208
+ # @authentication Requires user context
209
+ # @rate_limited Yes
210
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
211
+ # @return [Boolean] true if user is a member of the specified list, otherwise false.
212
+ # @overload list_member?(list, user_to_check, options={})
213
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
214
+ # @param user_to_check [Integer, String] The user ID or screen name of the list member.
215
+ # @param options [Hash] A customizable set of options.
216
+ # @overload list_member?(user, list, user_to_check, options={})
217
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
218
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
219
+ # @param user_to_check [Integer, String] The user ID or screen name of the list member.
220
+ # @param options [Hash] A customizable set of options.
221
+ def list_member?(*args)
222
+ list_user?(:get, "/1.1/lists/members/show.json", args)
223
+ end
224
+
225
+ # Returns the members of the specified list
226
+ #
227
+ # @see https://dev.twitter.com/docs/api/1.1/get/lists/members
228
+ # @rate_limited Yes
229
+ # @authentication Requires user context
230
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
231
+ # @return [Twitter::Cursor]
232
+ # @overload list_members(list, options={})
233
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
234
+ # @param options [Hash] A customizable set of options.
235
+ # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
236
+ # @overload list_members(user, list, options={})
237
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
238
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
239
+ # @param options [Hash] A customizable set of options.
240
+ # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
241
+ def list_members(*args)
242
+ cursor_from_response_with_list(:get, "/1.1/lists/members.json", args)
243
+ end
244
+
245
+ # Add a member to a list
246
+ #
247
+ # @see https://dev.twitter.com/docs/api/1.1/post/lists/members/create
248
+ # @note Lists are limited to having 500 members.
249
+ # @rate_limited No
250
+ # @authentication Requires user context
251
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
252
+ # @return [Twitter::List] The list.
253
+ # @overload add_list_member(list, user_to_add, options={})
254
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
255
+ # @param user_to_add [Integer, String] The user id or screen name to add to the list.
256
+ # @param options [Hash] A customizable set of options.
257
+ # @overload add_list_member(user, list, user_to_add, options={})
258
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
259
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
260
+ # @param user_to_add [Integer, String] The user id or screen name to add to the list.
261
+ # @param options [Hash] A customizable set of options.
262
+ def add_list_member(*args)
263
+ list_from_response_with_user(:post, "/1.1/lists/members/create.json", args)
264
+ end
265
+ deprecate_alias :list_add_member, :add_list_member
266
+
267
+ # Deletes the specified list
268
+ #
269
+ # @see https://dev.twitter.com/docs/api/1.1/post/lists/destroy
270
+ # @note Must be owned by the authenticated user.
271
+ # @rate_limited No
272
+ # @authentication Requires user context
273
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
274
+ # @return [Twitter::List] The deleted list.
275
+ # @overload destroy_list(list, options={})
276
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
277
+ # @param options [Hash] A customizable set of options.
278
+ # @overload destroy_list(user, list, options={})
279
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
280
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
281
+ # @param options [Hash] A customizable set of options.
282
+ def destroy_list(*args)
283
+ list_from_response(:post, "/1.1/lists/destroy.json", args)
284
+ end
285
+ deprecate_alias :list_destroy, :destroy_list
286
+
287
+ # Updates the specified list
288
+ #
289
+ # @see https://dev.twitter.com/docs/api/1.1/post/lists/update
290
+ # @rate_limited No
291
+ # @authentication Requires user context
292
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
293
+ # @return [Twitter::List] The created list.
294
+ # @overload list_update(list, options={})
295
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
296
+ # @param options [Hash] A customizable set of options.
297
+ # @option options [String] :mode ('public') Whether your list is public or private. Values can be 'public' or 'private'.
298
+ # @option options [String] :description The description to give the list.
299
+ # @overload list_update(user, list, options={})
300
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
301
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
302
+ # @param options [Hash] A customizable set of options.
303
+ # @option options [String] :mode ('public') Whether your list is public or private. Values can be 'public' or 'private'.
304
+ # @option options [String] :description The description to give the list.
305
+ def list_update(*args)
306
+ list_from_response(:post, "/1.1/lists/update.json", args)
307
+ end
308
+
309
+ # Creates a new list for the authenticated user
310
+ #
311
+ # @see https://dev.twitter.com/docs/api/1.1/post/lists/create
312
+ # @note Accounts are limited to 20 lists.
313
+ # @rate_limited No
314
+ # @authentication Requires user context
315
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
316
+ # @return [Twitter::List] The created list.
317
+ # @param name [String] The name for the list.
318
+ # @param options [Hash] A customizable set of options.
319
+ # @option options [String] :mode ('public') Whether your list is public or private. Values can be 'public' or 'private'.
320
+ # @option options [String] :description The description to give the list.
321
+ def create_list(name, options={})
322
+ object_from_response(Twitter::List, :post, "/1.1/lists/create.json", options.merge(:name => name))
323
+ end
324
+ deprecate_alias :list_create, :create_list
325
+
326
+ # Show the specified list
327
+ #
328
+ # @see https://dev.twitter.com/docs/api/1.1/get/lists/show
329
+ # @note Private lists will only be shown if the authenticated user owns the specified list.
330
+ # @rate_limited Yes
331
+ # @authentication Requires user context
332
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
333
+ # @return [Twitter::List] The specified list.
334
+ # @overload list(list, options={})
335
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
336
+ # @param options [Hash] A customizable set of options.
337
+ # @overload list(user, list, options={})
338
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
339
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
340
+ # @param options [Hash] A customizable set of options.
341
+ def list(*args)
342
+ list_from_response(:get, "/1.1/lists/show.json", args)
343
+ end
344
+
345
+ # List the lists the specified user follows
346
+ #
347
+ # @see https://dev.twitter.com/docs/api/1.1/get/lists/subscriptions
348
+ # @rate_limited Yes
349
+ # @authentication Requires user context
350
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
351
+ # @return [Twitter::Cursor]
352
+ # @overload subscriptions(options={})
353
+ # @param options [Hash] A customizable set of options.
354
+ # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
355
+ # @overload subscriptions(user, options={})
356
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
357
+ # @param options [Hash] A customizable set of options.
358
+ # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
359
+ def subscriptions(*args)
360
+ cursor_from_response_with_user(:lists, Twitter::List, :get, "/1.1/lists/subscriptions.json", args)
361
+ end
362
+
363
+ # Removes specified members from the list
364
+ #
365
+ # @see https://dev.twitter.com/docs/api/1.1/post/lists/members/destroy_all
366
+ # @rate_limited No
367
+ # @authentication Requires user context
368
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
369
+ # @return [Twitter::List] The list.
370
+ # @overload remove_list_members(list, users, options={})
371
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
372
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
373
+ # @param options [Hash] A customizable set of options.
374
+ # @overload remove_list_members(user, list, users, options={})
375
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
376
+ # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, URI, or object.
377
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
378
+ # @param options [Hash] A customizable set of options.
379
+ def remove_list_members(*args)
380
+ list_from_response_with_users(:post, "/1.1/lists/members/destroy_all.json", args)
381
+ end
382
+ deprecate_alias :list_remove_members, :remove_list_members
383
+
384
+ # Returns the lists owned by the specified Twitter user
385
+ #
386
+ # @see https://dev.twitter.com/docs/api/1.1/get/lists/ownerships
387
+ # @rate_limited Yes
388
+ # @authentication Requires user context
389
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
390
+ # @return [Array<Twitter::List>]
391
+ # @overload owned_lists(options={})
392
+ # @param options [Hash] A customizable set of options.
393
+ # @option options [Integer] :count The amount of results to return per page. Defaults to 20. No more than 1000 results will ever be returned in a single page.
394
+ # @overload owned_lists(user, options={})
395
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
396
+ # @param options [Hash] A customizable set of options.
397
+ # @option options [Integer] :count The amount of results to return per page. Defaults to 20. No more than 1000 results will ever be returned in a single page.
398
+ def owned_lists(*args)
399
+ cursor_from_response_with_user(:lists, Twitter::List, :get, "/1.1/lists/ownerships.json", args)
400
+ end
401
+ deprecate_alias :lists_ownerships, :owned_lists
402
+ deprecate_alias :lists_owned, :owned_lists
403
+
404
+ private
405
+
406
+ # @param request_method [Symbol]
407
+ # @param path [String]
408
+ # @param args [Array]
409
+ # @return [Array<Twitter::User>]
410
+ def list_from_response(request_method, path, args)
411
+ arguments = Twitter::Arguments.new(args)
412
+ merge_list!(arguments.options, arguments.pop)
413
+ merge_owner!(arguments.options, arguments.pop)
414
+ object_from_response(Twitter::List, request_method, path, arguments.options)
415
+ end
416
+
417
+ def cursor_from_response_with_list(request_method, path, args)
418
+ arguments = Twitter::Arguments.new(args)
419
+ merge_list!(arguments.options, arguments.pop)
420
+ merge_owner!(arguments.options, arguments.pop)
421
+ cursor_from_response(:users, Twitter::User, request_method, path, arguments.options)
422
+ end
423
+
424
+ def list_user?(request_method, path, args)
425
+ arguments = Twitter::Arguments.new(args)
426
+ merge_user!(arguments.options, arguments.pop)
427
+ merge_list!(arguments.options, arguments.pop)
428
+ merge_owner!(arguments.options, arguments.pop)
429
+ send(request_method.to_sym, path, arguments.options)
430
+ true
431
+ rescue Twitter::Error::NotFound, Twitter::Error::Forbidden
432
+ false
433
+ end
434
+
435
+ def list_from_response_with_user(request_method, path, args)
436
+ arguments = Twitter::Arguments.new(args)
437
+ merge_user!(arguments.options, arguments.pop)
438
+ merge_list!(arguments.options, arguments.pop)
439
+ merge_owner!(arguments.options, arguments.pop)
440
+ object_from_response(Twitter::List, request_method, path, arguments.options)
441
+ end
442
+
443
+ def list_from_response_with_users(request_method, path, args)
444
+ arguments = Twitter::Arguments.new(args)
445
+ members = arguments.pop
446
+ merge_list!(arguments.options, arguments.pop)
447
+ merge_owner!(arguments.options, arguments.pop)
448
+ members.flatten.each_slice(MAX_USERS_PER_REQUEST).threaded_map do |users|
449
+ object_from_response(Twitter::List, request_method, path, merge_users(arguments.options, users))
450
+ end.last
451
+ end
452
+
453
+ # Take a list and merge it into the hash with the correct key
454
+ #
455
+ # @param hash [Hash]
456
+ # @param list [Integer, String, URI, Twitter::List] A Twitter list ID, slug, URI, or object.
457
+ # @return [Hash]
458
+ def merge_list!(hash, list)
459
+ case list
460
+ when Integer
461
+ hash[:list_id] = list
462
+ when String
463
+ if list[URI_SUBSTRING]
464
+ list = list.split("/")
465
+ hash[:slug] = list.pop
466
+ hash[:owner_screen_name] = list.pop
467
+ else
468
+ hash[:slug] = list
469
+ end
470
+ when URI
471
+ list = list.path.split("/")
472
+ hash[:slug] = list.pop
473
+ hash[:owner_screen_name] = list.pop
474
+ when Twitter::List
475
+ hash[:list_id] = list.id
476
+ merge_owner!(hash, list.user)
477
+ end
478
+ hash
479
+ end
480
+
481
+ # Take an owner and merge it into the hash with the correct key
482
+ #
483
+ # @param hash [Hash]
484
+ # @param user[Integer, String, Twitter::User] A Twitter user ID, screen_name, or object.
485
+ # @return [Hash]
486
+ def merge_owner!(hash, user)
487
+ unless hash[:owner_id] || hash[:owner_screen_name]
488
+ user ||= screen_name
489
+ merge_user!(hash, user, "owner")
490
+ hash[:owner_id] = hash.delete(:owner_user_id) unless hash[:owner_user_id].nil?
491
+ end
492
+ hash
493
+ end
494
+
495
+ end
496
+ end
497
+ end
498
+ end
@@ -0,0 +1,62 @@
1
+ require 'twitter/rest/api/utils'
2
+ require 'twitter/token'
3
+ require 'twitter/rest/response/parse_error_json'
4
+
5
+ module Twitter
6
+ module REST
7
+ module API
8
+ module OAuth
9
+ include Twitter::REST::API::Utils
10
+
11
+ # Allows a registered application to obtain an OAuth 2 Bearer Token, which can be used to make API requests
12
+ # on an application's own behalf, without a user context.
13
+ #
14
+ # Only one bearer token may exist outstanding for an application, and repeated requests to this method
15
+ # will yield the same already-existent token until it has been invalidated.
16
+ #
17
+ # @see https://dev.twitter.com/docs/api/1.1/post/oauth2/token
18
+ # @rate_limited No
19
+ # @authentication Required
20
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
21
+ # @return [Twitter::Token] The Bearer Token. token_type should be 'bearer'.
22
+ # @example Generate a Bearer Token
23
+ # client = Twitter::REST::Client.new(:consumer_key => "abc", :consumer_secret => 'def')
24
+ # bearer_token = client.token
25
+ def token
26
+ object_from_response(Twitter::Token, :post, "/oauth2/token", :grant_type => "client_credentials", :bearer_token_request => true)
27
+ end
28
+ alias bearer_token token
29
+
30
+ # Allows a registered application to revoke an issued OAuth 2 Bearer Token by presenting its client credentials.
31
+ #
32
+ # @see https://dev.twitter.com/docs/api/1.1/post/oauth2/invalidate_token
33
+ # @rate_limited No
34
+ # @authentication Required
35
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
36
+ # @param access_token [String, Twitter::Token] The bearer token to revoke.
37
+ # @return [Twitter::Token] The invalidated token. token_type should be nil.
38
+ def invalidate_token(access_token)
39
+ access_token = access_token.access_token if access_token.is_a?(Twitter::Token)
40
+ object_from_response(Twitter::Token, :post, "/oauth2/invalidate_token", :access_token => access_token)
41
+ end
42
+
43
+ # Allows a registered application to revoke an issued OAuth 2 Bearer Token by presenting its client credentials.
44
+ #
45
+ # @see https://dev.twitter.com/docs/api/1.1/post/oauth2/invalidate_token
46
+ # @rate_limited No
47
+ # @authentication Required
48
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
49
+ # @return [String] The token string.
50
+ def reverse_token
51
+ conn = connection.dup
52
+ conn.builder.swap(4, Twitter::REST::Response::ParseErrorJson)
53
+ response = conn.post('/oauth/request_token?x_auth_mode=reverse_auth') do |request|
54
+ request.headers[:authorization] = oauth_auth_header(:post, 'https://api.twitter.com/oauth/request_token', :x_auth_mode => 'reverse_auth').to_s
55
+ end
56
+ response.body
57
+ end
58
+
59
+ end
60
+ end
61
+ end
62
+ end