twitter 6.2.0 → 8.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +32 -10
  3. data/LICENSE.md +1 -1
  4. data/README.md +9 -14
  5. data/lib/twitter/base.rb +11 -11
  6. data/lib/twitter/basic_user.rb +3 -2
  7. data/lib/twitter/client.rb +8 -8
  8. data/lib/twitter/creatable.rb +7 -3
  9. data/lib/twitter/cursor.rb +15 -5
  10. data/lib/twitter/direct_message.rb +4 -3
  11. data/lib/twitter/direct_message_event.rb +44 -0
  12. data/lib/twitter/direct_messages/welcome_message.rb +17 -0
  13. data/lib/twitter/direct_messages/welcome_message_rule.rb +12 -0
  14. data/lib/twitter/direct_messages/welcome_message_rule_wrapper.rb +36 -0
  15. data/lib/twitter/direct_messages/welcome_message_wrapper.rb +42 -0
  16. data/lib/twitter/entities.rb +6 -6
  17. data/lib/twitter/entity/hashtag.rb +1 -1
  18. data/lib/twitter/entity/symbol.rb +1 -1
  19. data/lib/twitter/entity/uri.rb +1 -1
  20. data/lib/twitter/entity/user_mention.rb +1 -1
  21. data/lib/twitter/entity.rb +1 -1
  22. data/lib/twitter/enumerable.rb +15 -6
  23. data/lib/twitter/error.rb +52 -9
  24. data/lib/twitter/factory.rb +1 -1
  25. data/lib/twitter/geo/point.rb +1 -1
  26. data/lib/twitter/geo/polygon.rb +1 -1
  27. data/lib/twitter/geo.rb +2 -2
  28. data/lib/twitter/geo_factory.rb +3 -3
  29. data/lib/twitter/geo_results.rb +2 -2
  30. data/lib/twitter/headers.rb +4 -4
  31. data/lib/twitter/identity.rb +2 -2
  32. data/lib/twitter/language.rb +1 -1
  33. data/lib/twitter/list.rb +3 -2
  34. data/lib/twitter/media/animated_gif.rb +1 -1
  35. data/lib/twitter/media/photo.rb +2 -2
  36. data/lib/twitter/media/video.rb +3 -3
  37. data/lib/twitter/media/video_info.rb +2 -2
  38. data/lib/twitter/media_factory.rb +4 -4
  39. data/lib/twitter/metadata.rb +1 -1
  40. data/lib/twitter/null_object.rb +19 -5
  41. data/lib/twitter/oembed.rb +2 -1
  42. data/lib/twitter/place.rb +3 -3
  43. data/lib/twitter/premium_search_results.rb +67 -0
  44. data/lib/twitter/profile.rb +6 -6
  45. data/lib/twitter/profile_banner.rb +2 -2
  46. data/lib/twitter/rate_limit.rb +6 -6
  47. data/lib/twitter/relationship.rb +2 -1
  48. data/lib/twitter/rest/account_activity.rb +99 -0
  49. data/lib/twitter/rest/api.rb +22 -16
  50. data/lib/twitter/rest/client.rb +4 -4
  51. data/lib/twitter/rest/direct_messages/welcome_messages.rb +90 -0
  52. data/lib/twitter/rest/direct_messages.rb +136 -47
  53. data/lib/twitter/rest/favorites.rb +17 -21
  54. data/lib/twitter/rest/form_encoder.rb +27 -0
  55. data/lib/twitter/rest/friends_and_followers.rb +21 -21
  56. data/lib/twitter/rest/help.rb +6 -18
  57. data/lib/twitter/rest/lists.rb +33 -32
  58. data/lib/twitter/rest/oauth.rb +8 -8
  59. data/lib/twitter/rest/places_and_geo.rb +6 -6
  60. data/lib/twitter/rest/premium_search.rb +34 -0
  61. data/lib/twitter/rest/request.rb +64 -31
  62. data/lib/twitter/rest/saved_searches.rb +6 -6
  63. data/lib/twitter/rest/search.rb +6 -5
  64. data/lib/twitter/rest/spam_reporting.rb +3 -3
  65. data/lib/twitter/rest/suggested_users.rb +5 -5
  66. data/lib/twitter/rest/timelines.rb +8 -7
  67. data/lib/twitter/rest/trends.rb +7 -7
  68. data/lib/twitter/rest/tweets.rb +20 -53
  69. data/lib/twitter/rest/undocumented.rb +7 -7
  70. data/lib/twitter/rest/upload_utils.rb +68 -0
  71. data/lib/twitter/rest/users.rb +32 -32
  72. data/lib/twitter/rest/utils.rb +41 -24
  73. data/lib/twitter/saved_search.rb +2 -2
  74. data/lib/twitter/search_results.rb +10 -9
  75. data/lib/twitter/settings.rb +2 -1
  76. data/lib/twitter/size.rb +2 -2
  77. data/lib/twitter/source_user.rb +1 -1
  78. data/lib/twitter/streaming/client.rb +19 -15
  79. data/lib/twitter/streaming/connection.rb +19 -7
  80. data/lib/twitter/streaming/message_parser.rb +7 -7
  81. data/lib/twitter/streaming/response.rb +12 -10
  82. data/lib/twitter/suggestion.rb +3 -3
  83. data/lib/twitter/target_user.rb +1 -1
  84. data/lib/twitter/trend.rb +3 -2
  85. data/lib/twitter/trend_results.rb +5 -5
  86. data/lib/twitter/tweet.rb +13 -4
  87. data/lib/twitter/user.rb +9 -9
  88. data/lib/twitter/utils.rb +6 -4
  89. data/lib/twitter/variant.rb +2 -1
  90. data/lib/twitter/version.rb +4 -4
  91. data/lib/twitter.rb +36 -31
  92. data/twitter.gemspec +19 -19
  93. metadata +33 -37
  94. data/lib/twitter/configuration.rb +0 -27
