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
@@ -1,30 +1,26 @@
1
1
  module Twitter
2
- class RateLimit
3
- attr_reader :attrs
4
- alias to_hash attrs
5
-
6
- # @return [Twitter::RateLimit]
7
- def initialize(attrs={})
8
- @attrs = attrs
9
- end
2
+ class RateLimit < Twitter::Base
10
3
 
11
4
  # @return [Integer]
12
5
  def limit
13
6
  limit = @attrs['x-rate-limit-limit']
14
7
  limit.to_i if limit
15
8
  end
9
+ memoize :limit
16
10
 
17
11
  # @return [Integer]
18
12
  def remaining
19
13
  remaining = @attrs['x-rate-limit-remaining']
20
14
  remaining.to_i if remaining
21
15
  end
16
+ memoize :remaining
22
17
 
23
18
  # @return [Time]
24
19
  def reset_at
25
20
  reset = @attrs['x-rate-limit-reset']
26
21
  Time.at(reset.to_i) if reset
27
22
  end
23
+ memoize :reset_at
28
24
 
29
25
  # @return [Integer]
30
26
  def reset_in
@@ -32,14 +28,5 @@ module Twitter
32
28
  end
33
29
  alias retry_after reset_in
34
30
 
35
- # Update the attributes of a RateLimit
36
- #
37
- # @param attrs [Hash]
38
- # @return [Twitter::RateLimit]
39
- def update(attrs)
40
- @attrs.update(attrs)
41
- self
42
- end
43
-
44
31
  end
45
32
  end
@@ -2,6 +2,8 @@ require 'twitter/base'
2
2
 
3
3
  module Twitter
4
4
  class Relationship < Twitter::Base
5
+ object_attr_reader :SourceUser, :source
6
+ object_attr_reader :TargetUser, :target
5
7
 
6
8
  # Initializes a new object
7
9
  #
@@ -11,24 +13,5 @@ module Twitter
11
13
  @attrs = attrs[:relationship]
12
14
  end
13
15
 
14
- # @return [Twitter::SourceUser]
15
- def source
16
- @source ||= Twitter::SourceUser.fetch_or_new(@attrs[:source])
17
- end
18
-
19
- # @return [Twitter::TargetUser]
20
- def target
21
- @target ||= Twitter::TargetUser.fetch_or_new(@attrs[:target])
22
- end
23
-
24
- # Update the attributes of a Relationship
25
- #
26
- # @param attrs [Hash]
27
- # @return [Twitter::Relationship]
28
- def update(attrs)
29
- @attrs.update(attrs[:relationship]) unless attrs[:relationship].nil?
30
- self
31
- end
32
-
33
16
  end
34
17
  end
