Skip to content

Commit

Permalink
Add Twitter::API::Arguments class; remove extract_options! helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Dec 23, 2012
1 parent 37c562f commit 1a4d9f3
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 115 deletions.
13 changes: 13 additions & 0 deletions lib/twitter/api/arguments.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 6 additions & 5 deletions lib/twitter/api/direct_messages.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/direct_message'
require 'twitter/user'
Expand Down Expand Up @@ -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
Expand Down
21 changes: 11 additions & 10 deletions lib/twitter/api/favorites.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/error/already_favorited'
require 'twitter/error/forbidden'
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
22 changes: 11 additions & 11 deletions lib/twitter/api/friends_and_followers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/cursor'
require 'twitter/error/forbidden'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down
56 changes: 29 additions & 27 deletions lib/twitter/api/lists.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
17 changes: 9 additions & 8 deletions lib/twitter/api/saved_searches.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/saved_search'

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions lib/twitter/api/suggested_users.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/suggestion'
require 'twitter/user'
Expand Down Expand Up @@ -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

Expand Down
25 changes: 13 additions & 12 deletions lib/twitter/api/tweets.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/error/already_retweeted'
require 'twitter/error/forbidden'
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
7 changes: 4 additions & 3 deletions lib/twitter/api/undocumented.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'twitter/action_factory'
require 'twitter/api/arguments'
require 'twitter/api/utils'
require 'twitter/cursor'
require 'twitter/tweet'
Expand Down Expand Up @@ -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

Expand Down
Loading

0 comments on commit 1a4d9f3

Please sign in to comment.