@@ -1,32 +1,38 @@
1
- require 'twitter/rest/direct_messages'
2
- require 'twitter/rest/favorites'
3
- require 'twitter/rest/friends_and_followers'
4
- require 'twitter/rest/help'
5
- require 'twitter/rest/lists'
6
- require 'twitter/rest/oauth'
7
- require 'twitter/rest/places_and_geo'
8
- require 'twitter/rest/saved_searches'
9
- require 'twitter/rest/search'
10
- require 'twitter/rest/spam_reporting'
11
- require 'twitter/rest/suggested_users'
12
- require 'twitter/rest/timelines'
13
- require 'twitter/rest/trends'
14
- require 'twitter/rest/tweets'
15
- require 'twitter/rest/undocumented'
16
- require 'twitter/rest/users'
1
+ require "twitter/rest/account_activity"
2
+ require "twitter/rest/direct_messages"
3
+ require "twitter/rest/direct_messages/welcome_messages"
4
+ require "twitter/rest/favorites"
5
+ require "twitter/rest/friends_and_followers"
6
+ require "twitter/rest/help"
7
+ require "twitter/rest/lists"
8
+ require "twitter/rest/oauth"
9
+ require "twitter/rest/places_and_geo"
10
+ require "twitter/rest/saved_searches"
11
+ require "twitter/rest/search"
12
+ require "twitter/rest/premium_search"
13
+ require "twitter/rest/spam_reporting"
14
+ require "twitter/rest/suggested_users"
15
+ require "twitter/rest/timelines"
16
+ require "twitter/rest/trends"
17
+ require "twitter/rest/tweets"
18
+ require "twitter/rest/undocumented"
19
+ require "twitter/rest/users"
17
20
 
18
21
  module Twitter
19
22
  module REST
20
23
  # @note All methods have been separated into modules and follow the same grouping used in {http://dev.twitter.com/doc the Twitter API Documentation}.
