twitter 4.2.0 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/CHANGELOG.md +4 -0
  2. data/lib/twitter/api/direct_messages.rb +150 -0
  3. data/lib/twitter/api/favorites.rb +132 -0
  4. data/lib/twitter/api/friends_and_followers.rb +259 -0
  5. data/lib/twitter/api/help.rb +64 -0
  6. data/lib/twitter/api/lists.rb +570 -0
  7. data/lib/twitter/api/places_and_geo.rb +121 -0
  8. data/lib/twitter/api/saved_searches.rb +98 -0
  9. data/lib/twitter/api/search.rb +37 -0
  10. data/lib/twitter/api/spam_reporting.rb +30 -0
  11. data/lib/twitter/api/suggested_users.rb +54 -0
  12. data/lib/twitter/api/timelines.rb +213 -0
  13. data/lib/twitter/api/trends.rb +63 -0
  14. data/lib/twitter/api/tweets.rb +284 -0
  15. data/lib/twitter/api/undocumented.rb +116 -0
  16. data/lib/twitter/api/users.rb +427 -0
  17. data/lib/twitter/api/utils.rb +111 -0
  18. data/lib/twitter/client.rb +41 -13
  19. data/lib/twitter/core_ext/enumerable.rb +1 -1
  20. data/lib/twitter/default.rb +16 -18
  21. data/lib/twitter/error/already_favorited.rb +1 -1
  22. data/lib/twitter/error/already_retweeted.rb +1 -1
  23. data/lib/twitter/profile_banner.rb +18 -0
  24. data/lib/twitter/version.rb +1 -1
  25. data/spec/fixtures/profile_banner.json +1 -0
  26. data/spec/twitter/api/direct_messages_spec.rb +32 -32
  27. data/spec/twitter/api/favorites_spec.rb +114 -0
  28. data/spec/twitter/api/{friendships_spec.rb → friends_and_followers_spec.rb} +146 -146
  29. data/spec/twitter/api/geo_spec.rb +28 -28
  30. data/spec/twitter/api/help_spec.rb +1 -1
  31. data/spec/twitter/api/lists_spec.rb +82 -82
  32. data/spec/twitter/api/saved_searches_spec.rb +1 -1
  33. data/spec/twitter/api/search_spec.rb +1 -17
  34. data/spec/twitter/api/{report_spam_spec.rb → spam_reporting_spec.rb} +1 -1
  35. data/spec/twitter/api/suggested_users_spec.rb +94 -0
  36. data/spec/twitter/api/timelines_spec.rb +138 -0
  37. data/spec/twitter/api/trends_spec.rb +1 -1
  38. data/spec/twitter/api/tweets_spec.rb +249 -0
  39. data/spec/twitter/api/undocumented_spec.rb +103 -0
  40. data/spec/twitter/api/users_spec.rb +308 -17
  41. data/spec/twitter/client_spec.rb +1 -1
  42. data/spec/twitter/profile_banner_spec.rb +13 -0
  43. data/twitter.gemspec +3 -2
  44. metadata +44 -21
  45. data/lib/twitter/api.rb +0 -2558
  46. data/spec/twitter/api/account_spec.rb +0 -152
  47. data/spec/twitter/api/activity_spec.rb +0 -37
  48. data/spec/twitter/api/blocks_spec.rb +0 -122
  49. data/spec/twitter/api/statuses_spec.rb +0 -541
