From 5629ec8eedb0a8e9b84f6592e322ad817ac8ac2e Mon Sep 17 00:00:00 2001 From: Robert Ingrum Date: Tue, 21 Jun 2016 15:09:47 -0600 Subject: [PATCH] Quick and dirty patch to make collections work. --- .gitignore | 4 ++++ lib/twitter/rest/api.rb | 2 ++ lib/twitter/rest/collections.rb | 29 +++++++++++++++++++++++++++++ lib/twitter/rest/utils.rb | 14 ++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 lib/twitter/rest/collections.rb diff --git a/.gitignore b/.gitignore index af948e388..e22cecf1c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,7 @@ doc/* log/* measurement/* pkg/* + +# Ignore IntelliJ specific files +*.iml +.idea \ No newline at end of file diff --git a/lib/twitter/rest/api.rb b/lib/twitter/rest/api.rb index 5cf9b58de..b6f5cc679 100644 --- a/lib/twitter/rest/api.rb +++ b/lib/twitter/rest/api.rb @@ -1,3 +1,4 @@ +require 'twitter/rest/collections' require 'twitter/rest/direct_messages' require 'twitter/rest/favorites' require 'twitter/rest/friends_and_followers' @@ -20,6 +21,7 @@ module REST # @note All methods have been separated into modules and follow the same grouping used in {http://dev.twitter.com/doc the Twitter API Documentation}. # @see https://dev.twitter.com/overview/general/things-every-developer-should-know module API + include Twitter::REST::Collections include Twitter::REST::DirectMessages include Twitter::REST::Favorites include Twitter::REST::FriendsAndFollowers diff --git a/lib/twitter/rest/collections.rb b/lib/twitter/rest/collections.rb new file mode 100644 index 000000000..36678b8ac --- /dev/null +++ b/lib/twitter/rest/collections.rb @@ -0,0 +1,29 @@ +require 'twitter/rest/utils' +require 'twitter/tweet' + +module Twitter + module REST + module Collections + include Twitter::REST::Utils + DEFAULT_TWEETS_PER_REQUEST = 20 + MAX_TWEETS_PER_REQUEST = 200 + + # Returns the 20 most recent tweets in the given collection + # + # @see https://dev.twitter.com/rest/reference/get/collections/list + # @rate_limited Yes + # @authentication Requires user context + # @raise [Twitter::Error::Unauthorized] Error raised when supplied use credentials are not valid. + # @return [Array] + # @overload user_timeline(collection_id, options = {}) + # @param collection_id [String] A twitter collection id + # @param options [Hash] A customizable set of options. + # @option options [Integer] :count Specifies the maximum number of results to include in the response. Between 1 and 200. + # @option options [Integer] :max_position Returns results with a position value less than or equal to the specified position. + # @option options [Integer] :min_position Returns results with a position greater than the specified position. + def collection_entries(*args) + objects_from_response_with_collection(Twitter::Tweet, :get, '/1.1/collections/entries.json', args) + end + end + end +end \ No newline at end of file diff --git a/lib/twitter/rest/utils.rb b/lib/twitter/rest/utils.rb index e254f599c..469d9c320 100644 --- a/lib/twitter/rest/utils.rb +++ b/lib/twitter/rest/utils.rb @@ -6,6 +6,8 @@ require 'twitter/utils' require 'uri' +require 'pp' + module Twitter module REST module Utils @@ -97,6 +99,12 @@ def perform_request_with_objects(request_method, path, options, klass) end end + def perform_request_with_collections(request_method, path, options, klass) + perform_request(request_method, path, options)[:objects][:tweets].values.collect do |element| + klass.new(element) + end + end + # @param path [String] # @param options [Hash] # @param collection_name [Symbol] @@ -139,6 +147,12 @@ def objects_from_response_with_user(klass, request_method, path, args) perform_request_with_objects(request_method, path, arguments.options, klass) end + def objects_from_response_with_collection(klass, request_method, path, args) + arguments = Twitter::Arguments.new(args) + set_compound_key('id', arguments.pop, arguments.options) + perform_request_with_collections(request_method, path, arguments.options, klass) + end + # @param klass [Class] # @param request_method [Symbol] # @param path [String]