21
24
  # @see https://dev.twitter.com/overview/general/things-every-developer-should-know
22
25
  module API
26
+ include Twitter::REST::AccountActivity
23
27
  include Twitter::REST::DirectMessages
28
+ include Twitter::REST::DirectMessages::WelcomeMessages
24
29
  include Twitter::REST::Favorites
25
30
  include Twitter::REST::FriendsAndFollowers
26
31
  include Twitter::REST::Help
27
32
  include Twitter::REST::Lists
28
33
  include Twitter::REST::OAuth
29
34
  include Twitter::REST::PlacesAndGeo
35
+ include Twitter::REST::PremiumSearch
30
36
  include Twitter::REST::SavedSearches
31
37
  include Twitter::REST::Search
32
38
  include Twitter::REST::SpamReporting
@@ -1,7 +1,7 @@
1
- require 'twitter/client'
2
- require 'twitter/rest/api'
3
- require 'twitter/rest/request'
4
- require 'twitter/rest/utils'
1
+ require "twitter/client"
2
+ require "twitter/rest/api"
3
+ require "twitter/rest/request"
4
+ require "twitter/rest/utils"
5
5
 
6
6
  module Twitter
7
7
  module REST
@@ -0,0 +1,90 @@
1
+ require "twitter/arguments"
2
+ require "twitter/rest/upload_utils"
3
+ require "twitter/rest/utils"
4
+ require "twitter/utils"
5
+
6
+ module Twitter
7
+ module REST
8
+ module DirectMessages
9
+ module WelcomeMessages
10
+ include Twitter::REST::UploadUtils
11
+ include Twitter::REST::Utils
12
+ include Twitter::Utils
13
+
14
+ # Welcome Message
15
+
16
+ def create_welcome_message(text, name = nil, options = {})
17
+ json_options = {
18
+ welcome_message: {
19
+ message_data: {
20
+ text: text,
21
+ },
22
+ },
23
+ }
24
+ json_options[:welcome_message][:name] = name if name
25
+ welcome_message_wrapper = perform_request_with_object(:json_post, "/1.1/direct_messages/welcome_messages/new.json", json_options.merge!(options), Twitter::DirectMessages::WelcomeMessageWrapper)
26
+ welcome_message_wrapper.welcome_message
27
+ end
28
+
29
+ def destroy_welcome_message(*ids)
30
+ perform_requests(:delete, "/1.1/direct_messages/welcome_messages/destroy.json", ids)
31
+ end
32
+
33
+ def update_welcome_message(welcome_message_id, text, options = {})
34
+ params = {
35
+ id: welcome_message_id,
36
+ }
37
+ json_options = {
38
+ message_data: {
39
+ text: text,
40
+ },
41
+ }
42
+ welcome_message_wrapper = perform_request_with_object(:json_put, "/1.1/direct_messages/welcome_messages/update.json", json_options.merge!(options), Twitter::DirectMessages::WelcomeMessageWrapper, params)
43
+ welcome_message_wrapper.welcome_message
44
+ end
45
+
46
+ def welcome_message(id, options = {})
47
+ options = options.dup
48
+ options[:id] = id
49
+ welcome_message_wrapper = perform_get_with_object("/1.1/direct_messages/welcome_messages/show.json", options, Twitter::DirectMessages::WelcomeMessageWrapper)
50
+ welcome_message_wrapper.welcome_message
51
+ end
52
+
53
+ def welcome_message_list(options = {})
54
+ limit = options.fetch(:count, 20)
55
+ welcome_message_wrappers = perform_get_with_cursor("/1.1/direct_messages/welcome_messages/list.json", options.merge!(no_default_cursor: true, count: 50, limit: limit), :welcome_messages, Twitter::DirectMessages::WelcomeMessageWrapper)
56
+ welcome_message_wrappers.collect(&:welcome_message)
57
+ end
58
+
59
+ # Welcome Message Rule
60
+
61
+ def create_welcome_message_rule(welcome_message_id, options = {})
62
+ json_options = {
63
+ welcome_message_rule: {
64
+ welcome_message_id: welcome_message_id,
65
+ },
66
+ }
67
+ rule_wrapper = perform_request_with_object(:json_post, "/1.1/direct_messages/welcome_messages/rules/new.json", json_options.merge!(options), Twitter::DirectMessages::WelcomeMessageRuleWrapper)
68
+ rule_wrapper.welcome_message_rule
69
+ end
70
+
71
+ def destroy_welcome_message_rule(*ids)
72
+ perform_requests(:delete, "/1.1/direct_messages/welcome_messages/rules/destroy.json", ids)
73
+ end
74
+
75
+ def welcome_message_rule(id, options = {})
76
+ options = options.dup
77
+ options[:id] = id
78
+ rule_wrapper = perform_get_with_object("/1.1/direct_messages/welcome_messages/rules/show.json", options, Twitter::DirectMessages::WelcomeMessageRuleWrapper)
79
+ rule_wrapper.welcome_message_rule
80
+ end
81
+
82
+ def welcome_message_rule_list(options = {})
83
+ limit = options.fetch(:count, 20)
84
+ rule_wrappers = perform_get_with_cursor("/1.1/direct_messages/welcome_messages/rules/list.json", options.merge!(no_default_cursor: true, count: 50, limit: limit), :welcome_message_rules, Twitter::DirectMessages::WelcomeMessageRuleWrapper)
85
+ rule_wrappers.collect(&:welcome_message_rule)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -1,65 +1,111 @@
1
- require 'twitter/arguments'
2
- require 'twitter/direct_message'
3
- require 'twitter/rest/utils'
4
- require 'twitter/user'
5
- require 'twitter/utils'
1
+ require "twitter/arguments"
2
+ require "twitter/direct_message"
3
+ require "twitter/direct_message_event"
4
+ require "twitter/rest/upload_utils"
5
+ require "twitter/rest/utils"
6
+ require "twitter/user"
7
+ require "twitter/utils"
6
8
 
