twitter 5.6.0 → 5.7.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 (93) hide show
  1. data.tar.gz.sig +0 -0
  2. data/.yardopts +1 -0
  3. data/CHANGELOG.md +8 -0
  4. data/README.md +3 -3
  5. data/Rakefile +1 -1
  6. data/lib/twitter/base.rb +0 -8
  7. data/lib/twitter/client.rb +2 -1
  8. data/lib/twitter/cursor.rb +2 -15
  9. data/lib/twitter/error.rb +90 -18
  10. data/lib/twitter/geo_results.rb +0 -10
  11. data/lib/twitter/request.rb +4 -4
  12. data/lib/twitter/rest/api.rb +43 -0
  13. data/lib/twitter/rest/client.rb +10 -44
  14. data/lib/twitter/rest/direct_messages.rb +138 -0
  15. data/lib/twitter/rest/favorites.rb +108 -0
  16. data/lib/twitter/rest/friends_and_followers.rb +281 -0
  17. data/lib/twitter/rest/help.rb +56 -0
  18. data/lib/twitter/rest/lists.rb +497 -0
  19. data/lib/twitter/rest/oauth.rb +65 -0
  20. data/lib/twitter/rest/places_and_geo.rb +84 -0
  21. data/lib/twitter/rest/request/multipart_with_file.rb +5 -5
  22. data/lib/twitter/rest/response/parse_json.rb +2 -4
  23. data/lib/twitter/rest/response/raise_error.rb +24 -15
  24. data/lib/twitter/rest/saved_searches.rb +93 -0
  25. data/lib/twitter/rest/search.rb +37 -0
  26. data/lib/twitter/rest/spam_reporting.rb +27 -0
  27. data/lib/twitter/rest/suggested_users.rb +49 -0
  28. data/lib/twitter/rest/timelines.rb +200 -0
  29. data/lib/twitter/rest/trends.rb +57 -0
  30. data/lib/twitter/rest/tweets.rb +309 -0
  31. data/lib/twitter/rest/undocumented.rb +49 -0
  32. data/lib/twitter/rest/users.rb +383 -0
  33. data/lib/twitter/rest/utils.rb +205 -0
  34. data/lib/twitter/search_results.rb +2 -13
  35. data/lib/twitter/trend_results.rb +0 -10
  36. data/lib/twitter/version.rb +1 -1
  37. data/spec/helper.rb +1 -1
  38. data/spec/twitter/error_spec.rb +26 -42
  39. data/spec/twitter/rest/{api/direct_messages_spec.rb → direct_messages_spec.rb} +1 -1
  40. data/spec/twitter/rest/{api/favorites_spec.rb → favorites_spec.rb} +1 -1
  41. data/spec/twitter/rest/{api/friends_and_followers_spec.rb → friends_and_followers_spec.rb} +1 -1
  42. data/spec/twitter/rest/{api/geo_spec.rb → geo_spec.rb} +1 -1
  43. data/spec/twitter/rest/{api/help_spec.rb → help_spec.rb} +1 -1
  44. data/spec/twitter/rest/{api/lists_spec.rb → lists_spec.rb} +1 -1
  45. data/spec/twitter/rest/{api/oauth_spec.rb → oauth_spec.rb} +4 -8
  46. data/spec/twitter/rest/{api/saved_searches_spec.rb → saved_searches_spec.rb} +1 -1
  47. data/spec/twitter/rest/{api/search_spec.rb → search_spec.rb} +1 -1
  48. data/spec/twitter/rest/{api/spam_reporting_spec.rb → spam_reporting_spec.rb} +1 -1
  49. data/spec/twitter/rest/{api/suggested_users_spec.rb → suggested_users_spec.rb} +1 -1
  50. data/spec/twitter/rest/{api/timelines_spec.rb → timelines_spec.rb} +1 -1
  51. data/spec/twitter/rest/{api/trends_spec.rb → trends_spec.rb} +1 -1
  52. data/spec/twitter/rest/{api/tweets_spec.rb → tweets_spec.rb} +74 -5
  53. data/spec/twitter/rest/{api/undocumented_spec.rb → undocumented_spec.rb} +1 -1
  54. data/spec/twitter/rest/{api/users_spec.rb → users_spec.rb} +5 -5
  55. data/spec/twitter/tweet_spec.rb +1 -1
  56. data/twitter.gemspec +0 -1
  57. metadata +247 -237
  58. metadata.gz.sig +0 -0
  59. checksums.yaml +0 -7
  60. checksums.yaml.gz.sig +0 -0
  61. data/lib/twitter/error/already_favorited.rb +0 -10
  62. data/lib/twitter/error/already_posted.rb +0 -10
  63. data/lib/twitter/error/already_retweeted.rb +0 -10
  64. data/lib/twitter/error/bad_gateway.rb +0 -10
  65. data/lib/twitter/error/bad_request.rb +0 -10
  66. data/lib/twitter/error/configuration_error.rb +0 -8
  67. data/lib/twitter/error/forbidden.rb +0 -10
  68. data/lib/twitter/error/gateway_timeout.rb +0 -10
  69. data/lib/twitter/error/internal_server_error.rb +0 -10
  70. data/lib/twitter/error/not_acceptable.rb +0 -10
  71. data/lib/twitter/error/not_found.rb +0 -10
  72. data/lib/twitter/error/request_timeout.rb +0 -10
  73. data/lib/twitter/error/service_unavailable.rb +0 -10
  74. data/lib/twitter/error/too_many_requests.rb +0 -12
  75. data/lib/twitter/error/unauthorized.rb +0 -10
  76. data/lib/twitter/error/unprocessable_entity.rb +0 -10
  77. data/lib/twitter/rest/api/direct_messages.rb +0 -140
  78. data/lib/twitter/rest/api/favorites.rb +0 -115
  79. data/lib/twitter/rest/api/friends_and_followers.rb +0 -284
  80. data/lib/twitter/rest/api/help.rb +0 -58
  81. data/lib/twitter/rest/api/lists.rb +0 -500
  82. data/lib/twitter/rest/api/oauth.rb +0 -67
  83. data/lib/twitter/rest/api/places_and_geo.rb +0 -86
  84. data/lib/twitter/rest/api/saved_searches.rb +0 -95
  85. data/lib/twitter/rest/api/search.rb +0 -39
  86. data/lib/twitter/rest/api/spam_reporting.rb +0 -29
  87. data/lib/twitter/rest/api/suggested_users.rb +0 -51
  88. data/lib/twitter/rest/api/timelines.rb +0 -202
  89. data/lib/twitter/rest/api/trends.rb +0 -58
  90. data/lib/twitter/rest/api/tweets.rb +0 -297
  91. data/lib/twitter/rest/api/undocumented.rb +0 -51
  92. data/lib/twitter/rest/api/users.rb +0 -383
  93. data/lib/twitter/rest/api/utils.rb +0 -212