@@ -0,0 +1,138 @@
1
+ require 'twitter/arguments'
2
+ require 'twitter/direct_message'
3
+ require 'twitter/rest/api/utils'
4
+ require 'twitter/user'
5
+
6
+ module Twitter
7
+ module REST
8
+ module API
9
+ module DirectMessages
10
+ include Twitter::REST::API::Utils
11
+
12
+ # Returns the 20 most recent direct messages sent to the authenticating user
13
+ #
14
+ # @see https://dev.twitter.com/docs/api/1.1/get/direct_messages
15
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
16
+ # @rate_limited Yes
17
+ # @authentication Requires user context
18
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
19
+ # @return [Array<Twitter::DirectMessage>] Direct messages sent to the authenticating user.
20
+ # @param options [Hash] A customizable set of options.
21
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
22
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
23
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
24
+ # @option options [Integer] :page Specifies the page of results to retrieve.
25
+ def direct_messages_received(options={})
26
+ objects_from_response(Twitter::DirectMessage, :get, "/1.1/direct_messages.json", options)
27
+ end
28
+
29
+ # Returns the 20 most recent direct messages sent by the authenticating user
30
+ #
31
+ # @see https://dev.twitter.com/docs/api/1.1/get/direct_messages/sent
32
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
33
+ # @rate_limited Yes
34
+ # @authentication Requires user context
35
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
36
+ # @return [Array<Twitter::DirectMessage>] Direct messages sent by the authenticating user.
37
+ # @param options [Hash] A customizable set of options.
38
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
39
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
40
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
41
+ # @option options [Integer] :page Specifies the page of results to retrieve.
42
+ def direct_messages_sent(options={})
43
+ objects_from_response(Twitter::DirectMessage, :get, "/1.1/direct_messages/sent.json", options)
44
+ end
45
+
46
+ # Returns a direct message
47
+ #
48
+ # @see https://dev.twitter.com/docs/api/1.1/get/direct_messages/show
49
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
50
+ # @rate_limited Yes
51
+ # @authentication Requires user context
52
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
53
+ # @return [Twitter::DirectMessage] The requested messages.
54
+ # @param id [Integer] A direct message ID.
55
+ # @param options [Hash] A customizable set of options.
56
+ def direct_message(id, options={})
57
+ options[:id] = id
58
+ object_from_response(Twitter::DirectMessage, :get, "/1.1/direct_messages/show.json", options)
59
+ end
60
+
61
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
62
+ # @rate_limited Yes
63
+ # @authentication Requires user context
64
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
65
+ # @return [Array<Twitter::DirectMessage>] The requested messages.
66
+ # @overload direct_messages(options={})
67
+ # Returns the 20 most recent direct messages sent to the authenticating user
68
+ #
69
+ # @see https://dev.twitter.com/docs/api/1.1/get/direct_messages
70
+ # @param options [Hash] A customizable set of options.
71
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
72
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
73
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
74
+ # @option options [Integer] :page Specifies the page of results to retrieve.
75
+ # @overload direct_messages(*ids)
76
+ # Returns direct messages
77
+ #
78
+ # @see https://dev.twitter.com/docs/api/1.1/get/direct_messages/show
79
+ # @param ids [Enumerable<Integer>] A collection of direct message IDs.
80
+ # @overload direct_messages(*ids, options)
81
+ # Returns direct messages
82
+ #
83
+ # @see https://dev.twitter.com/docs/api/1.1/get/direct_messages/show
84
+ # @param ids [Enumerable<Integer>] A collection of direct message IDs.
85
+ # @param options [Hash] A customizable set of options.
86
+ def direct_messages(*args)
87
+ arguments = Twitter::Arguments.new(args)
88
+ if arguments.empty?
89
+ direct_messages_received(arguments.options)
90
+ else
91
+ arguments.flatten.threaded_map do |id|
92
+ direct_message(id, arguments.options)
93
+ end
94
+ end
95
+ end
96
+
97
+ # Destroys direct messages
98
+ #
99
+ # @see https://dev.twitter.com/docs/api/1.1/post/direct_messages/destroy
100
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
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 [Array<Twitter::DirectMessage>] Deleted direct message.
105
+ # @overload destroy_direct_message(*ids)
106
+ # @param ids [Enumerable<Integer>] A collection of direct message IDs.
107
+ # @overload destroy_direct_message(*ids, options)
108
+ # @param ids [Enumerable<Integer>] A collection of direct message IDs.
109
+ # @param options [Hash] A customizable set of options.
110
+ def destroy_direct_message(*args)
111
+ threaded_objects_from_response(Twitter::DirectMessage, :post, "/1.1/direct_messages/destroy.json", args)
112
+ end
113
+ deprecate_alias :direct_message_destroy, :destroy_direct_message
114
+
115
+ # Sends a new direct message to the specified user from the authenticating user
116
+ #
117
+ # @see https://dev.twitter.com/docs/api/1.1/post/direct_messages/new
118
+ # @rate_limited No
119
+ # @authentication Requires user context
120
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
121
+ # @return [Twitter::DirectMessage] The sent message.
122
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
123
+ # @param text [String] The text of your direct message, up to 140 characters.
124
+ # @param options [Hash] A customizable set of options.
125
+ def create_direct_message(user, text, options={})
126
+ merge_user!(options, user)
127
+ options[:text] = text
128
+ object_from_response(Twitter::DirectMessage, :post, "/1.1/direct_messages/new.json", options)
129
+ end
130
+ alias d create_direct_message
131
+ alias m create_direct_message
132
+ alias dm create_direct_message
133
+ deprecate_alias :direct_message_create, :create_direct_message
134
+
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,115 @@
1
+ require 'twitter/arguments'
2
+ require 'twitter/error/already_favorited'
3
+ require 'twitter/error/forbidden'
4
+ require 'twitter/rest/api/utils'
5
+ require 'twitter/tweet'
6
+ require 'twitter/user'
7
+
8
+ module Twitter
9
+ module REST
10
+ module API
11
+ module Favorites
12
+ include Twitter::REST::API::Utils
13
+
14
+ # @see https://dev.twitter.com/docs/api/1.1/get/favorites/list
15
+ # @rate_limited Yes
16
+ # @authentication Requires user context
17
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
18
+ # @return [Array<Twitter::Tweet>] favorite Tweets.
19
+ # @overload favorites(options={})
20
+ # Returns the 20 most recent favorite Tweets for the authenticating user
21
+ #
22
+ # @param options [Hash] A customizable set of options.
23
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100.
24
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
25
+ # @overload favorites(user, options={})
26
+ # Returns the 20 most recent favorite Tweets for the specified user
27
+ #
28
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
29
+ # @param options [Hash] A customizable set of options.
30
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100.
31
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
32
+ def favorites(*args)
33
+ arguments = Twitter::Arguments.new(args)
34
+ merge_user!(arguments.options, arguments.pop) if arguments.last
35
+ objects_from_response(Twitter::Tweet, :get, "/1.1/favorites/list.json", arguments.options)
36
+ end
37
+
38
+ # Un-favorites the specified Tweets as the authenticating user
39
+ #
40
+ # @see https://dev.twitter.com/docs/api/1.1/post/favorites/destroy
41
+ # @rate_limited No
42
+ # @authentication Requires user context
43
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
44
+ # @return [Array<Twitter::Tweet>] The un-favorited Tweets.
45
+ # @overload unfavorite(*tweets)
46
+ # @param tweets [Enumerable<Integer, String, URI, Twitter::Tweet>] A collection of Tweet IDs, URIs, or objects.
47
+ # @overload unfavorite(*tweets, options)
48
+ # @param tweets [Enumerable<Integer, String, URI, Twitter::Tweet>] A collection of Tweet IDs, URIs, or objects.
49
+ # @param options [Hash] A customizable set of options.
50
+ def unfavorite(*args)
51
+ threaded_objects_from_response(Twitter::Tweet, :post, "/1.1/favorites/destroy.json", args)
52
+ end
53
+ alias destroy_favorite unfavorite
54
+ deprecate_alias :favorite_destroy, :unfavorite
55
+
56
+ # Favorites the specified Tweets as the authenticating user
57
+ #
58
+ # @see https://dev.twitter.com/docs/api/1.1/post/favorites/create
59
+ # @rate_limited No
60
+ # @authentication Requires user context
61
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
62
+ # @return [Array<Twitter::Tweet>] The favorited Tweets.
63
+ # @overload favorite(*tweets)
64
+ # @param tweets [Enumerable<Integer, String, URI, Twitter::Tweet>] A collection of Tweet IDs, URIs, or objects.
65
+ # @overload favorite(*tweets, options)
66
+ # @param tweets [Enumerable<Integer, String, URI, Twitter::Tweet>] A collection of Tweet IDs, URIs, or objects.
67
+ # @param options [Hash] A customizable set of options.
68
+ def favorite(*args)
69
+ arguments = Twitter::Arguments.new(args)
70
+ arguments.flatten.threaded_map do |tweet|
71
+ id = extract_id(tweet)
72
+ begin
73
+ object_from_response(Twitter::Tweet, :post, "/1.1/favorites/create.json", arguments.options.merge(:id => id))
74
+ rescue Twitter::Error::Forbidden => error
75
+ raise unless error.message == Twitter::Error::AlreadyFavorited::MESSAGE
76
+ end
77
+ end.compact
78
+ end
79
+ alias fav favorite
80
+ alias fave favorite
81
+ deprecate_alias :favorite_create, :favorite
82
+
83
+ # Favorites the specified Tweets as the authenticating user and raises an error if one has already been favorited
84
+ #
85
+ # @see https://dev.twitter.com/docs/api/1.1/post/favorites/create
86
+ # @rate_limited No
87
+ # @authentication Requires user context
88
+ # @raise [Twitter::Error::AlreadyFavorited] Error raised when tweet has already been favorited.
89
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
90
+ # @return [Array<Twitter::Tweet>] The favorited Tweets.
91
+ # @overload favorite(*tweets)
92
+ # @param tweets [Enumerable<Integer, String, URI, Twitter::Tweet>] A collection of Tweet IDs, URIs, or objects.
93
+ # @overload favorite(*tweets, options)
94
+ # @param tweets [Enumerable<Integer, String, URI, Twitter::Tweet>] A collection of Tweet IDs, URIs, or objects.
95
+ # @param options [Hash] A customizable set of options.
96
+ def favorite!(*args)
97
+ arguments = Twitter::Arguments.new(args)
98
+ arguments.flatten.threaded_map do |tweet|
99
+ id = extract_id(tweet)
100
+ begin
101
+ object_from_response(Twitter::Tweet, :post, "/1.1/favorites/create.json", arguments.options.merge(:id => id))
102
+ rescue Twitter::Error::Forbidden => error
103
+ handle_forbidden_error(Twitter::Error::AlreadyFavorited, error)
104
+ end
105
+ end
106
+ end
107
+ alias create_favorite! favorite!
108
+ alias fav! favorite!
109
+ alias fave! favorite!
110
+ deprecate_alias :favorite_create!, :favorite!
111
+
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,287 @@
1
+ require 'twitter/arguments'
2
+ require 'twitter/cursor'
3
+ require 'twitter/error/forbidden'
4
+ require 'twitter/relationship'
5
+ require 'twitter/rest/api/utils'
6
+ require 'twitter/user'
7
+
8
+ module Twitter
9
+ module REST
10
+ module API
11
+ module FriendsAndFollowers
12
+ include Twitter::REST::API::Utils
13
+
14
+ # @see https://dev.twitter.com/docs/api/1.1/get/friends/ids
15
+ # @rate_limited Yes
16
+ # @authentication Requires user context
17
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
18
+ # @return [Twitter::Cursor]
19
+ # @overload friend_ids(options={})
20
+ # Returns an array of numeric IDs for every user the authenticated user is following
21
+ #
22
+ # @param options [Hash] A customizable set of options.
23
+ # @option options [Integer] :cursor (-1) Breaks the results into pages. This is recommended for users who are following many users. Provide a value of -1 to begin paging. Provide values as returned in the response body's next_cursor and previous_cursor attributes to page back and forth in the list.
24
+ # @overload friend_ids(user, options={})
25
+ # Returns an array of numeric IDs for every user the specified user is following
26
+ #
27
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
28
+ # @param options [Hash] A customizable set of options.
29
+ # @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.
30
+ def friend_ids(*args)
31
+ cursor_from_response_with_user(:ids, nil, :get, "/1.1/friends/ids.json", args)
32
+ end
33
+
34
+ # @see https://dev.twitter.com/docs/api/1.1/get/followers/ids
35
+ # @rate_limited Yes
36
+ # @authentication Requires user context
37
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
38
+ # @return [Twitter::Cursor]
39
+ # @overload follower_ids(options={})
40
+ # Returns an array of numeric IDs for every user following the authenticated user
41
+ #
42
+ # @param options [Hash] A customizable set of options.
43
+ # @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.
44
+ # @overload follower_ids(user, options={})
45
+ # Returns an array of numeric IDs for every user following the specified user
46
+ #
47
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
48
+ # @param options [Hash] A customizable set of options.
49
+ # @option options [Integer] :cursor (-1) Breaks the results into pages. This is recommended for users who are following many users. Provide a value of -1 to begin paging. Provide values as returned in the response body's next_cursor and previous_cursor attributes to page back and forth in the list.
50
+ def follower_ids(*args)
51
+ cursor_from_response_with_user(:ids, nil, :get, "/1.1/followers/ids.json", args)
52
+ end
53
+
54
+ # Returns the relationship of the authenticating user to the comma separated list of up to 100 screen_names or user_ids provided. Values for connections can be: following, following_requested, followed_by, none.
55
+ #
56
+ # @see https://dev.twitter.com/docs/api/1.1/get/friendships/lookup
57
+ # @rate_limited Yes
58
+ # @authentication Requires user context
59
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
60
+ # @return [Array<Twitter::User>] The requested users.
61
+ # @overload friendships(*users)
62
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
63
+ # @overload friendships(*users, options)
64
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
65
+ # @param options [Hash] A customizable set of options.
66
+ def friendships(*args)
67
+ arguments = Twitter::Arguments.new(args)
68
+ merge_users!(arguments.options, arguments)
69
+ objects_from_response(Twitter::User, :get, "/1.1/friendships/lookup.json", arguments.options)
70
+ end
71
+
72
+ # Returns an array of numeric IDs for every user who has a pending request to follow the authenticating user
73
+ #
74
+ # @see https://dev.twitter.com/docs/api/1.1/get/friendships/incoming
75
+ # @rate_limited Yes
76
+ # @authentication Requires user context
77
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
78
+ # @return [Twitter::Cursor]
79
+ # @param options [Hash] A customizable set of options.
80
+ # @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.
81
+ def friendships_incoming(options={})
82
+ cursor_from_response(:ids, nil, :get, "/1.1/friendships/incoming.json", options)
83
+ end
84
+
85
+ # Returns an array of numeric IDs for every protected user for whom the authenticating user has a pending follow request
86
+ #
87
+ # @see https://dev.twitter.com/docs/api/1.1/get/friendships/outgoing
88
+ # @rate_limited Yes
89
+ # @authentication Requires user context
90
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
91
+ # @return [Twitter::Cursor]
92
+ # @param options [Hash] A customizable set of options.
93
+ # @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.
94
+ def friendships_outgoing(options={})
95
+ cursor_from_response(:ids, nil, :get, "/1.1/friendships/outgoing.json", options)
96
+ end
97
+
98
+ # Allows the authenticating user to follow the specified users, unless they are already followed
99
+ #
100
+ # @see https://dev.twitter.com/docs/api/1.1/post/friendships/create
101
+ # @rate_limited Yes
102
+ # @authentication Requires user context
103
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
104
+ # @return [Array<Twitter::User>] The followed users.
105
+ # @overload follow(*users)
106
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
107
+ # @overload follow(*users, options)
108
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
109
+ # @param options [Hash] A customizable set of options.
110
+ # @option options [Boolean] :follow (false) Enable notifications for the target user.
111
+ def follow(*args)
112
+ arguments = Twitter::Arguments.new(args)
113
+ existing_friends = Thread.new do
114
+ friend_ids.to_a
115
+ end
116
+ new_friends = Thread.new do
117
+ users(args).map(&:id)
118
+ end
119
+ follow!(new_friends.value - existing_friends.value, arguments.options)
120
+ end
121
+ alias create_friendship follow
122
+ deprecate_alias :friendship_create, :follow
123
+
124
+ # Allows the authenticating user to follow the specified users
125
+ #
126
+ # @see https://dev.twitter.com/docs/api/1.1/post/friendships/create
127
+ # @rate_limited No
128
+ # @authentication Requires user context
129
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
130
+ # @return [Array<Twitter::User>] The followed users.
131
+ # @overload follow!(*users)
132
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
133
+ # @overload follow!(*users, options)
134
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
135
+ # @param options [Hash] A customizable set of options.
136
+ # @option options [Boolean] :follow (false) Enable notifications for the target user.
137
+ def follow!(*args)
138
+ arguments = Twitter::Arguments.new(args)
139
+ arguments.flatten.threaded_map do |user|
140
+ begin
141
+ object_from_response(Twitter::User, :post, "/1.1/friendships/create.json", merge_user(arguments.options, user))
142
+ rescue Twitter::Error::Forbidden
143
+ # This error will be raised if the user doesn't have permission to
144
+ # follow list_member, for whatever reason.
145
+ end
146
+ end.compact
147
+ end
148
+ alias create_friendship! follow!
149
+ deprecate_alias :friendship_create!, :follow!
150
+
151
+ # Allows the authenticating user to unfollow the specified users
152
+ #
153
+ # @see https://dev.twitter.com/docs/api/1.1/post/friendships/destroy
154
+ # @rate_limited No
155
+ # @authentication Requires user context
156
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
157
+ # @return [Array<Twitter::User>] The unfollowed users.
158
+ # @overload unfollow(*users)
159
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
160
+ # @overload unfollow(*users, options)
161
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
162
+ # @param options [Hash] A customizable set of options.
163
+ def unfollow(*args)
164
+ threaded_user_objects_from_response(:post, "/1.1/friendships/destroy.json", args)
165
+ end
166
+ alias destroy_friendship unfollow
167
+ deprecate_alias :friendship_destroy, :unfollow
168
+
169
+ # Allows one to enable or disable retweets and device notifications from the specified user.
170
+ #
171
+ # @see https://dev.twitter.com/docs/api/1.1/post/friendships/update
172
+ # @rate_limited No
173
+ # @authentication Requires user context
174
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
175
+ # @return [Twitter::Relationship]
176
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
177
+ # @param options [Hash] A customizable set of options.
178
+ # @option options [Boolean] :device Enable/disable device notifications from the target user.
179
+ # @option options [Boolean] :retweets Enable/disable retweets from the target user.
180
+ def friendship_update(user, options={})
181
+ merge_user!(options, user)
182
+ object_from_response(Twitter::Relationship, :post, "/1.1/friendships/update.json", options)
183
+ end
184
+
185
+ # Returns detailed information about the relationship between two users
186
+ #
187
+ # @see https://dev.twitter.com/docs/api/1.1/get/friendships/show
188
+ # @rate_limited Yes
189
+ # @authentication Requires user context
190
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
191
+ # @return [Twitter::Relationship]
192
+ # @param source [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the source user.
193
+ # @param target [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the target user.
194
+ # @param options [Hash] A customizable set of options.
195
+ def friendship(source, target, options={})
196
+ merge_user!(options, source, "source")
197
+ options[:source_id] = options.delete(:source_user_id) unless options[:source_user_id].nil?
198
+ merge_user!(options, target, "target")
199
+ options[:target_id] = options.delete(:target_user_id) unless options[:target_user_id].nil?
200
+ object_from_response(Twitter::Relationship, :get, "/1.1/friendships/show.json", options)
201
+ end
202
+ alias friendship_show friendship
203
+ alias relationship friendship
204
+
205
+ # Test for the existence of friendship between two users
206
+ #
207
+ # @see https://dev.twitter.com/docs/api/1.1/get/friendships/show
208
+ # @rate_limited Yes
209
+ # @authentication Requires user context
210
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
211
+ # @return [Boolean] true if user_a follows user_b, otherwise false.
212
+ # @param source [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the source user.
213
+ # @param target [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the target user.
214
+ # @param options [Hash] A customizable set of options.
215
+ def friendship?(source, target, options={})
216
+ friendship(source, target, options).source.following?
217
+ end
218
+
219
+ # Returns a cursored collection of user objects for users following the specified user.
220
+ #
221
+ # @see https://dev.twitter.com/docs/api/1.1/get/followers/list
222
+ # @rate_limited Yes
223
+ # @authentication Requires user context
224
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
225
+ # @return [Twitter::Cursor]
226
+ # @overload followers(options={})
227
+ # Returns a cursored collection of user objects for users following the authenticated user.
228
+ #
229
+ # @param options [Hash] A customizable set of options.
230
+ # @option options [Integer] :cursor (-1) Breaks the results into pages. This is recommended for users who are following many users. Provide a value of -1 to begin paging. Provide values as returned in the response body's next_cursor and previous_cursor attributes to page back and forth in the list.
231
+ # @option options [Boolean, String, Integer] :skip_status Do not include contributee's Tweets when set to true, 't' or 1.
232
+ # @option options [Boolean, String, Integer] :include_user_entities The user entities node will be disincluded when set to false.
233
+ # @overload followers(user, options={})
234
+ # Returns a cursored collection of user objects for users following the specified user.
235
+ #
236
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
237
+ # @param options [Hash] A customizable set of options.
238
+ # @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.
239
+ # @option options [Boolean, String, Integer] :skip_status Do not include contributee's Tweets when set to true, 't' or 1.
240
+ # @option options [Boolean, String, Integer] :include_user_entities The user entities node will be disincluded when set to false.
241
+ def followers(*args)
242
+ cursor_from_response_with_user(:users, Twitter::User, :get, "/1.1/followers/list.json", args)
243
+ end
244
+
245
+ # Returns a cursored collection of user objects for every user the specified user is following (otherwise known as their "friends").
246
+ #
247
+ # @see https://dev.twitter.com/docs/api/1.1/get/friendships/show
248
+ # @rate_limited Yes
249
+ # @authentication Requires user context
250
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
251
+ # @return [Twitter::Cursor]
252
+ # @overload friends(options={})
253
+ # Returns a cursored collection of user objects for every user the authenticated user is following (otherwise known as their "friends").
254
+ #
255
+ # @param options [Hash] A customizable set of options.
256
+ # @option options [Integer] :cursor (-1) Breaks the results into pages. This is recommended for users who are following many users. Provide a value of -1 to begin paging. Provide values as returned in the response body's next_cursor and previous_cursor attributes to page back and forth in the list.
257
+ # @option options [Boolean, String, Integer] :skip_status Do not include contributee's Tweets when set to true, 't' or 1.
258
+ # @option options [Boolean, String, Integer] :include_user_entities The user entities node will be disincluded when set to false.
259
+ # @overload friends(user, options={})
260
+ # Returns a cursored collection of user objects for every user the specified user is following (otherwise known as their "friends").
261
+ #
262
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
263
+ # @param options [Hash] A customizable set of options.
264
+ # @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.
265
+ # @option options [Boolean, String, Integer] :skip_status Do not include contributee's Tweets when set to true, 't' or 1.
266
+ # @option options [Boolean, String, Integer] :include_user_entities The user entities node will be disincluded when set to false.
267
+ def friends(*args)
268
+ cursor_from_response_with_user(:users, Twitter::User, :get, "/1.1/friends/list.json", args)
269
+ end
270
+ alias following friends
271
+
272
+ # Returns a collection of user IDs that the currently authenticated user does not want to receive retweets from.
273
+ # @see https://dev.twitter.com/docs/api/1.1/get/friendships/no_retweets/ids
274
+ # @rate_limited Yes
275
+ # @authentication Requires user context
276
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
277
+ # @return [Array<Integer>]
278
+ # @param options [Hash] A customizable set of options.
279
+ def no_retweet_ids(options={})
280
+ get("/1.1/friendships/no_retweets/ids.json", options)[:body].map(&:to_i)
281
+ end
282
+ alias no_retweets_ids no_retweet_ids
283
+
284
+ end
285
+ end
286
+ end
287
+ end