7
9
  module Twitter
8
10
  module REST
9
11
  module DirectMessages
12
+ include Twitter::REST::UploadUtils
10
13
  include Twitter::REST::Utils
11
14
  include Twitter::Utils
12
15
 
13
- # Returns the 20 most recent direct messages sent to the authenticating user
14
- #
15
- # @see https://dev.twitter.com/rest/reference/get/direct_messages
16
+ # Returns all Direct Message events for the authenticated user (both sent and received) within the last 30 days. Sorted in reverse-chronological order.
17
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events
18
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
19
+ # @rate_limited Yes
20
+ # @authentication Requires user context
21
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
22
+ # @return [Array<Twitter::DirectMessageEvent>] Direct message events sent by and received by the authenticating user.
23
+ # @param options [Hash] A customizable set of options.
24
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 50. Default is 20
25
+ # @option options [String] :cursor Specifies the cursor position of results to retrieve.
26
+ def direct_messages_events(options = {})
27
+ limit = options.fetch(:count, 20)
28
+ perform_get_with_cursor("/1.1/direct_messages/events/list.json", options.merge!(no_default_cursor: true, count: 50, limit: limit), :events, Twitter::DirectMessageEvent)
29
+ end
30
+
31
+ # Returns all Direct Messages for the authenticated user (both sent and received) within the last 30 days. Sorted in reverse-chronological order.
32
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events
33
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
34
+ # @rate_limited Yes
35
+ # @authentication Requires user context
36
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
37
+ # @return [Array<Twitter::DirectMessage>] Direct messages sent by and received by the authenticating user.
38
+ # @param options [Hash] A customizable set of options.
39
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 50. Default is 20
40
+ # @option options [String] :cursor Specifies the cursor position of results to retrieve.
41
+ def direct_messages_list(options = {})
42
+ direct_messages_events(options).collect(&:direct_message)
43
+ end
44
+
45
+ # Returns Direct Messages received by the authenticated user within the last 30 days. Sorted in reverse-chronological order.
46
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events
16
47
  # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