@@ -0,0 +1,65 @@
1
+ require 'twitter/request'
2
+ require 'twitter/rest/utils'
3
+ require 'twitter/rest/response/parse_error_json'
4
+ require 'twitter/token'
5
+
6
+ module Twitter
7
+ module REST
8
+ module OAuth
9
+ include Twitter::REST::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
+ # @param options [Hash] A customizable set of options.
23
+ # @example Generate a Bearer Token
24
+ # client = Twitter::REST::Client.new(:consumer_key => "abc", :consumer_secret => 'def')
25
+ # bearer_token = client.token
26
+ def token(options = {})
27
+ options[:bearer_token_request] = true
28
+ options[:grant_type] ||= 'client_credentials'
29
+ perform_with_object(:post, '/oauth2/token', options, Twitter::Token)
30
+ end
31
+ alias_method :bearer_token, :token
32
+
33
+ # Allows a registered application to revoke an issued OAuth 2 Bearer Token by presenting its client credentials.
34
+ #
35
+ # @see https://dev.twitter.com/docs/api/1.1/post/oauth2/invalidate_token
36
+ # @rate_limited No
37
+ # @authentication Required
38
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
39
+ # @param access_token [String, Twitter::Token] The bearer token to revoke.
40
+ # @param options [Hash] A customizable set of options.
41
+ # @return [Twitter::Token] The invalidated token. token_type should be nil.
42
+ def invalidate_token(access_token, options = {})
43
+ access_token = access_token.access_token if access_token.is_a?(Twitter::Token)
44
+ options[:access_token] = access_token
45
+ perform_with_object(:post, '/oauth2/invalidate_token', options, Twitter::Token)
46
+ end
47
+
48
+ # Allows a registered application to revoke an issued OAuth 2 Bearer Token by presenting its client credentials.
49
+ #
50
+ # @see https://dev.twitter.com/docs/api/1.1/post/oauth2/invalidate_token
51
+ # @rate_limited No
52
+ # @authentication Required
53
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
54
+ # @return [String] The token string.
55
+ def reverse_token
56
+ conn = connection.dup
57
+ conn.builder.swap(4, Twitter::REST::Response::ParseErrorJson)
58
+ response = conn.post('/oauth/request_token?x_auth_mode=reverse_auth') do |request|
59
+ request.headers[:authorization] = oauth_auth_header(:post, 'https://api.twitter.com/oauth/request_token', :x_auth_mode => 'reverse_auth').to_s
60
+ end
61
+ response.body
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,84 @@
1
+ require 'twitter/geo_results'
2
+ require 'twitter/place'
3
+ require 'twitter/request'
4
+ require 'twitter/rest/utils'
5
+
6
+ module Twitter
7
+ module REST
8
+ module PlacesAndGeo
9
+ include Twitter::REST::Utils
10
+
11
+ # Returns all the information about a known place
12
+ #
13
+ # @see https://dev.twitter.com/docs/api/1.1/get/geo/id/:place_id
14
+ # @rate_limited Yes
15
+ # @authentication Requires user context
16
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
17
+ # @param place_id [String] A place in the world. These IDs can be retrieved from {Twitter::REST::PlacesAndGeo#reverse_geocode}.
18
+ # @param options [Hash] A customizable set of options.
19
+ # @return [Twitter::Place] The requested place.
20
+ def place(place_id, options = {})
21
+ perform_with_object(:get, "/1.1/geo/id/#{place_id}.json", options, Twitter::Place)
22
+ end
23
+
24
+ # Searches for up to 20 places that can be used as a place_id
25
+ #
26
+ # @see https://dev.twitter.com/docs/api/1.1/get/geo/reverse_geocode
27
+ # @note This request is an informative call and will deliver generalized results about geography.
28
+ # @rate_limited Yes
29
+ # @authentication Requires user context
30
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
31
+ # @param options [Hash] A customizable set of options.
32
+ # @option options [Float] :lat The latitude to search around. This option will be ignored unless it is inside the range -90.0 to +90.0 (North is positive) inclusive. It will also be ignored if there isn't a corresponding :long option.
33
+ # @option options [Float] :long The longitude to search around. The valid range for longitude is -180.0 to +180.0 (East is positive) inclusive. This option will be ignored if outside that range, if it is not a number, if geo_enabled is disabled, or if there not a corresponding :lat option.
34
+ # @option options [String] :accuracy ('0m') A hint on the "region" in which to search. If a number, then this is a radius in meters, but it can also take a string that is suffixed with ft to specify feet. If coming from a device, in practice, this value is whatever accuracy the device has measuring its location (whether it be coming from a GPS, WiFi triangulation, etc.).
35
+ # @option options [String] :granularity ('neighborhood') This is the minimal granularity of place types to return and must be one of: 'poi', 'neighborhood', 'city', 'admin' or 'country'.
36
+ # @option options [Integer] :max_results A hint as to the number of results to return. This does not guarantee that the number of results returned will equal max_results, but instead informs how many "nearby" results to return. Ideally, only pass in the number of places you intend to display to the user here.
37
+ # @return [Array<Twitter::Place>]
38
+ def reverse_geocode(options = {})
39
+ perform_with_object(:get, '/1.1/geo/reverse_geocode.json', options, Twitter::GeoResults)
40
+ end
41
+
42
+ # Search for places that can be attached to a {Twitter::REST::Tweets#update}
43
+ #
44
+ # @see https://dev.twitter.com/docs/api/1.1/get/geo/search
45
+ # @rate_limited Yes
46
+ # @authentication Requires user context
47
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
48
+ # @param options [Hash] A customizable set of options.
49
+ # @option options [Float] :lat The latitude to search around. This option will be ignored unless it is inside the range -90.0 to +90.0 (North is positive) inclusive. It will also be ignored if there isn't a corresponding :long option.
50
+ # @option options [Float] :long The longitude to search around. The valid range for longitude is -180.0 to +180.0 (East is positive) inclusive. This option will be ignored if outside that range, if it is not a number, if geo_enabled is disabled, or if there not a corresponding :lat option.
51
+ # @option options [String] :query Free-form text to match against while executing a geo-based query, best suited for finding nearby locations by name.
52
+ # @option options [String] :ip An IP address. Used when attempting to fix geolocation based off of the user's IP address.
53
+ # @option options [String] :granularity ('neighborhood') This is the minimal granularity of place types to return and must be one of: 'poi', 'neighborhood', 'city', 'admin' or 'country'.
54
+ # @option options [String] :accuracy ('0m') A hint on the "region" in which to search. If a number, then this is a radius in meters, but it can also take a string that is suffixed with ft to specify feet. If coming from a device, in practice, this value is whatever accuracy the device has measuring its location (whether it be coming from a GPS, WiFi triangulation, etc.).
55
+ # @option options [Integer] :max_results A hint as to the number of results to return. This does not guarantee that the number of results returned will equal max_results, but instead informs how many "nearby" results to return. Ideally, only pass in the number of places you intend to display to the user here.
56
+ # @option options [String] :contained_within This is the place_id which you would like to restrict the search results to. Setting this value means only places within the given place_id will be found.
57
+ # @option options [String] :"attribute:street_address" This option searches for places which have this given street address. There are other well-known and application-specific attributes available. Custom attributes are also permitted.
58
+ # @return [Array<Twitter::Place>]
59
+ def geo_search(options = {})
60
+ perform_with_object(:get, '/1.1/geo/search.json', options, Twitter::GeoResults)
61
+ end
62
+ alias_method :places_nearby, :geo_search
63
+
64
+ # Locates places near the given coordinates which are similar in name
65
+ #
66
+ # @see https://dev.twitter.com/docs/api/1.1/get/geo/similar_places
67
+ # @note Conceptually, you would use this method to get a list of known places to choose from first. Then, if the desired place doesn't exist, make a request to {Twitter::REST::PlacesAndGeo#place} to create a new one. The token contained in the response is the token necessary to create a new place.
68
+ # @rate_limited Yes
69
+ # @authentication Requires user context
70
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
71
+ # @param options [Hash] A customizable set of options.
72
+ # @option options [Float] :lat The latitude to search around. This option will be ignored unless it is inside the range -90.0 to +90.0 (North is positive) inclusive. It will also be ignored if there isn't a corresponding :long option.
73
+ # @option options [Float] :long The longitude to search around. The valid range for longitude is -180.0 to +180.0 (East is positive) inclusive. This option will be ignored if outside that range, if it is not a number, if geo_enabled is disabled, or if there not a corresponding :lat option.
74
+ # @option options [String] :name The name a place is known as.
75
+ # @option options [String] :contained_within This is the place_id which you would like to restrict the search results to. Setting this value means only places within the given place_id will be found.
76
+ # @option options [String] :"attribute:street_address" This option searches for places which have this given street address. There are other well-known and application-specific attributes available. Custom attributes are also permitted.
77
+ # @return [Array<Twitter::Place>]
78
+ def similar_places(options = {})
79
+ perform_with_object(:get, '/1.1/geo/similar_places.json', options, Twitter::GeoResults)
80
+ end
81
+ alias_method :places_similar, :similar_places
82
+ end
83
+ end
84
+ end
@@ -9,13 +9,13 @@ module Twitter
9
9
  JPEG_REGEX = /\.jpe?g/i
