diff --git a/lib/twitter/rest/api/direct_messages.rb b/lib/twitter/rest/api/direct_messages.rb index 667bdacb0..eb0c4a35c 100644 --- a/lib/twitter/rest/api/direct_messages.rb +++ b/lib/twitter/rest/api/direct_messages.rb @@ -9,6 +9,7 @@ module REST module API module DirectMessages include Twitter::REST::API::Utils + include Twitter::Utils # Returns the 20 most recent direct messages sent to the authenticating user # @@ -89,7 +90,7 @@ def direct_messages(*args) if arguments.empty? direct_messages_received(arguments.options) else - Twitter::Utils.parallel_map(arguments) do |id| + parallel_map(arguments) do |id| direct_message(id, arguments.options) end end diff --git a/lib/twitter/rest/api/favorites.rb b/lib/twitter/rest/api/favorites.rb index 9e23b7f9f..9aed7c9fc 100644 --- a/lib/twitter/rest/api/favorites.rb +++ b/lib/twitter/rest/api/favorites.rb @@ -11,6 +11,7 @@ module REST module API module Favorites include Twitter::REST::API::Utils + include Twitter::Utils # @see https://dev.twitter.com/docs/api/1.1/get/favorites/list # @rate_limited Yes @@ -68,7 +69,7 @@ def unfavorite(*args) # @param options [Hash] A customizable set of options. def favorite(*args) arguments = Twitter::Arguments.new(args) - Twitter::Utils.parallel_map(arguments) do |tweet| + parallel_map(arguments) do |tweet| id = extract_id(tweet) begin object_from_response(Twitter::Tweet, :post, '/1.1/favorites/create.json', arguments.options.merge(:id => id)) @@ -96,7 +97,7 @@ def favorite(*args) # @param options [Hash] A customizable set of options. def favorite!(*args) arguments = Twitter::Arguments.new(args) - Twitter::Utils.parallel_map(arguments) do |tweet| + parallel_map(arguments) do |tweet| id = extract_id(tweet) begin object_from_response(Twitter::Tweet, :post, '/1.1/favorites/create.json', arguments.options.merge(:id => id)) diff --git a/lib/twitter/rest/api/friends_and_followers.rb b/lib/twitter/rest/api/friends_and_followers.rb index 747f9dddf..bd1978dad 100644 --- a/lib/twitter/rest/api/friends_and_followers.rb +++ b/lib/twitter/rest/api/friends_and_followers.rb @@ -11,6 +11,7 @@ module REST module API module FriendsAndFollowers include Twitter::REST::API::Utils + include Twitter::Utils # @see https://dev.twitter.com/docs/api/1.1/get/friends/ids # @rate_limited Yes @@ -137,7 +138,7 @@ def follow(*args) # @option options [Boolean] :follow (false) Enable notifications for the target user. def follow!(*args) arguments = Twitter::Arguments.new(args) - Twitter::Utils.parallel_map(arguments) do |user| + parallel_map(arguments) do |user| object_from_response(Twitter::User, :post, '/1.1/friendships/create.json', merge_user(arguments.options, user)) end.compact end diff --git a/lib/twitter/rest/api/lists.rb b/lib/twitter/rest/api/lists.rb index 20e8b6299..5beb815c3 100644 --- a/lib/twitter/rest/api/lists.rb +++ b/lib/twitter/rest/api/lists.rb @@ -13,6 +13,7 @@ module REST module API module Lists include Twitter::REST::API::Utils + include Twitter::Utils MAX_USERS_PER_REQUEST = 100 URI_SUBSTRING = '://' @@ -446,7 +447,7 @@ def list_from_response_with_users(request_method, path, args) members = arguments.pop merge_list!(options, arguments.pop) merge_owner!(options, arguments.pop) - Twitter::Utils.parallel_map(members.flatten.each_slice(MAX_USERS_PER_REQUEST)) do |users| + parallel_map(members.flatten.each_slice(MAX_USERS_PER_REQUEST)) do |users| object_from_response(Twitter::List, request_method, path, merge_users(options, users)) end.last end diff --git a/lib/twitter/rest/api/saved_searches.rb b/lib/twitter/rest/api/saved_searches.rb index 46a33671a..0392522df 100644 --- a/lib/twitter/rest/api/saved_searches.rb +++ b/lib/twitter/rest/api/saved_searches.rb @@ -8,6 +8,7 @@ module REST module API module SavedSearches include Twitter::REST::API::Utils + include Twitter::Utils # @rate_limited Yes # @authentication Requires user context @@ -34,7 +35,7 @@ def saved_searches(*args) if arguments.empty? objects_from_response(Twitter::SavedSearch, :get, '/1.1/saved_searches/list.json', arguments.options) else - Twitter::Utils.parallel_map(arguments) do |id| + parallel_map(arguments) do |id| saved_search(id, arguments.options) end end @@ -82,7 +83,7 @@ def create_saved_search(query, options = {}) # @param options [Hash] A customizable set of options. def destroy_saved_search(*args) arguments = Twitter::Arguments.new(args) - Twitter::Utils.parallel_map(arguments) do |id| + parallel_map(arguments) do |id| object_from_response(Twitter::SavedSearch, :post, "/1.1/saved_searches/destroy/#{id}.json", arguments.options) end end diff --git a/lib/twitter/rest/api/tweets.rb b/lib/twitter/rest/api/tweets.rb index fae293b54..8d22157a5 100644 --- a/lib/twitter/rest/api/tweets.rb +++ b/lib/twitter/rest/api/tweets.rb @@ -12,6 +12,7 @@ module REST module API module Tweets include Twitter::REST::API::Utils + include Twitter::Utils # Returns up to 100 of the first retweets of a given tweet # @@ -143,7 +144,7 @@ def update(status, options = {}) # @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. def retweet(*args) arguments = Twitter::Arguments.new(args) - Twitter::Utils.parallel_map(arguments) do |tweet| + parallel_map(arguments) do |tweet| id = extract_id(tweet) begin post_retweet(id, arguments.options) @@ -169,7 +170,7 @@ def retweet(*args) # @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. def retweet!(*args) arguments = Twitter::Arguments.new(args) - Twitter::Utils.parallel_map(arguments) do |tweet| + parallel_map(arguments) do |tweet| id = extract_id(tweet) begin post_retweet(id, arguments.options) @@ -250,7 +251,7 @@ def oembed(tweet, options = {}) # @option options [String] :lang Language code for the rendered embed. This will affect the text and localization of the rendered HTML. def oembeds(*args) arguments = Twitter::Arguments.new(args) - Twitter::Utils.parallel_map(arguments) do |tweet| + parallel_map(arguments) do |tweet| id = extract_id(tweet) oembed(id, arguments.options) end @@ -282,7 +283,7 @@ def retweeters_ids(*args) # @return [Array] def parallel_tweets_from_response(request_method, path, args) arguments = Twitter::Arguments.new(args) - Twitter::Utils.parallel_map(arguments) do |tweet| + parallel_map(arguments) do |tweet| id = extract_id(tweet) object_from_response(Twitter::Tweet, request_method, path + "/#{id}.json", arguments.options) end diff --git a/lib/twitter/rest/api/users.rb b/lib/twitter/rest/api/users.rb index 5c22e7f41..30d88c42f 100644 --- a/lib/twitter/rest/api/users.rb +++ b/lib/twitter/rest/api/users.rb @@ -11,6 +11,7 @@ module REST module API module Users include Twitter::REST::API::Utils + include Twitter::Utils MAX_USERS_PER_REQUEST = 100 # Updates the authenticating user's settings. @@ -227,7 +228,7 @@ def unblock(*args) def users(*args) arguments = Twitter::Arguments.new(args) method = arguments.options.delete(:method) || :post - Twitter::Utils.parallel_map(arguments.each_slice(MAX_USERS_PER_REQUEST)) do |users| + parallel_map(arguments.each_slice(MAX_USERS_PER_REQUEST)) do |users| objects_from_response(Twitter::User, method, '/1.1/users/lookup.json', merge_users(arguments.options, users)) end.flatten end diff --git a/lib/twitter/rest/api/utils.rb b/lib/twitter/rest/api/utils.rb index f7eddcfea..e0e10fdbb 100644 --- a/lib/twitter/rest/api/utils.rb +++ b/lib/twitter/rest/api/utils.rb @@ -8,24 +8,10 @@ module Twitter module REST module API module Utils + include Twitter::Utils DEFAULT_CURSOR = -1 URI_SUBSTRING = '://' - class << self - def included(base) - base.extend(ClassMethods) - end - end - - module ClassMethods - def deprecate_alias(new_name, old_name) - define_method(new_name) do |*args, &block| - warn "#{Kernel.caller.first}: [DEPRECATION] ##{new_name} is deprecated. Use ##{old_name} instead." - send(old_name, *args, &block) - end - end - end - private # Take a URI string or Twitter::Identity object and return its ID @@ -51,7 +37,7 @@ def extract_id(object) # @return [Array] def parallel_user_objects_from_response(request_method, path, args) arguments = Twitter::Arguments.new(args) - Twitter::Utils.parallel_map(arguments) do |user| + parallel_map(arguments) do |user| object_from_response(Twitter::User, request_method, path, merge_user(arguments.options, user)) end end @@ -103,7 +89,7 @@ def objects_from_array(klass, array) # @return [Array] def parallel_objects_from_response(klass, request_method, path, args) # rubocop:disable ParameterLists arguments = Twitter::Arguments.new(args) - Twitter::Utils.parallel_map(arguments) do |object| + parallel_map(arguments) do |object| id = extract_id(object) object_from_response(klass, request_method, path, arguments.options.merge(:id => id)) end diff --git a/lib/twitter/utils.rb b/lib/twitter/utils.rb index 2283dc000..eda225473 100644 --- a/lib/twitter/utils.rb +++ b/lib/twitter/utils.rb @@ -1,6 +1,20 @@ module Twitter module Utils - module_function + class << self + def included(base) + base.extend(ClassMethods) + end + end + + module ClassMethods + def deprecate_alias(new_name, old_name) + define_method(new_name) do |*args, &block| + warn "#{Kernel.caller.first}: [DEPRECATION] ##{new_name} is deprecated. Use ##{old_name} instead." + send(old_name, *args, &block) + end + end + end + def parallel_map(enumerable) # Don't bother spawning a new thread if there's only one item if enumerable.count == 1 @@ -9,5 +23,6 @@ def parallel_map(enumerable) enumerable.map { |object| Thread.new { yield object } }.map(&:value) end end + module_function :parallel_map end end