17
48
  # @rate_limited Yes
18
49
  # @authentication Requires user context
19
50
  # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
20
- # @return [Array<Twitter::DirectMessage>] Direct messages sent to the authenticating user.
51
+ # @return [Array<Twitter::DirectMessage>] Direct messages received by the authenticating user.
21
52
  # @param options [Hash] A customizable set of options.
22
- # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
23
- # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
24
- # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
25
- # @option options [Integer] :page Specifies the page of results to retrieve.
53
+ # @option options [Integer] :count Specifies the number of records (sent and received dms) to retrieve. Must be less than or equal to 50. Default is 50
54
+ # this count does not directly correspond to the output, as we pull sent and received messages from twitter and only present received to the user
55
+ # @option options [String] :cursor Specifies the cursor position of results to retrieve.
26
56
  def direct_messages_received(options = {})
27
- perform_get_with_objects('/1.1/direct_messages.json', options, Twitter::DirectMessage)
57
+ limit = options.fetch(:count, 20)
58
+ direct_messages_list(options).select { |dm| dm.recipient_id == user_id }.first(limit)
28
59
  end
29
60
 
30
- # Returns the 20 most recent direct messages sent by the authenticating user
31
- #
32
- # @see https://dev.twitter.com/rest/reference/get/direct_messages/sent
61
+ # Returns Direct Messages sent by the authenticated user within the last 30 days. Sorted in reverse-chronological order.
62
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events
33
63
  # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
34
64
  # @rate_limited Yes
35
65
  # @authentication Requires user context
36
66
  # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
37
67
  # @return [Array<Twitter::DirectMessage>] Direct messages sent by the authenticating user.
38
68
  # @param options [Hash] A customizable set of options.
39
- # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
40
- # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
41
- # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
42
- # @option options [Integer] :page Specifies the page of results to retrieve.
69
+ # @option options [Integer] :count Specifies the number of records (sent and received dms) to retrieve. Must be less than or equal to 50. Default is 50
70
+ # this count does not directly correspond to the output, as we pull sent and received messages from twitter and only present received to the user
71
+ # @option options [String] :cursor Specifies the cursor position of results to retrieve.
43
72
  def direct_messages_sent(options = {})
44
- perform_get_with_objects('/1.1/direct_messages/sent.json', options, Twitter::DirectMessage)
73
+ limit = options.fetch(:count, 20)
74
+ direct_messages_list(options).select { |dm| dm.sender_id == user_id }.first(limit)
45
75
  end
46
76
 
47
77
  # Returns a direct message
48
78
  #
49
- # @see https://dev.twitter.com/rest/reference/get/direct_messages/show
79
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/get-event
50
80
  # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
51
81
  # @rate_limited Yes
52
82
  # @authentication Requires user context
53
83
  # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
54
- # @return [Twitter::DirectMessage] The requested messages.
84
+ # @return [Twitter::DirectMessage] The requested message.
55
85
  # @param id [Integer] A direct message ID.
56
86
  # @param options [Hash] A customizable set of options.
87
+
57
88
  def direct_message(id, options = {})
89
+ direct_message_event(id, options).direct_message
90
+ end
91
+
92
+ # Returns a direct message event
93
+ #
94
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/get-event
95
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
96
+ # @rate_limited Yes
97
+ # @authentication Requires user context
98
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
99
+ # @return [Twitter::DirectMessageEvent] The requested message.
100
+ # @param id [Integer] A direct message ID.
101
+ # @param options [Hash] A customizable set of options.
102
+ def direct_message_event(id, options = {})
58
103
  options = options.dup
59
104
  options[:id] = id
