Permalink
Browse files

Add Twitter::API::Arguments class; remove extract_options! helper method

  • Loading branch information...
1 parent 496b745 commit 65972c599ced8da27fbbfa72aeead92464355583 @sferik committed Dec 22, 2012
@@ -0,0 +1,13 @@
+module Twitter
+ module API
+ class Arguments < Array
+ attr_reader :options
+
+ def initialize(args)
+ @options = args.last.is_a?(::Hash) ? args.pop : {}
+ super(args)
+ end
+
+ end
+ end
+end
@@ -1,3 +1,4 @@
+require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/direct_message'
require 'twitter/user'
@@ -92,12 +93,12 @@ def direct_message(id, options={})
# @param ids [Array<Integer>, Set<Integer>] An array of Tweet IDs.
# @param options [Hash] A customizable set of options.
def direct_messages(*args)
- options = extract_options!(args)
- if args.empty?
- direct_messages_received(options)
+ arguments = Twitter::API::Arguments.new(args)
+ if arguments.empty?
+ direct_messages_received(arguments.options)
else
- args.flatten.threaded_map do |id|
- direct_message(id, options)
+ arguments.flatten.threaded_map do |id|
+ direct_message(id, arguments.options)
end
end
end
@@ -1,3 +1,4 @@
+require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/error/already_favorited'
require 'twitter/error/forbidden'
@@ -32,11 +33,11 @@ module Favorites
# @example Return the 20 most recent favorite Tweets for @sferik
# Twitter.favorites('sferik')
def favorites(*args)
- options = extract_options!(args)
- if user = args.pop
- merge_user!(options, user)
+ arguments = Twitter::API::Arguments.new(args)
+ if user = arguments.pop
+ merge_user!(arguments.options, user)
end
- objects_from_response(Twitter::Tweet, :get, "/1.1/favorites/list.json", options)
+ objects_from_response(Twitter::Tweet, :get, "/1.1/favorites/list.json", arguments.options)
end
alias favourites favorites
@@ -76,10 +77,10 @@ def unfavorite(*args)
# @param ids [Array<Integer>, Set<Integer>] An array of Tweet IDs.
# @param options [Hash] A customizable set of options.
def favorite(*args)
- options = extract_options!(args)
- args.flatten.threaded_map do |id|
+ arguments = Twitter::API::Arguments.new(args)
+ arguments.flatten.threaded_map do |id|
begin
- object_from_response(Twitter::Tweet, :post, "/1.1/favorites/create.json", options.merge(:id => id))
+ object_from_response(Twitter::Tweet, :post, "/1.1/favorites/create.json", arguments.options.merge(:id => id))
rescue Twitter::Error::Forbidden => error
raise unless error.message == Twitter::Error::AlreadyFavorited::MESSAGE
end
@@ -106,10 +107,10 @@ def favorite(*args)
# @param ids [Array<Integer>, Set<Integer>] An array of Tweet IDs.
# @param options [Hash] A customizable set of options.
def favorite!(*args)
- options = extract_options!(args)
- args.flatten.threaded_map do |id|
+ arguments = Twitter::API::Arguments.new(args)
+ arguments.flatten.threaded_map do |id|
begin
- object_from_response(Twitter::Tweet, :post, "/1.1/favorites/create.json", options.merge(:id => id))
+ object_from_response(Twitter::Tweet, :post, "/1.1/favorites/create.json", arguments.options.merge(:id => id))
rescue Twitter::Error::Forbidden => error
handle_forbidden_error(Twitter::Error::AlreadyFavorited, error)
end
@@ -1,3 +1,4 @@
+require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/cursor'
require 'twitter/error/forbidden'
@@ -76,9 +77,9 @@ def follower_ids(*args)
# @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen names, or objects.
# @param options [Hash] A customizable set of options.
def friendships(*args)
- options = extract_options!(args)
- merge_users!(options, Array(args))
- objects_from_response(Twitter::User, :get, "/1.1/friendships/lookup.json", options)
+ arguments = Twitter::API::Arguments.new(args)
+ merge_users!(arguments.options, arguments)
+ objects_from_response(Twitter::User, :get, "/1.1/friendships/lookup.json", arguments.options)
end
# Returns an array of numeric IDs for every user who has a pending request to follow the authenticating user
@@ -127,17 +128,17 @@ def friendships_outgoing(options={})
# @param options [Hash] A customizable set of options.
# @option options [Boolean] :follow (false) Enable notifications for the target user.
def follow(*args)
- options = extract_options!(args)
+ arguments = Twitter::API::Arguments.new(args)
# Twitter always turns on notifications if the "follow" option is present, even if it's set to false
# so only send follow if it's true
- options[:follow] = true if !!options.delete(:follow)
+ arguments.options[:follow] = true if !!arguments.options.delete(:follow)
existing_friends = Thread.new do
friend_ids.ids
end
new_friends = Thread.new do
users(args).map(&:id)
end
- follow!(new_friends.value - existing_friends.value, options)
+ follow!(new_friends.value - existing_friends.value, arguments.options)
end
alias friendship_create follow
@@ -157,14 +158,13 @@ def follow(*args)
# @param options [Hash] A customizable set of options.
# @option options [Boolean] :follow (false) Enable notifications for the target user.
def follow!(*args)
- options = extract_options!(args)
+ arguments = Twitter::API::Arguments.new(args)
# Twitter always turns on notifications if the "follow" option is present, even if it's set to false
# so only send follow if it's true
- options[:follow] = true if !!options.delete(:follow)
- args.flatten.threaded_map do |user|
+ arguments.options[:follow] = true if !!arguments.options.delete(:follow)
+ arguments.flatten.threaded_map do |user|
begin
- merge_user!(options, user)
- object_from_response(Twitter::User, :post, "/1.1/friendships/create.json", options)
+ object_from_response(Twitter::User, :post, "/1.1/friendships/create.json", merge_user(arguments.options, user))
rescue Twitter::Error::Forbidden
# This error will be raised if the user doesn't have permission to
# follow list_member, for whatever reason.
View
@@ -1,4 +1,6 @@
+require 'twitter/api/arguments'
require 'twitter/api/utils'
+require 'twitter/core_ext/enumerable'
require 'twitter/cursor'
require 'twitter/error/forbidden'
require 'twitter/error/not_found'
@@ -63,10 +65,10 @@ def lists(*args)
# Twitter.list_timeline(7505382, 'presidents')
# Twitter.list_timeline(7505382, 8863586)
def list_timeline(*args)
- options = extract_options!(args)
- merge_list!(options, args.pop)
- merge_owner!(options, args.pop || screen_name) unless options[:owner_id] || options[:owner_screen_name]
- objects_from_response(Twitter::Tweet, :get, "/1.1/lists/statuses.json", options)
+ arguments = Twitter::API::Arguments.new(args)
+ merge_list!(arguments.options, arguments.pop)
+ merge_owner!(arguments.options, arguments.pop || screen_name) unless arguments.options[:owner_id] || arguments.options[:owner_screen_name]
+ objects_from_response(Twitter::Tweet, :get, "/1.1/lists/statuses.json", arguments.options)
end
# Removes the specified member from the list
@@ -511,45 +513,45 @@ def list_remove_members(*args)
# @param args [Array]
# @return [Array<Twitter::User>]
def list_from_response(request_method, path, args)
- options = extract_options!(args)
- merge_list!(options, args.pop)
- merge_owner!(options, args.pop || screen_name) unless options[:owner_id] || options[:owner_screen_name]
- object_from_response(Twitter::List, request_method, path, options)
+ arguments = Twitter::API::Arguments.new(args)
+ merge_list!(arguments.options, arguments.pop)
+ merge_owner!(arguments.options, arguments.pop || screen_name) unless arguments.options[:owner_id] || arguments.options[:owner_screen_name]
+ object_from_response(Twitter::List, request_method, path, arguments.options)
end
def cursor_from_response_with_list(request_method, path, args, calling_method)
- options = extract_options!(args)
- merge_list!(options, args.pop)
- merge_owner!(options, args.pop || screen_name) unless options[:owner_id] || options[:owner_screen_name]
- cursor_from_response(:users, Twitter::User, request_method, path, options, calling_method)
+ arguments = Twitter::API::Arguments.new(args)
+ merge_list!(arguments.options, arguments.pop)
+ merge_owner!(arguments.options, arguments.pop || screen_name) unless arguments.options[:owner_id] || arguments.options[:owner_screen_name]
+ cursor_from_response(:users, Twitter::User, request_method, path, arguments.options, calling_method)
end
def list_user?(request_method, path, args)
- options = extract_options!(args)
- merge_user!(options, args.pop)
- merge_list!(options, args.pop)
- merge_owner!(options, args.pop || screen_name) unless options[:owner_id] || options[:owner_screen_name]
- send(request_method.to_sym, path, options)
+ arguments = Twitter::API::Arguments.new(args)
+ merge_user!(arguments.options, arguments.pop)
+ merge_list!(arguments.options, arguments.pop)
+ merge_owner!(arguments.options, arguments.pop || screen_name) unless arguments.options[:owner_id] || arguments.options[:owner_screen_name]
+ send(request_method.to_sym, path, arguments.options)
true
rescue Twitter::Error::NotFound, Twitter::Error::Forbidden
false
end
def list_from_response_with_user(request_method, path, args)
- options = extract_options!(args)
- merge_user!(options, args.pop)
- merge_list!(options, args.pop)
- merge_owner!(options, args.pop || screen_name) unless options[:owner_id] || options[:owner_screen_name]
- object_from_response(Twitter::List, request_method, path, options)
+ arguments = Twitter::API::Arguments.new(args)
+ merge_user!(arguments.options, arguments.pop)
+ merge_list!(arguments.options, arguments.pop)
+ merge_owner!(arguments.options, arguments.pop || screen_name) unless arguments.options[:owner_id] || arguments.options[:owner_screen_name]
+ object_from_response(Twitter::List, request_method, path, arguments.options)
end
def list_from_response_with_users(request_method, path, args)
- options = extract_options!(args)
- members = args.pop
- merge_list!(options, args.pop)
- merge_owner!(options, args.pop || screen_name) unless options[:owner_id] || options[:owner_screen_name]
+ arguments = Twitter::API::Arguments.new(args)
+ members = arguments.pop
+ merge_list!(arguments.options, arguments.pop)
+ merge_owner!(arguments.options, arguments.pop || screen_name) unless arguments.options[:owner_id] || arguments.options[:owner_screen_name]
members.flatten.each_slice(MAX_USERS_PER_REQUEST).threaded_map do |users|
- object_from_response(Twitter::List, request_method, path, merge_users(options, users))
+ object_from_response(Twitter::List, request_method, path, merge_users(arguments.options, users))
end.last
end
@@ -1,3 +1,4 @@
+require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/saved_search'
@@ -31,12 +32,12 @@ module SavedSearches
# @param ids [Array<Integer>, Set<Integer>] An array of Tweet IDs.
# @param options [Hash] A customizable set of options.
def saved_searches(*args)
- options = extract_options!(args)
- if args.empty?
- objects_from_response(Twitter::SavedSearch, :get, "/1.1/saved_searches/list.json", options)
+ arguments = Twitter::API::Arguments.new(args)
+ if arguments.empty?
+ objects_from_response(Twitter::SavedSearch, :get, "/1.1/saved_searches/list.json", arguments.options)
else
- args.flatten.threaded_map do |id|
- saved_search(id)
+ arguments.flatten.threaded_map do |id|
+ saved_search(id, arguments.options)
end
end
end
@@ -87,9 +88,9 @@ def saved_search_create(query, options={})
# @param ids [Array<Integer>, Set<Integer>] An array of Tweet IDs.
# @param options [Hash] A customizable set of options.
def saved_search_destroy(*args)
- options = extract_options!(args)
- args.flatten.threaded_map do |id|
- object_from_response(Twitter::SavedSearch, :post, "/1.1/saved_searches/destroy/#{id}.json", options)
+ arguments = Twitter::API::Arguments.new(args)
+ arguments.flatten.threaded_map do |id|
+ object_from_response(Twitter::SavedSearch, :post, "/1.1/saved_searches/destroy/#{id}.json", arguments.options)
end
end
@@ -1,3 +1,4 @@
+require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/suggestion'
require 'twitter/user'
@@ -27,11 +28,11 @@ module SuggestedUsers
# @example Return the users in the Art & Design category
# Twitter.suggestions("art-design")
def suggestions(*args)
- options = extract_options!(args)
- if slug = args.pop
- object_from_response(Twitter::Suggestion, :get, "/1.1/users/suggestions/#{slug}.json", options)
+ arguments = Twitter::API::Arguments.new(args)
+ if slug = arguments.pop
+ object_from_response(Twitter::Suggestion, :get, "/1.1/users/suggestions/#{slug}.json", arguments.options)
else
- objects_from_response(Twitter::Suggestion, :get, "/1.1/users/suggestions.json", options)
+ objects_from_response(Twitter::Suggestion, :get, "/1.1/users/suggestions.json", arguments.options)
end
end
View
@@ -1,3 +1,4 @@
+require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/error/already_retweeted'
require 'twitter/error/forbidden'
@@ -144,10 +145,10 @@ def update(status, options={})
# @param options [Hash] A customizable set of 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)
- options = extract_options!(args)
- args.flatten.threaded_map do |id|
+ arguments = Twitter::API::Arguments.new(args)
+ arguments.flatten.threaded_map do |id|
begin
- post_retweet(id, options)
+ post_retweet(id, arguments.options)
rescue Twitter::Error::Forbidden => error
raise unless error.message == Twitter::Error::AlreadyRetweeted::MESSAGE
end
@@ -171,10 +172,10 @@ def retweet(*args)
# @param options [Hash] A customizable set of 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)
- options = extract_options!(args)
- args.flatten.threaded_map do |id|
+ arguments = Twitter::API::Arguments.new(args)
+ arguments.flatten.threaded_map do |id|
begin
- post_retweet(id, options)
+ post_retweet(id, arguments.options)
rescue Twitter::Error::Forbidden => error
handle_forbidden_error(Twitter::Error::AlreadyRetweeted, error)
end
@@ -248,9 +249,9 @@ def oembed(id, options={})
# @option options [String] :related A value for the TWT related parameter, as described in {https://dev.twitter.com/docs/intents Web Intents}. This value will be forwarded to all Web Intents calls.
# @option options [String] :lang Language code for the rendered embed. This will affect the text and localization of the rendered HTML.
def oembeds(*args)
- options = extract_options!(args)
- args.flatten.threaded_map do |id|
- object_from_response(Twitter::OEmbed, :get, "/1.1/statuses/oembed.json?id=#{id}", options)
+ arguments = Twitter::API::Arguments.new(args)
+ arguments.flatten.threaded_map do |id|
+ object_from_response(Twitter::OEmbed, :get, "/1.1/statuses/oembed.json?id=#{id}", arguments.options)
end
end
@@ -261,9 +262,9 @@ def oembeds(*args)
# @param args [Array]
# @return [Array<Twitter::Tweet>]
def threaded_tweets_from_response(request_method, path, args)
- options = extract_options!(args)
- args.flatten.threaded_map do |id|
- object_from_response(Twitter::Tweet, request_method, path + "/#{id}.json", options)
+ arguments = Twitter::API::Arguments.new(args)
+ arguments.flatten.threaded_map do |id|
+ object_from_response(Twitter::Tweet, request_method, path + "/#{id}.json", arguments.options)
end
end
@@ -1,4 +1,5 @@
require 'twitter/action_factory'
+require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/cursor'
require 'twitter/tweet'
@@ -102,9 +103,9 @@ def status_activity(id, options={})
# @param ids [Array<Integer>, Set<Integer>] An array of Tweet IDs.
# @param options [Hash] A customizable set of options.
def statuses_activity(*args)
- options = extract_options!(args)
- args.flatten.threaded_map do |id|
- status_activity(id, options)
+ arguments = Twitter::API::Arguments.new(args)
+ arguments.flatten.threaded_map do |id|
+ status_activity(id, arguments.options)
end
end
Oops, something went wrong.

0 comments on commit 65972c5

Please sign in to comment.