@@ -0,0 +1,121 @@
1
+ require 'twitter/api/utils'
2
+ require 'twitter/place'
3
+
4
+ module Twitter
5
+ module API
6
+ module PlacesAndGeo
7
+ include Twitter::API::Utils
8
+
9
+ # Returns all the information about a known place
10
+ #
11
+ # @see https://dev.twitter.com/docs/api/1.1/get/geo/id/:place_id
12
+ # @rate_limited Yes
13
+ # @authentication_required Requires user context
14
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
15
+ # @param place_id [String] A place in the world. These IDs can be retrieved from {Twitter::API::Geo#reverse_geocode}.
16
+ # @param options [Hash] A customizable set of options.
17
+ # @return [Twitter::Place] The requested place.
18
+ # @example Return all the information about Twitter HQ
19
+ # Twitter.place("247f43d441defc03")
20
+ def place(place_id, options={})
21
+ object_from_response(Twitter::Place, :get, "/1.1/geo/id/#{place_id}.json", options)
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_required 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
+ # @example Return an array of places within the specified region
39
+ # Twitter.reverse_geocode(:lat => "37.7821120598956", :long => "-122.400612831116")
40
+ def reverse_geocode(options={})
41
+ geo_collection_from_response(:get, "/1.1/geo/reverse_geocode.json", options)
42
+ end
43
+
44
+ # Search for places that can be attached to a {Twitter::API::Statuses#update}
45
+ #
46
+ # @see https://dev.twitter.com/docs/api/1.1/get/geo/search
47
+ # @rate_limited Yes
48
+ # @authentication_required Requires user context
49
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
50
+ # @param options [Hash] A customizable set of options.
51
+ # @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.
52
+ # @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.
53
+ # @option options [String] :query Free-form text to match against while executing a geo-based query, best suited for finding nearby locations by name.
54
+ # @option options [String] :ip An IP address. Used when attempting to fix geolocation based off of the user's IP address.
55
+ # @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'.
56
+ # @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.).
57
+ # @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.
58
+ # @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.
59
+ # @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.
60
+ # @return [Array<Twitter::Place>]
61
+ # @example Return an array of places near the IP address 74.125.19.104
62
+ # Twitter.geo_search(:ip => "74.125.19.104")
63
+ def geo_search(options={})
64
+ geo_collection_from_response(:get, "/1.1/geo/search.json", options)
65
+ end
66
+ alias places_nearby geo_search
67
+
68
+ # Locates places near the given coordinates which are similar in name
69
+ #
70
+ # @see https://dev.twitter.com/docs/api/1.1/get/geo/similar_places
71
+ # @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::API::Geo#place} to create a new one. The token contained in the response is the token necessary to create a new place.
72
+ # @rate_limited Yes
73
+ # @authentication_required Requires user context
74
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
75
+ # @param options [Hash] A customizable set of options.
76
+ # @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.
77
+ # @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.
78
+ # @option options [String] :name The name a place is known as.
79
+ # @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.
80
+ # @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.
81
+ # @return [Array<Twitter::Place>]
82
+ # @example Return an array of places similar to Twitter HQ
83
+ # Twitter.similar_places(:lat => "37.7821120598956", :long => "-122.400612831116", :name => "Twitter HQ")
84
+ def similar_places(options={})
85
+ geo_collection_from_response(:get, "/1.1/geo/similar_places.json", options)
86
+ end
87
+ alias places_similar similar_places
88
+
89
+ # Creates a new place at the given latitude and longitude
90
+ #
91
+ # @see https://dev.twitter.com/docs/api/1.1/post/geo/place
92
+ # @rate_limited Yes
93
+ # @authentication_required Requires user context
94
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
95
+ # @param options [Hash] A customizable set of options.
96
+ # @option options [String] :name The name a place is known as.
97
+ # @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.
98
+ # @option options [String] :token The token found in the response from {Twitter::API::Geo#places_similar}.
99
+ # @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.
100
+ # @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.
101
+ # @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.
102
+ # @return [Twitter::Place] The created place.
103
+ # @example Create a new place
104
+ # Twitter.place_create(:name => "@sferik's Apartment", :token => "22ff5b1f7159032cf69218c4d8bb78bc", :contained_within => "41bcb736f84a799e", :lat => "37.783699", :long => "-122.393581")
105
+ def place_create(options={})
106
+ object_from_response(Twitter::Place, :post, "/1.1/geo/place.json", options)
107
+ end
108
+
109
+ private
110
+
111
+ # @param request_method [Symbol]
112
+ # @param url [String]
113
+ # @param options [Hash]
114
+ # @return [Array]
115
+ def geo_collection_from_response(request_method, url, params={})
116
+ collection_from_array(Twitter::Place, send(request_method.to_sym, url, params)[:body][:result][:places])
117
+ end
118
+
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,98 @@
1
+ require 'twitter/api/utils'
2
+ require 'twitter/saved_search'
3
+
4
+ module Twitter
5
+ module API
6
+ module SavedSearches
7
+ include Twitter::API::Utils
8
+
9
+ # @rate_limited Yes
10
+ # @authentication_required Requires user context
11
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
12
+ # @return [Array<Twitter::SavedSearch>] The saved searches.
13
+ # @overload saved_search(options={})
14
+ # Returns the authenticated user's saved search queries
15
+ #
16
+ # @see https://dev.twitter.com/docs/api/1.1/get/saved_searches/list
17
+ # @param options [Hash] A customizable set of options.
18
+ # @example Return the authenticated user's saved search queries
19
+ # Twitter.saved_searches
20
+ # @overload saved_search(*ids)
21
+ # Retrieve the data for saved searches owned by the authenticating user
22
+ #
23
+ # @see https://dev.twitter.com/docs/api/1.1/get/saved_searches/show/:id
24
+ # @param ids [Array<Integer>, Set<Integer>] An array of Tweet IDs.
25
+ # @example Retrieve the data for a saved search owned by the authenticating user with the ID 16129012
26
+ # Twitter.saved_search(16129012)
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 [Array<Integer>, Set<Integer>] An array of Tweet IDs.
32
+ # @param options [Hash] A customizable set of options.
33
+ def saved_searches(*args)
34
+ options = args.extract_options!
35
+ if args.empty?
36
+ collection_from_response(Twitter::SavedSearch, :get, "/1.1/saved_searches/list.json", options)
37
+ else
38
+ args.flatten.threaded_map do |id|
39
+ saved_search(id)
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_required 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] A Tweet IDs.
52
+ # @param options [Hash] A customizable set of options.
53
+ # @example Retrieve the data for a saved search owned by the authenticating user with the ID 16129012
54
+ # Twitter.saved_search(16129012)
55
+ def saved_search(id, options={})
56
+ object_from_response(Twitter::SavedSearch, :get, "/1.1/saved_searches/show/#{id}.json", options)
57
+ end
58
+
59
+ # Creates a saved search for the authenticated user
60
+ #
61
+ # @see https://dev.twitter.com/docs/api/1.1/post/saved_searches/create
62
+ # @rate_limited No
63
+ # @authentication_required Requires user context
64
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
65
+ # @return [Twitter::SavedSearch] The created saved search.
66
+ # @param query [String] The query of the search the user would like to save.
67
+ # @param options [Hash] A customizable set of options.
68
+ # @example Create a saved search for the authenticated user with the query "twitter"
69
+ # Twitter.saved_search_create("twitter")
70
+ def saved_search_create(query, options={})
71
+ object_from_response(Twitter::SavedSearch, :post, "/1.1/saved_searches/create.json", options.merge(:query => query))
72
+ end
73
+
74
+ # Destroys saved searches for the authenticated user
75
+ #
76
+ # @see https://dev.twitter.com/docs/api/1.1/post/saved_searches/destroy/:id
77
+ # @note The search specified by ID must be owned by the authenticating user.
78
+ # @rate_limited No
79
+ # @authentication_required Requires user context
80
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
81
+ # @return [Array<Twitter::SavedSearch>] The deleted saved searches.
82
+ # @overload saved_search_destroy(*ids)
83
+ # @param ids [Array<Integer>, Set<Integer>] An array of Tweet IDs.
84
+ # @example Destroys a saved search for the authenticated user with the ID 16129012
85
+ # Twitter.saved_search_destroy(16129012)
86
+ # @overload saved_search_destroy(*ids, options)
87
+ # @param ids [Array<Integer>, Set<Integer>] An array of Tweet IDs.
88
+ # @param options [Hash] A customizable set of options.
89
+ def saved_search_destroy(*args)
90
+ options = args.extract_options!
91
+ args.flatten.threaded_map do |id|
92
+ object_from_response(Twitter::SavedSearch, :post, "/1.1/saved_searches/destroy/#{id}.json", options)
93
+ end
94
+ end
95
+
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,37 @@
1
+ require 'twitter/api/utils'
2
+ require 'twitter/search_results'
3
+
4
+ module Twitter
5
+ module API
6
+ module Search
7
+ include Twitter::API::Utils
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_required 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
+ # @example Returns tweets related to twitter
30
+ # Twitter.search('twitter')
31
+ def search(q, options={})
32
+ object_from_response(Twitter::SearchResults, :get, "/1.1/search/tweets.json", options.merge(:q => q))
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,30 @@
1
+ require 'twitter/api/utils'
2
+ require 'twitter/user'
3
+
4
+ module Twitter
5
+ module API
6
+ module SpamReporting
7
+ include Twitter::API::Utils
8
+
9
+ # The users specified are blocked by the authenticated user and reported as spammers
10
+ #
11
+ # @see https://dev.twitter.com/docs/api/1.1/post/report_spam
12
+ # @rate_limited Yes
13
+ # @authentication_required Requires user context
14
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
15
+ # @return [Array<Twitter::User>] The reported users.
16
+ # @overload report_spam(*users)
17
+ # @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen names, or objects.
18
+ # @example Report @spam for spam
19
+ # Twitter.report_spam("spam")
20
+ # Twitter.report_spam(14589771) # Same as above
21
+ # @overload report_spam(*users, options)
22
+ # @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen names, or objects.
23
+ # @param options [Hash] A customizable set of options.
24
+ def report_spam(*args)
25
+ threaded_users_from_response(:post, "/1.1/report_spam.json", args)
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,54 @@
1
+ require 'twitter/api/utils'
2
+ require 'twitter/suggestion'
3
+ require 'twitter/user'
4
+
5
+ module Twitter
6
+ module API
7
+ module SuggestedUsers
8
+ include Twitter::API::Utils
9
+
10
+ # @return [Array<Twitter::Suggestion>]
11
+ # @rate_limited Yes
12
+ # @authentication_required Requires user context
13
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
14
+ # @overload suggestions(options={})
15
+ # Returns the list of suggested user categories
16
+ #
17
+ # @see https://dev.twitter.com/docs/api/1.1/get/users/suggestions
18
+ # @param options [Hash] A customizable set of options.
19
+ # @example Return the list of suggested user categories
20
+ # Twitter.suggestions
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
+ # @example Return the users in the Art & Design category
28
+ # Twitter.suggestions("art-design")
29
+ def suggestions(*args)
30
+ options = args.extract_options!
31
+ if slug = args.pop
32
+ object_from_response(Twitter::Suggestion, :get, "/1.1/users/suggestions/#{slug}.json", options)
33
+ else
34
+ collection_from_response(Twitter::Suggestion, :get, "/1.1/users/suggestions.json", options)
35
+ end
36
+ end
37
+
38
+ # 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
39
+ #
40
+ # @see https://dev.twitter.com/docs/api/1.1/get/users/suggestions/:slug/members
41
+ # @rate_limited Yes
42
+ # @authentication_required Requires user context
43
+ # @param slug [String] The short name of list or a category.
44
+ # @param options [Hash] A customizable set of options.
45
+ # @return [Array<Twitter::User>]
46
+ # @example Return the users in the Art & Design category and their most recent Tweet if they are not a protected user
47
+ # Twitter.suggest_users("art-design")
48
+ def suggest_users(slug, options={})
49
+ collection_from_response(Twitter::User, :get, "/1.1/users/suggestions/#{slug}/members.json", options)
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,213 @@
1
+ require 'twitter/api/utils'
2
+ require 'twitter/tweet'
3
+ require 'twitter/user'
4
+
5
+ module Twitter
6
+ module API
7
+ module Timelines
8
+ include Twitter::API::Utils
9
+ DEFAULT_TWEETS_PER_REQUEST = 20
10
+ MAX_TWEETS_PER_REQUEST = 200
11
+
12
+ # Returns the 20 most recent mentions (statuses containing @username) for the authenticating user
13
+ #
14
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/mentions_timeline
15
+ # @note This method can only return up to 800 Tweets.
16
+ # @rate_limited Yes
17
+ # @authentication_required Requires user context
18
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
19
+ # @return [Array<Twitter::Tweet>]
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 [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.
25
+ # @example Return the 20 most recent mentions (statuses containing @username) for the authenticating user
26
+ # Twitter.mentions
27
+ def mentions_timeline(options={})
28
+ collection_from_response(Twitter::Tweet, :get, "/1.1/statuses/mentions_timeline.json", options)
29
+ end
30
+ alias mentions mentions_timeline
31
+
32
+ # Returns the 20 most recent Tweets posted by the specified user
33
+ #
34
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
35
+ # @note This method can only return up to 3,200 Tweets.
36
+ # @rate_limited Yes
37
+ # @authentication_required Requires user context
38
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
39
+ # @return [Array<Twitter::Tweet>]
40
+ # @overload user_timeline(user, options={})
41
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
42
+ # @param options [Hash] A customizable set of options.
43
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
44
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
45
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
46
+ # @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.
47
+ # @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.
48
+ # @option options [Boolean, String, Integer] :contributor_details Specifies that the contributors element should be enhanced to include the screen_name of the contributor.
49
+ # @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.
50
+ # @example Return the 20 most recent Tweets posted by @sferik
51
+ # Twitter.user_timeline('sferik')
52
+ def user_timeline(*args)
53
+ objects_from_response(Twitter::Tweet, :get, "/1.1/statuses/user_timeline.json", args)
54
+ end
55
+
56
+ # Returns the 20 most recent retweets posted by the specified user
57
+ #
58
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
59
+ # @note This method can only return up to 3,200 Tweets.
60
+ # @rate_limited Yes
61
+ # @authentication_required Requires user context
62
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
63
+ # @return [Array<Twitter::Tweet>]
64
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
65
+ # @param options [Hash] A customizable set of options.
66
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
67
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
68
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
69
+ # @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.
70
+ # @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.
71
+ # @option options [Boolean, String, Integer] :contributor_details Specifies that the contributors element should be enhanced to include the screen_name of the contributor.
72
+ # @example Return the 20 most recent retweets posted by @sferik
73
+ # Twitter.retweeted_by_user('sferik')
74
+ def retweeted_by_user(user, options={})
75
+ options[:include_rts] = true
76
+ count = options[:count] || DEFAULT_TWEETS_PER_REQUEST
77
+ collect_with_count(count) do |count_options|
78
+ select_retweets(user_timeline(user, options.merge(count_options)))
79
+ end
80
+ end
81
+ alias retweeted_by retweeted_by_user
82
+
83
+ # Returns the 20 most recent retweets posted by the authenticating user
84
+ #
85
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
86
+ # @note This method can only return up to 3,200 Tweets.
87
+ # @rate_limited Yes
88
+ # @authentication_required Requires user context
89
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
90
+ # @return [Array<Twitter::Tweet>]
91
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
92
+ # @param options [Hash] A customizable set of options.
93
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
94
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
95
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
96
+ # @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.
97
+ # @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.
98
+ # @option options [Boolean, String, Integer] :contributor_details Specifies that the contributors element should be enhanced to include the screen_name of the contributor.
99
+ # @example Return the 20 most recent retweets posted by the authenticating user
100
+ # Twitter.retweeted_by_me
101
+ def retweeted_by_me(options={})
102
+ options[:include_rts] = true
103
+ count = options[:count] || DEFAULT_TWEETS_PER_REQUEST
104
+ collect_with_count(count) do |count_options|
105
+ select_retweets(user_timeline(options.merge(count_options)))
106
+ end
107
+ end
108
+
109
+ # Returns the 20 most recent Tweets, including retweets if they exist, posted by the authenticating user and the users they follow
110
+ #
111
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline
112
+ # @note This method can only return up to 800 Tweets, including retweets.
113
+ # @rate_limited Yes
114
+ # @authentication_required Requires user context
115
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
116
+ # @return [Array<Twitter::Tweet>]
117
+ # @param options [Hash] A customizable set of options.
118
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
119
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
120
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
121
+ # @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.
122
+ # @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.
123
+ # @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.
124
+ # @option options [Boolean, String, Integer] :contributor_details Specifies that the contributors element should be enhanced to include the screen_name of the contributor.
125
+ # @option options [Boolean, String, Integer] :include_entities The tweet entities node will be disincluded when set to false.
126
+ # @example Return the 20 most recent Tweets, including retweets if they exist, posted by the authenticating user and the users they follow
127
+ # Twitter.home_timeline
128
+ def home_timeline(options={})
129
+ collection_from_response(Twitter::Tweet, :get, "/1.1/statuses/home_timeline.json", options)
130
+ end
131
+
132
+ # Returns the 20 most recent retweets posted by users the authenticating user follow.
133
+ #
134
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline
135
+ # @note This method can only return up to 800 Tweets, including retweets.
136
+ # @rate_limited Yes
137
+ # @authentication_required Requires user context
138
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
139
+ # @return [Array<Twitter::Tweet>]
140
+ # @param options [Hash] A customizable set of options.
141
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
142
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
143
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
144
+ # @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.
145
+ # @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.
146
+ # @option options [Boolean, String, Integer] :contributor_details Specifies that the contributors element should be enhanced to include the screen_name of the contributor.
147
+ # @option options [Boolean, String, Integer] :include_entities The tweet entities node will be disincluded when set to false.
148
+ # @example Return the 20 most recent retweets posted by users followed by the authenticating user
149
+ # Twitter.retweeted_to_me
150
+ def retweeted_to_me(options={})
151
+ options[:include_rts] = true
152
+ count = options[:count] || DEFAULT_TWEETS_PER_REQUEST
153
+ collect_with_count(count) do |count_options|
154
+ select_retweets(home_timeline(options.merge(count_options)))
155
+ end
156
+ end
157
+
158
+ # Returns the 20 most recent tweets of the authenticated user that have been retweeted by others
159
+ #
160
+ # @see https://dev.twitter.com/docs/api/1.1/get/statuses/retweets_of_me
161
+ # @rate_limited Yes
162
+ # @authentication_required Requires user context
163
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
164
+ # @return [Array<Twitter::Tweet>]
165
+ # @param options [Hash] A customizable set of options.
166
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
167
+ # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
168
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
169
+ # @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.
170
+ # @option options [Boolean, String, Integer] :include_entities The tweet entities node will be disincluded when set to false.
171
+ # @option options [Boolean, String, Integer] :include_user_entities The user entities node will be disincluded when set to false.
172
+ # @example Return the 20 most recent tweets of the authenticated user that have been retweeted by others
173
+ # Twitter.retweets_of_me
174
+ def retweets_of_me(options={})
175
+ collection_from_response(Twitter::Tweet, :get, "/1.1/statuses/retweets_of_me.json", options)
176
+ end
177
+
178
+ private
179
+
180
+ # @param collection [Array]
181
+ # @param max_id [Integer, NilClass]
182
+ # @return [Array]
183
+ def collect_with_max_id(collection=[], max_id=nil, &block)
184
+ tweets = yield(max_id)
185
+ return collection if tweets.nil?
186
+ collection += tweets
187
+ tweets.empty? ? collection.flatten : collect_with_max_id(collection, tweets.last.id - 1, &block)
188
+ end
189
+
190
+ # @param count [Integer]
191
+ # @return [Array]
192
+ def collect_with_count(count, &block)
193
+ options = {}
194
+ options[:count] = MAX_TWEETS_PER_REQUEST
195
+ collect_with_max_id do |max_id|
196
+ options[:max_id] = max_id unless max_id.nil?
197
+ if count > 0
198
+ tweets = yield(options)
199
+ count -= tweets.length
200
+ tweets
201
+ end
202
+ end.flatten.compact[0...count]
203
+ end
204
+
205
+ # @param tweets [Array]
206
+ # @return [Array]
207
+ def select_retweets(tweets)
208
+ tweets.select(&:retweet?)
209
+ end
210
+
211
+ end
212
+ end
213
+ end