60
- perform_get_with_object('/1.1/direct_messages/show.json', options, Twitter::DirectMessage)
105
+ perform_get_with_object("/1.1/direct_messages/events/show.json", options, Twitter::DirectMessageEvent)
61
106
  end
62
107
 
108
+ # Returns direct messages specified in arguments, or, if no arguments are given, returns direct messages received by authenticating user
63
109
  # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
64
110
  # @rate_limited Yes
65
111
  # @authentication Requires user context
@@ -67,13 +113,12 @@ module Twitter
67
113
  # @return [Array<Twitter::DirectMessage>] The requested messages.
68
114
  # @overload direct_messages(options = {})
69
115
  # Returns the 20 most recent direct messages sent to the authenticating user
70
- #
71
- # @see https://dev.twitter.com/rest/reference/get/direct_messages
116
+
117
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events
72
118
  # @param options [Hash] A customizable set of options.
73
- # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
74
- # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
75
- # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
76
- # @option options [Integer] :page Specifies the page of results to retrieve.
119
+ # @option options [Integer] :count Specifies the number of records (sent and received dms) to retrieve. Must be less than or equal to 50. Default is 50
120
+ # this count does not directly correspond to the output, as we pull sent and received messages from twitter and only present received to the user
121
+ # @option options [String] :cursor Specifies the cursor position of results to retrieve.
77
122
  # @overload direct_messages(*ids)
78
123
  # Returns direct messages
79
124
  #
@@ -98,40 +143,84 @@ module Twitter
98
143
 
99
144
  # Destroys direct messages
100
145
  #
101
- # @see https://dev.twitter.com/rest/reference/post/direct_messages/destroy
146
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/delete-message-event
102
147
  # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
103
- # @rate_limited No
148
+ # @rate_limited Yes
104
149
  # @authentication Requires user context
105
150
  # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
106
- # @return [Array<Twitter::DirectMessage>] Deleted direct message.
151
+ # @return [nil] Response body from Twitter is nil if successful
107
152
  # @overload destroy_direct_message(*ids)
108
153
  # @param ids [Enumerable<Integer>] A collection of direct message IDs.
109
- # @overload destroy_direct_message(*ids, options)
110
- # @param ids [Enumerable<Integer>] A collection of direct message IDs.
111
- # @param options [Hash] A customizable set of options.
112
- def destroy_direct_message(*args)
113
- parallel_objects_from_response(Twitter::DirectMessage, :post, '/1.1/direct_messages/destroy.json', args)
154
+ def destroy_direct_message(*ids)
155
+ pmap(ids) do |id|
156
+ perform_requests(:delete, "/1.1/direct_messages/events/destroy.json", id: id)
157
+ end
158
+ nil
114
159
  end
115
160
 
116
161
  # Sends a new direct message to the specified user from the authenticating user
117
162
  #
118
- # @see https://dev.twitter.com/rest/reference/post/direct_messages/new
119
- # @rate_limited No
163
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event
164
+ # @rate_limited Yes
120
165
  # @authentication Requires user context
121
166
  # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
122
167
  # @return [Twitter::DirectMessage] The sent message.
123
- # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
168
+ # @param user [Integer, String, Twitter::User] A Twitter user ID
124
169
  # @param text [String] The text of your direct message, up to 10,000 characters.
125
170
  # @param options [Hash] A customizable set of options.
126
- def create_direct_message(user, text, options = {})
127
- options = options.dup
128
- merge_user!(options, user)
129
- options[:text] = text
130
- perform_post_with_object('/1.1/direct_messages/new.json', options, Twitter::DirectMessage)
171
+ def create_direct_message(user_id, text, options = {})
172
+ event = perform_request_with_object(:json_post, "/1.1/direct_messages/events/new.json", format_json_options(user_id, text, options), Twitter::DirectMessageEvent)
173
+ event.direct_message
131
174
  end
132
175
  alias d create_direct_message
133
176
  alias m create_direct_message
134
177
  alias dm create_direct_message