10
10
  PNG_REGEX = /\.png$/i
11
11
 
12
- def call(env)
13
- env[:body].each do |key, value|
12
+ def call(request)
13
+ request.body.each do |key, value|
14
14
  if value.respond_to?(:to_io)
15
- env[:body][key] = Faraday::UploadIO.new(value, mime_type(value.path), value.path)
15
+ request.body[key] = Faraday::UploadIO.new(value, mime_type(value.path), value.path)
16
16
  end
17
- end if env[:body].is_a?(::Hash)
18
- @app.call(env)
17
+ end if request.body.is_a?(::Hash)
18
+ @app.call(request)
19
19
  end
20
20
 
21
21
  private
@@ -16,10 +16,8 @@ module Twitter
16
16
  end
17
17
  end
18
18
 
19
- def on_complete(env)
20
- if respond_to?(:parse)
21
- env[:body] = parse(env[:body]) unless unparsable_status_codes.include?(env[:status])
22
- end
19
+ def on_complete(response)
20
+ response.body = parse(response.body) if respond_to?(:parse) && !unparsable_status_codes.include?(response.status)
23
21
  end
24
22
 
25
23
  def unparsable_status_codes
@@ -1,24 +1,33 @@
1
1
  require 'faraday'
2
- require 'twitter/error/bad_gateway'
3
- require 'twitter/error/bad_request'
4
- require 'twitter/error/forbidden'
5
- require 'twitter/error/gateway_timeout'
6
- require 'twitter/error/internal_server_error'
7
- require 'twitter/error/not_acceptable'
8
- require 'twitter/error/not_found'
9
- require 'twitter/error/service_unavailable'
10
- require 'twitter/error/too_many_requests'
11
- require 'twitter/error/unauthorized'
12
- require 'twitter/error/unprocessable_entity'
2
+ require 'twitter/error'
13
3
 
