From a556028ace04cb00c3c2b9cb8f72f792a86f04d6 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Wed, 18 Dec 2013 08:11:21 -0500 Subject: [PATCH] Automatically flatten Twitter::Arguments --- lib/twitter/arguments.rb | 2 +- lib/twitter/rest/api/direct_messages.rb | 2 +- lib/twitter/rest/api/favorites.rb | 4 ++-- lib/twitter/rest/api/friends_and_followers.rb | 2 +- lib/twitter/rest/api/lists.rb | 9 +++++---- lib/twitter/rest/api/saved_searches.rb | 4 ++-- lib/twitter/rest/api/tweets.rb | 8 ++++---- lib/twitter/rest/api/users.rb | 2 +- lib/twitter/rest/api/utils.rb | 4 ++-- 9 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/twitter/arguments.rb b/lib/twitter/arguments.rb index e22466831..754673d3d 100644 --- a/lib/twitter/arguments.rb +++ b/lib/twitter/arguments.rb @@ -7,7 +7,7 @@ class Arguments < Array # @return [Twitter::Arguments] def initialize(args) @options = args.last.is_a?(::Hash) ? args.pop : {} - super(args) + super(args.flatten) end end end diff --git a/lib/twitter/rest/api/direct_messages.rb b/lib/twitter/rest/api/direct_messages.rb index b98528b12..1152b6762 100644 --- a/lib/twitter/rest/api/direct_messages.rb +++ b/lib/twitter/rest/api/direct_messages.rb @@ -89,7 +89,7 @@ def direct_messages(*args) if arguments.empty? direct_messages_received(arguments.options) else - Twitter::Utils.parallel_map(arguments.flatten) do |id| + Twitter::Utils.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 596ae1435..9a0354dee 100644 --- a/lib/twitter/rest/api/favorites.rb +++ b/lib/twitter/rest/api/favorites.rb @@ -68,7 +68,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.flatten) do |tweet| + Twitter::Utils.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 +96,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.flatten) do |tweet| + Twitter::Utils.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 728d4bedc..812d69ee8 100644 --- a/lib/twitter/rest/api/friends_and_followers.rb +++ b/lib/twitter/rest/api/friends_and_followers.rb @@ -137,7 +137,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.flatten) do |user| + Twitter::Utils.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 c8e3d0285..20e8b6299 100644 --- a/lib/twitter/rest/api/lists.rb +++ b/lib/twitter/rest/api/lists.rb @@ -441,12 +441,13 @@ def list_from_response_with_user(request_method, path, args) end def list_from_response_with_users(request_method, path, args) - arguments = Twitter::Arguments.new(args) + arguments = args.dup + options = arguments.last.is_a?(::Hash) ? arguments.pop : {} members = arguments.pop - merge_list!(arguments.options, arguments.pop) - merge_owner!(arguments.options, 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| - object_from_response(Twitter::List, request_method, path, merge_users(arguments.options, 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 3536cfa05..46a33671a 100644 --- a/lib/twitter/rest/api/saved_searches.rb +++ b/lib/twitter/rest/api/saved_searches.rb @@ -34,7 +34,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.flatten) do |id| + Twitter::Utils.parallel_map(arguments) do |id| saved_search(id, arguments.options) end end @@ -82,7 +82,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.flatten) do |id| + Twitter::Utils.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 ebd8ae7d2..568ef1737 100644 --- a/lib/twitter/rest/api/tweets.rb +++ b/lib/twitter/rest/api/tweets.rb @@ -143,7 +143,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.flatten) do |tweet| + Twitter::Utils.parallel_map(arguments) do |tweet| id = extract_id(tweet) begin post_retweet(id, arguments.options) @@ -169,7 +169,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.flatten) do |tweet| + Twitter::Utils.parallel_map(arguments) do |tweet| id = extract_id(tweet) begin post_retweet(id, arguments.options) @@ -250,7 +250,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.flatten) do |tweet| + Twitter::Utils.parallel_map(arguments) do |tweet| id = extract_id(tweet) oembed(id, arguments.options) end @@ -282,7 +282,7 @@ def retweeters_ids(*args) # @return [Array] def threaded_tweets_from_response(request_method, path, args) arguments = Twitter::Arguments.new(args) - Twitter::Utils.parallel_map(arguments.flatten) do |tweet| + Twitter::Utils.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 daad99449..80701cc00 100644 --- a/lib/twitter/rest/api/users.rb +++ b/lib/twitter/rest/api/users.rb @@ -227,7 +227,7 @@ def unblock(*args) def users(*args) arguments = Twitter::Arguments.new(args) method = arguments.options.delete(:method) || :post - Twitter::Utils.parallel_map(arguments.flatten.each_slice(MAX_USERS_PER_REQUEST)) do |users| + Twitter::Utils.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 70699ceef..f05230a07 100644 --- a/lib/twitter/rest/api/utils.rb +++ b/lib/twitter/rest/api/utils.rb @@ -51,7 +51,7 @@ def extract_id(object) # @return [Array] def threaded_user_objects_from_response(request_method, path, args) arguments = Twitter::Arguments.new(args) - Twitter::Utils.parallel_map(arguments.flatten) do |user| + Twitter::Utils.parallel_map(arguments) do |user| object_from_response(Twitter::User, request_method, path, merge_user(arguments.options, user)) end end @@ -103,7 +103,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) - Twitter::Utils.parallel_map(arguments.flatten) do |object| + Twitter::Utils.parallel_map(arguments) do |object| id = extract_id(object) object_from_response(klass, request_method, path, arguments.options.merge(:id => id)) end