178
+
179
+ # Create a new direct message event to the specified user from the authenticating user
180
+ #
181
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event
182
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
183
+ # @rate_limited Yes
184
+ # @authentication Requires user context
185
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
186
+ # @return [Twitter::DirectMessageEvent] The created direct message event.
187
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
188
+ # @param text [String] The text of your direct message, up to 10,000 characters.
189
+ # @param options [Hash] A customizable set of options.
190
+ def create_direct_message_event(*args)
191
+ arguments = Twitter::Arguments.new(args)
192
+ options = arguments.options.dup
193
+ options[:event] = {type: "message_create", message_create: {target: {recipient_id: extract_id(arguments[0])}, message_data: {text: arguments[1]}}} if arguments.length >= 2
194
+ response = Twitter::REST::Request.new(self, :json_post, "/1.1/direct_messages/events/new.json", options).perform
195
+ Twitter::DirectMessageEvent.new(response[:event])
196
+ end
197
+
198
+ # Create a new direct message event to the specified user from the authenticating user with media
199
+ #
200
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event
201
+ # @see https://developer.twitter.com/en/docs/direct-messages/message-attachments/guides/attaching-media
202
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
203
+ # @rate_limited Yes
204
+ # @authentication Requires user context
205
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
206
+ # @return [Twitter::DirectMessageEvent] The created direct message event.
207
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
208
+ # @param text [String] The text of your direct message, up to 10,000 characters.
209
+ # @param media [File] A media file (PNG, JPEG, GIF or MP4).
210
+ # @param options [Hash] A customizable set of options.
211
+ def create_direct_message_event_with_media(user, text, media, options = {})
212
+ media_id = upload(media, media_category_prefix: "dm")[:media_id]
213
+ options = options.dup
214
+ options[:event] = {type: "message_create", message_create: {target: {recipient_id: extract_id(user)}, message_data: {text: text, attachment: {type: "media", media: {id: media_id}}}}}
215
+ response = Twitter::REST::Request.new(self, :json_post, "/1.1/direct_messages/events/new.json", options).perform
216
+ Twitter::DirectMessageEvent.new(response[:event])
217
+ end
218
+
219
+ private
220
+
221
+ def format_json_options(user_id, text, options)
222
+ {event: {type: "message_create", message_create: {target: {recipient_id: user_id}, message_data: {text: text}.merge(options)}}}
223
+ end
135
224
  end
136
225
  end
137
226
  end
@@ -1,9 +1,9 @@
1
- require 'twitter/arguments'
2
- require 'twitter/error'
3
- require 'twitter/rest/utils'
4
- require 'twitter/tweet'
5
- require 'twitter/user'
6
- require 'twitter/utils'
1
+ require "twitter/arguments"
2
+ require "twitter/error"
3
+ require "twitter/rest/utils"
4
+ require "twitter/tweet"
5
+ require "twitter/user"
6
+ require "twitter/utils"
7
7
 
8
8
  module Twitter
9
9
  module REST
@@ -20,19 +20,19 @@ module Twitter
20
20
  # Returns the 20 most recent favorite Tweets for the authenticating user
21
21
  #
22
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.
23
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
24
24
  # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
25
25
  # @overload favorites(user, options = {})
26
26
  # Returns the 20 most recent favorite Tweets for the specified user
27
27
  #
28
28
  # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
29
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.
30
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
31
31
  # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
32
32
  def favorites(*args)
33
33
  arguments = Twitter::Arguments.new(args)
34
34
  merge_user!(arguments.options, arguments.pop) if arguments.last
35
- perform_get_with_objects('/1.1/favorites/list.json', arguments.options, Twitter::Tweet)
35
+ perform_get_with_objects("/1.1/favorites/list.json", arguments.options, Twitter::Tweet)
36
36
  end
37
37
 
38
38
  # Un-favorites the specified Tweets as the authenticating user
@@ -50,11 +50,9 @@ module Twitter
50
50
  def unfavorite(*args)