14
4
  module Twitter
15
5
  module REST
16
6
  module Response
17
7
  class RaiseError < Faraday::Response::Middleware
18
- def on_complete(env)
19
- status_code = env[:status].to_i
20
- error_class = Twitter::Error.errors[status_code]
21
- fail error_class.from_response(env) if error_class
8
+ def on_complete(response)
9
+ status_code = response.status.to_i
10
+ klass = Twitter::Error.errors[status_code]
11
+ if klass
12
+ error = if klass == Twitter::Error::Forbidden
13
+ handle_forbidden_errors(response)
14
+ else
15
+ klass.from_response(response)
16
+ end
17
+ fail(error)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def handle_forbidden_errors(response)
24
+ error = Twitter::Error::Forbidden.from_response(response)
25
+ klass = Twitter::Error.forbidden_messages[error.message]
26
+ if klass
27
+ klass.from_response(response)
28
+ else
29
+ error
30
+ end
22
31
  end
23
32
  end
24
33
  end
@@ -0,0 +1,93 @@
1
+ require 'twitter/arguments'
2
+ require 'twitter/request'
3
+ require 'twitter/rest/utils'
4
+ require 'twitter/saved_search'
5
+ require 'twitter/utils'
6
+
7
+ module Twitter
8
+ module REST
9
+ module SavedSearches
10
+ include Twitter::REST::Utils
11
+ include Twitter::Utils
12
+
13
+ # @rate_limited Yes
14
+ # @authentication Requires user context
15
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
16
+ # @return [Array<Twitter::SavedSearch>] The saved searches.
17
+ # @overload saved_search(options = {})
18
+ # Returns the authenticated user's saved search queries
19
+ #
20
+ # @see https://dev.twitter.com/docs/api/1.1/get/saved_searches/list
21
+ # @param options [Hash] A customizable set of options.
22
+ # @overload saved_search(*ids)
23
+ # Retrieve the data for saved searches owned by the authenticating user
24
+ #
25
+ # @see https://dev.twitter.com/docs/api/1.1/get/saved_searches/show/:id
26
+ # @param ids [Enumerable<Integer>] A collection of saved search IDs.
27
+ # @overload saved_search(*ids, options)
28
+ # Retrieve the data for saved searches owned by the authenticating user
29
+ #
30
+ # @see https://dev.twitter.com/docs/api/1.1/get/saved_searches/show/:id
31
+ # @param ids [Enumerable<Integer>] A collection of saved search IDs.
32
+ # @param options [Hash] A customizable set of options.
33
+ def saved_searches(*args)
34
+ arguments = Twitter::Arguments.new(args)
35
+ if arguments.empty?
36
+ perform_with_objects(:get, '/1.1/saved_searches/list.json', arguments.options, Twitter::SavedSearch)
37
+ else
38
+ parallel_map(arguments) do |id|
39
+ saved_search(id, arguments.options)
40
+ end
41
+ end
42
+ end
43
+
44
+ # Retrieve the data for saved searches owned by the authenticating user
45
+ #
46
+ # @see https://dev.twitter.com/docs/api/1.1/get/saved_searches/show/:id
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 [Twitter::SavedSearch] The saved searches.
51
+ # @param id [Integer] The ID of the saved search.
52
+ # @param options [Hash] A customizable set of options.
53
+ def saved_search(id, options = {})
54
+ perform_with_object(:get, "/1.1/saved_searches/show/#{id}.json", options, Twitter::SavedSearch)
55
+ end
56
+
57
+ # Creates a saved search for the authenticated user
58
+ #
59
+ # @see https://dev.twitter.com/docs/api/1.1/post/saved_searches/create
60
+ # @rate_limited No
61
+ # @authentication Requires user context
62
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
63
+ # @return [Twitter::SavedSearch] The created saved search.
64
+ # @param query [String] The query of the search the user would like to save.
65
+ # @param options [Hash] A customizable set of options.
66
+ def create_saved_search(query, options = {})
67
+ perform_with_object(:post, '/1.1/saved_searches/create.json', options.merge(:query => query), Twitter::SavedSearch)
68
+ end
69
+ deprecate_alias :saved_search_create, :create_saved_search
70
+
71
+ # Destroys saved searches for the authenticated user
72
+ #
73
+ # @see https://dev.twitter.com/docs/api/1.1/post/saved_searches/destroy/:id
74
+ # @note The search specified by ID must be owned by the authenticating user.
75
+ # @rate_limited No
76
+ # @authentication Requires user context
77
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
78
+ # @return [Array<Twitter::SavedSearch>] The deleted saved searches.
79
+ # @overload destroy_saved_search(*ids)
80
+ # @param ids [Enumerable<Integer>] A collection of saved search IDs.
81
+ # @overload destroy_saved_search(*ids, options)
82
+ # @param ids [Enumerable<Integer>] A collection of saved search IDs.
83
+ # @param options [Hash] A customizable set of options.
84
+ def destroy_saved_search(*args)
85
+ arguments = Twitter::Arguments.new(args)
86
+ parallel_map(arguments) do |id|
87
+ perform_with_object(:post, "/1.1/saved_searches/destroy/#{id}.json", arguments.options, Twitter::SavedSearch)
88
+ end
89
+ end
90
+ deprecate_alias :saved_search_destroy, :destroy_saved_search
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,37 @@
1
+ require 'twitter/request'
2
+ require 'twitter/search_results'
3
+
4
+ module Twitter
5
+ module REST
6
+ module Search
7
+ MAX_TWEETS_PER_REQUEST = 100
8
+
9
+ # Returns tweets that match a specified query.
10
+ #
11
+ # @see https://dev.twitter.com/docs/api/1.1/get/search/tweets
12
+ # @see https://dev.twitter.com/docs/using-search
13
+ # @note Please note that Twitter's search service and, by extension, the Search API is not meant to be an exhaustive source of Tweets. Not all Tweets will be indexed or made available via the search interface.
14
+ # @rate_limited Yes
15
+ # @authentication Requires user context
16
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
17
+ # @param q [String] A search term.
18
+ # @param options [Hash] A customizable set of options.
19
+ # @option options [String] :geocode Returns tweets by users located within a given radius of the given latitude/longitude. The location is preferentially taking from the Geotagging API, but will fall back to their Twitter profile. The parameter value is specified by "latitude,longitude,radius", where radius units must be specified as either "mi" (miles) or "km" (kilometers). Note that you cannot use the near operator via the API to geocode arbitrary locations; however you can use this geocode parameter to search near geocodes directly.
20
+ # @option options [String] :lang Restricts tweets to the given language, given by an ISO 639-1 code.
21
+ # @option options [String] :locale Specify the language of the query you are sending (only ja is currently effective). This is intended for language-specific clients and the default should work in the majority of cases.
22
+ # @option options [String] :result_type Specifies what type of search results you would prefer to receive. Options are "mixed", "recent", and "popular". The current default is "mixed."
23
+ # @option options [Integer] :count The number of tweets to return per page, up to a maximum of 100.
24
+ # @option options [String] :until Optional. Returns tweets generated before the given date. Date should be formatted as YYYY-MM-DD.
25
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. There are limits to the number of Tweets which can be accessed through the API. If the limit of Tweets has occured since the since_id, the since_id will be forced to the oldest ID available.
26
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
27
+ # @option options [Boolean, String, Integer] :include_entities The tweet entities node will be disincluded when set to false.
28
+ # @return [Twitter::SearchResults] Return tweets that match a specified query with search metadata
29
+ def search(q, options = {})
30
+ options[:count] ||= MAX_TWEETS_PER_REQUEST
31
+ request = Twitter::Request.new(self, :get, '/1.1/search/tweets.json', options.merge(:q => q))
32
+ response = get(request.path, request.options).body
33
+ Twitter::SearchResults.new(response, request)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ require 'twitter/request'
2
+ require 'twitter/rest/utils'
3
+ require 'twitter/user'
4
+
5
+ module Twitter
6
+ module REST
7
+ module SpamReporting
8
+ include Twitter::REST::Utils
9
+
10
+ # The users specified are blocked by the authenticated user and reported as spammers
11
+ #
12
+ # @see https://dev.twitter.com/docs/api/1.1/post/users/report_spam
13
+ # @rate_limited Yes
14
+ # @authentication Requires user context
15
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
16
+ # @return [Array<Twitter::User>] The reported users.
17
+ # @overload report_spam(*users)
18
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
19
+ # @overload report_spam(*users, options)
20
+ # @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen names, or objects.
21
+ # @param options [Hash] A customizable set of options.
22
+ def report_spam(*args)
23
+ parallel_user_objects_from_response(:post, '/1.1/users/report_spam.json', args)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,49 @@
1
+ require 'twitter/arguments'
2
+ require 'twitter/request'
3
+ require 'twitter/rest/utils'
4
+ require 'twitter/suggestion'
5
+ require 'twitter/user'
6
+
7
+ module Twitter
8
+ module REST
9
+ module SuggestedUsers
10
+ include Twitter::REST::Utils
11
+
12
+ # @return [Array<Twitter::Suggestion>]
13
+ # @rate_limited Yes
14
+ # @authentication Requires user context
15
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
16
+ # @overload suggestions(options = {})
17
+ # Returns the list of suggested user categories
18
+ #
19
+ # @see https://dev.twitter.com/docs/api/1.1/get/users/suggestions
20
+ # @param options [Hash] A customizable set of options.
21
+ # @overload suggestions(slug, options = {})
22
+ # Returns the users in a given category
23
+ #
24
+ # @see https://dev.twitter.com/docs/api/1.1/get/users/suggestions/:slug
25
+ # @param slug [String] The short name of list or a category.
26
+ # @param options [Hash] A customizable set of options.
27
+ def suggestions(*args)
28
+ arguments = Twitter::Arguments.new(args)
29
+ if arguments.last
30
+ perform_with_object(:get, "/1.1/users/suggestions/#{arguments.pop}.json", arguments.options, Twitter::Suggestion)
31
+ else
32
+ perform_with_objects(:get, '/1.1/users/suggestions.json', arguments.options, Twitter::Suggestion)
33
+ end
34
+ end
35
+
36
+ # Access the users in a given category of the Twitter suggested user list and return their most recent Tweet if they are not a protected user
37
+ #
38
+ # @see https://dev.twitter.com/docs/api/1.1/get/users/suggestions/:slug/members
39
+ # @rate_limited Yes
40
+ # @authentication Requires user context
41
+ # @param slug [String] The short name of list or a category.
42
+ # @param options [Hash] A customizable set of options.
43
+ # @return [Array<Twitter::User>]
44
+ def suggest_users(slug, options = {})
45
+ perform_with_objects(:get, "/1.1/users/suggestions/#{slug}/members.json", options, Twitter::User)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,200 @@
1
+ require 'twitter/request'
2
+ require 'twitter/rest/utils'
3
+ require 'twitter/tweet'
4
+ require 'twitter/user'
5
+
6
+ module Twitter
7
+ module REST
8
+ module Timelines
9
+ include Twitter::REST::Utils
10
+ DEFAULT_TWEETS_PER_REQUEST = 20
11
+ MAX_TWEETS_PER_REQUEST = 200
12
+
13
+ # Returns the 20 most recent mentions (statuses containing @username) for the authenticating user
14
+ #
15
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/mentions_timeline
16
+ # @note This method can only return up to 800 Tweets.
17
+ # @rate_limited Yes
18
+ # @authentication Requires user context
19
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
20
+ # @return [Array<Twitter::Tweet>]
21
+ # @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 [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.
26
+ def mentions_timeline(options = {})
27
+ perform_with_objects(:get, '/1.1/statuses/mentions_timeline.json', options, Twitter::Tweet)
28
+ end
29
+ alias_method :mentions, :mentions_timeline
30
+
31
+ # Returns the 20 most recent Tweets posted by the specified user
32
+ #
33
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
34
+ # @note This method can only return up to 3,200 Tweets.
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 [Array<Twitter::Tweet>]
39
+ # @overload user_timeline(user, options = {})
40
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
41
+ # @param options [Hash] A customizable set of options.
42
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
43
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
44
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
45
+ # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.
46
+ # @option options [Boolean, String, Integer] :exclude_replies This parameter will prevent replies from appearing in the returned timeline. Using exclude_replies with the count parameter will mean you will receive up-to count tweets - this is because the count parameter retrieves that many tweets before filtering out retweets and replies.
47
+ # @option options [Boolean, String, Integer] :contributor_details Specifies that the contributors element should be enhanced to include the screen_name of the contributor.
48
+ # @option options [Boolean, String, Integer] :include_rts Specifies that the timeline should include native retweets in addition to regular tweets. Note: If you're using the trim_user parameter in conjunction with include_rts, the retweets will no longer contain a full user object.
49
+ def user_timeline(*args)
50
+ objects_from_response_with_user(Twitter::Tweet, :get, '/1.1/statuses/user_timeline.json', args)
51
+ end
52
+
53
+ # Returns the 20 most recent retweets posted by the specified user
54
+ #
55
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
56
+ # @note This method can only return up to 3,200 Tweets.
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::Tweet>]
61
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
62
+ # @param options [Hash] A customizable set of options.
63
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
64
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
65
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
66
+ # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.
67
+ # @option options [Boolean, String, Integer] :exclude_replies This parameter will prevent replies from appearing in the returned timeline. Using exclude_replies with the count parameter will mean you will receive up-to count tweets - this is because the count parameter retrieves that many tweets before filtering out retweets and replies.
68
+ # @option options [Boolean, String, Integer] :contributor_details Specifies that the contributors element should be enhanced to include the screen_name of the contributor.
69
+ def retweeted_by_user(user, options = {})
70
+ retweets_from_timeline(options) do |opts|
71
+ user_timeline(user, opts)
72
+ end
73
+ end
74
+ alias_method :retweeted_by, :retweeted_by_user
75
+
76
+ # Returns the 20 most recent retweets posted by the authenticating user
77
+ #
78
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
79
+ # @note This method can only return up to 3,200 Tweets.
80
+ # @rate_limited Yes
81
+ # @authentication Requires user context
82
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
83
+ # @return [Array<Twitter::Tweet>]
84
+ # @param options [Hash] A customizable set of options.
85
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
86
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
87
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
88
+ # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.
89
+ # @option options [Boolean, String, Integer] :exclude_replies This parameter will prevent replies from appearing in the returned timeline. Using exclude_replies with the count parameter will mean you will receive up-to count tweets - this is because the count parameter retrieves that many tweets before filtering out retweets and replies.
90
+ # @option options [Boolean, String, Integer] :contributor_details Specifies that the contributors element should be enhanced to include the screen_name of the contributor.
91
+ def retweeted_by_me(options = {})
92
+ retweets_from_timeline(options) do |opts|
93
+ user_timeline(opts)
94
+ end
95
+ end
96
+
97
+ # Returns the 20 most recent Tweets, including retweets if they exist, posted by the authenticating user and the users they follow
98
+ #
99
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline
100
+ # @note This method can only return up to 800 Tweets, including retweets.
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::Tweet>]
105
+ # @param options [Hash] A customizable set of options.
106
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
107
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
108
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
109
+ # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.
110
+ # @option options [Boolean, String, Integer] :exclude_replies This parameter will prevent replies from appearing in the returned timeline. Using exclude_replies with the count parameter will mean you will receive up-to count tweets - this is because the count parameter retrieves that many tweets before filtering out retweets and replies.
111
+ # @option options [Boolean, String, Integer] :include_rts Specifies that the timeline should include native retweets in addition to regular tweets. Note: If you're using the trim_user parameter in conjunction with include_rts, the retweets will no longer contain a full user object.
112
+ # @option options [Boolean, String, Integer] :contributor_details Specifies that the contributors element should be enhanced to include the screen_name of the contributor.
113
+ # @option options [Boolean, String, Integer] :include_entities The tweet entities node will be disincluded when set to false.
114
+ def home_timeline(options = {})
115
+ perform_with_objects(:get, '/1.1/statuses/home_timeline.json', options, Twitter::Tweet)
116
+ end
117
+
118
+ # Returns the 20 most recent retweets posted by users the authenticating user follow.
119
+ #
120
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline
121
+ # @note This method can only return up to 800 Tweets, including retweets.
122
+ # @rate_limited Yes
123
+ # @authentication Requires user context
124
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
125
+ # @return [Array<Twitter::Tweet>]
126
+ # @param options [Hash] A customizable set of options.
127
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
128
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
129
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
130
+ # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.
131
+ # @option options [Boolean, String, Integer] :exclude_replies This parameter will prevent replies from appearing in the returned timeline. Using exclude_replies with the count parameter will mean you will receive up-to count tweets - this is because the count parameter retrieves that many tweets before filtering out retweets and replies.
132
+ # @option options [Boolean, String, Integer] :contributor_details Specifies that the contributors element should be enhanced to include the screen_name of the contributor.
133
+ # @option options [Boolean, String, Integer] :include_entities The tweet entities node will be disincluded when set to false.
134
+ def retweeted_to_me(options = {})
135
+ retweets_from_timeline(options) do |opts|
136
+ home_timeline(opts)
137
+ end
138
+ end
139
+
140
+ # Returns the 20 most recent tweets of the authenticated user that have been retweeted by others
141
+ #
142
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/retweets_of_me
143
+ # @rate_limited Yes
144
+ # @authentication Requires user context
145
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
146
+ # @return [Array<Twitter::Tweet>]
147
+ # @param options [Hash] A customizable set of options.
148
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
149
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
150
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
151
+ # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.
152
+ # @option options [Boolean, String, Integer] :include_entities The tweet entities node will be disincluded when set to false.
153
+ # @option options [Boolean, String, Integer] :include_user_entities The user entities node will be disincluded when set to false.
154
+ def retweets_of_me(options = {})
155
+ perform_with_objects(:get, '/1.1/statuses/retweets_of_me.json', options, Twitter::Tweet)
156
+ end
157
+
158
+ private
159
+
160
+ def retweets_from_timeline(options)
161
+ options[:include_rts] = true
162
+ count = options[:count] || DEFAULT_TWEETS_PER_REQUEST
163
+ collect_with_count(count) do |count_options|
164
+ select_retweets(yield(options.merge(count_options)))
165
+ end
166
+ end
167
+
168
+ # @param tweets [Array]
169
+ # @return [Array<Twitter::Tweet>]
170
+ def select_retweets(tweets)
171
+ tweets.select(&:retweet?)
172
+ end
173
+
174
+ # @param count [Integer]
175
+ # @return [Array<Twitter::Tweet>]
176
+ def collect_with_count(count)
177
+ options = {}
178
+ options[:count] = MAX_TWEETS_PER_REQUEST
179
+ collect_with_max_id do |max_id|
180
+ options[:max_id] = max_id unless max_id.nil?
181
+ if count > 0
182
+ tweets = yield(options)
183
+ count -= tweets.length
184
+ tweets
185
+ end
186
+ end.flatten.compact[0...count]
187
+ end
188
+
189
+ # @param collection [Array]
190
+ # @param max_id [Integer, NilClass]
191
+ # @return [Array<Twitter::Tweet>]
192
+ def collect_with_max_id(collection = [], max_id = nil, &block)
193
+ tweets = yield(max_id)
194
+ return collection if tweets.nil?
195
+ collection += tweets
196
+ tweets.empty? ? collection.flatten : collect_with_max_id(collection, tweets.last.id - 1, &block)
197
+ end
198
+ end
199
+ end
200
+ end