diff --git a/lib/twitter/rest/api/direct_messages.rb b/lib/twitter/rest/api/direct_messages.rb index 14681b2b0..d8b1310f6 100644 --- a/lib/twitter/rest/api/direct_messages.rb +++ b/lib/twitter/rest/api/direct_messages.rb @@ -88,7 +88,7 @@ def direct_messages(*args) if arguments.empty? direct_messages_received(arguments.options) else - Util.threaded_map(arguments.flatten) do |id| + Util.parallel_map(arguments.flatten) 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 7f1b62bbd..cd83ac542 100644 --- a/lib/twitter/rest/api/favorites.rb +++ b/lib/twitter/rest/api/favorites.rb @@ -67,7 +67,7 @@ def unfavorite(*args) # @param options [Hash] A customizable set of options. def favorite(*args) arguments = Twitter::Arguments.new(args) - Util.threaded_map(arguments.flatten) do |tweet| + Util.parallel_map(arguments.flatten) do |tweet| id = extract_id(tweet) begin object_from_response(Twitter::Tweet, :post, '/1.1/favorites/create.json', arguments.options.merge(:id => id)) @@ -95,7 +95,7 @@ def favorite(*args) # @param options [Hash] A customizable set of options. def favorite!(*args) arguments = Twitter::Arguments.new(args) - Util.threaded_map(arguments.flatten) do |tweet| + Util.parallel_map(arguments.flatten) 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 899f59d5a..ca196aaa4 100644 --- a/lib/twitter/rest/api/friends_and_followers.rb +++ b/lib/twitter/rest/api/friends_and_followers.rb @@ -136,7 +136,7 @@ def follow(*args) # @option options [Boolean] :follow (false) Enable notifications for the target user. def follow!(*args) arguments = Twitter::Arguments.new(args) - Util.threaded_map(arguments.flatten) do |user| + Util.parallel_map(arguments.flatten) 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 9d3a0a52c..0cea4e0f8 100644 --- a/lib/twitter/rest/api/lists.rb +++ b/lib/twitter/rest/api/lists.rb @@ -444,7 +444,7 @@ def list_from_response_with_users(request_method, path, args) members = arguments.pop merge_list!(arguments.options, arguments.pop) merge_owner!(arguments.options, arguments.pop) - Util.threaded_map(members.flatten.each_slice(MAX_USERS_PER_REQUEST)) do |users| + Util.parallel_map(members.flatten.each_slice(MAX_USERS_PER_REQUEST)) do |users| object_from_response(Twitter::List, request_method, path, merge_users(arguments.options, users)) end.last end diff --git a/lib/twitter/rest/api/saved_searches.rb b/lib/twitter/rest/api/saved_searches.rb index afae51015..32673f085 100644 --- a/lib/twitter/rest/api/saved_searches.rb +++ b/lib/twitter/rest/api/saved_searches.rb @@ -33,7 +33,7 @@ def saved_searches(*args) if arguments.empty? objects_from_response(Twitter::SavedSearch, :get, '/1.1/saved_searches/list.json', arguments.options) else - Util.threaded_map(arguments.flatten) do |id| + Util.parallel_map(arguments.flatten) do |id| saved_search(id, arguments.options) end end @@ -81,7 +81,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) - Util.threaded_map(arguments.flatten) do |id| + Util.parallel_map(arguments.flatten) 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 d840e4f4b..3aed14c82 100644 --- a/lib/twitter/rest/api/tweets.rb +++ b/lib/twitter/rest/api/tweets.rb @@ -142,7 +142,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) - Util.threaded_map(arguments.flatten) do |tweet| + Util.parallel_map(arguments.flatten) do |tweet| id = extract_id(tweet) begin post_retweet(id, arguments.options) @@ -168,7 +168,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) - Util.threaded_map(arguments.flatten) do |tweet| + Util.parallel_map(arguments.flatten) do |tweet| id = extract_id(tweet) begin post_retweet(id, arguments.options) @@ -249,7 +249,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) - Util.threaded_map(arguments.flatten) do |tweet| + Util.parallel_map(arguments.flatten) do |tweet| id = extract_id(tweet) oembed(id, arguments.options) end @@ -281,7 +281,7 @@ def retweeters_ids(*args) # @return [Array] def threaded_tweets_from_response(request_method, path, args) arguments = Twitter::Arguments.new(args) - Util.threaded_map(arguments.flatten) do |tweet| + Util.parallel_map(arguments.flatten) 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 190d14daa..b20694e56 100644 --- a/lib/twitter/rest/api/users.rb +++ b/lib/twitter/rest/api/users.rb @@ -226,7 +226,7 @@ def unblock(*args) def users(*args) arguments = Twitter::Arguments.new(args) method = arguments.options.delete(:method) || :post - Util.threaded_map(arguments.flatten.each_slice(MAX_USERS_PER_REQUEST)) do |users| + Util.parallel_map(arguments.flatten.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 a52a22755..46c406300 100644 --- a/lib/twitter/rest/api/utils.rb +++ b/lib/twitter/rest/api/utils.rb @@ -50,7 +50,7 @@ def extract_id(object) # @return [Array] def threaded_user_objects_from_response(request_method, path, args) arguments = Twitter::Arguments.new(args) - Util.threaded_map(arguments.flatten) do |user| + Util.parallel_map(arguments.flatten) do |user| object_from_response(Twitter::User, request_method, path, merge_user(arguments.options, user)) end end @@ -102,7 +102,7 @@ def objects_from_array(klass, array) # @return [Array] def threaded_objects_from_response(klass, request_method, path, args) # rubocop:disable ParameterLists arguments = Twitter::Arguments.new(args) - Util.threaded_map(arguments.flatten) do |object| + Util.parallel_map(arguments.flatten) do |object| id = extract_id(object) object_from_response(klass, request_method, path, arguments.options.merge(:id => id)) end diff --git a/lib/twitter/util.rb b/lib/twitter/util.rb index c7fecd174..e6aebf6fe 100644 --- a/lib/twitter/util.rb +++ b/lib/twitter/util.rb @@ -1,11 +1,13 @@ module Twitter module Util - def self.threaded_map(enumerable) - # Don't bother spawning a new thread if there's only one item - if enumerable.count == 1 - enumerable.map { |object| yield object } - else - enumerable.map { |object| Thread.new { yield object } }.map(&:value) + class << self + def parallel_map(enumerable) + # Don't bother spawning a new thread if there's only one item + if enumerable.count == 1 + enumerable.map { |object| yield object } + else + enumerable.map { |object| Thread.new { yield object } }.map(&:value) + end end end end