51
51
  arguments = Twitter::Arguments.new(args)
52
52
  pmap(arguments) do |tweet|
53
- begin
54
- perform_post_with_object('/1.1/favorites/destroy.json', arguments.options.merge(id: extract_id(tweet)), Twitter::Tweet)
55
- rescue Twitter::Error::NotFound
56
- next
57
- end
53
+ perform_post_with_object("/1.1/favorites/destroy.json", arguments.options.merge(id: extract_id(tweet)), Twitter::Tweet)
54
+ rescue Twitter::Error::NotFound
55
+ next
58
56
  end.compact
59
57
  end
60
58
  alias destroy_favorite unfavorite
@@ -73,7 +71,7 @@ module Twitter
73
71
  # @param tweets [Enumerable<Integer, String, URI, Twitter::Tweet>] A collection of Tweet IDs, URIs, or objects.
74
72
  # @param options [Hash] A customizable set of options.
75
73
  def unfavorite!(*args)
76
- parallel_objects_from_response(Twitter::Tweet, :post, '/1.1/favorites/destroy.json', args)
74
+ parallel_objects_from_response(Twitter::Tweet, :post, "/1.1/favorites/destroy.json", args)
77
75
  end
78
76
 
79
77
  # Favorites the specified Tweets as the authenticating user
@@ -91,11 +89,9 @@ module Twitter
91
89
  def favorite(*args)
92
90
  arguments = Twitter::Arguments.new(args)
93
91
  pmap(arguments) do |tweet|
94
- begin
95
- perform_post_with_object('/1.1/favorites/create.json', arguments.options.merge(id: extract_id(tweet)), Twitter::Tweet)
96
- rescue Twitter::Error::AlreadyFavorited, Twitter::Error::NotFound
97
- next
98
- end
92
+ perform_post_with_object("/1.1/favorites/create.json", arguments.options.merge(id: extract_id(tweet)), Twitter::Tweet)
93
+ rescue Twitter::Error::AlreadyFavorited, Twitter::Error::NotFound
94
+ next
99
95
  end.compact
100
96
  end
101
97
  alias fav favorite
@@ -118,7 +114,7 @@ module Twitter
118
114
  def favorite!(*args)
119
115
  arguments = Twitter::Arguments.new(args)
120
116
  pmap(arguments) do |tweet|
121
- perform_post_with_object('/1.1/favorites/create.json', arguments.options.merge(id: extract_id(tweet)), Twitter::Tweet)
117
+ perform_post_with_object("/1.1/favorites/create.json", arguments.options.merge(id: extract_id(tweet)), Twitter::Tweet)
122
118
  end
123
119
  end
124
120
  alias create_favorite! favorite!
@@ -0,0 +1,27 @@
1
+ module Twitter
2
+ module REST
3
+ class FormEncoder
4
+ UNESCAPED_CHARS = /[^a-z0-9\-._~]/i
5
+
6
+ def self.encode(data)
7
+ data.collect do |k, v|
8
+ if v.nil?
9
+ ::URI::DEFAULT_PARSER.escape(k.to_s, UNESCAPED_CHARS)
10
+ elsif v.respond_to?(:to_ary)
11
+ v.to_ary.collect do |w|
12
+ str = ::URI::DEFAULT_PARSER.escape(k.to_s, UNESCAPED_CHARS)
13
+ unless w.nil?
14
+ str << "="
15
+ str << ::URI::DEFAULT_PARSER.escape(w.to_s, UNESCAPED_CHARS)
16
+ end
17
+ end.join("&")
18
+ else
19
+ str = ::URI::DEFAULT_PARSER.escape(k.to_s, UNESCAPED_CHARS)
20
+ str << "="
21
+ str << ::URI::DEFAULT_PARSER.escape(v.to_s, UNESCAPED_CHARS)
22
+ end
23
+ end.join("&")
24
+ end
25
+ end
26
+ end
27
+ end