From 2dd5d32ca1a67a88d61d2f762e011295cab8a9bd Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Sun, 20 May 2012 12:13:17 -1000 Subject: [PATCH] Allow methods to accept Twitter::User objects as arguments --- lib/twitter/client.rb | 188 +++++---- lib/twitter/core_ext/hash.rb | 46 ++- .../client/friends_and_followers_spec.rb | 117 +++++- spec/twitter/client/lists_spec.rb | 376 +++--------------- spec/twitter/client/users_spec.rb | 91 +++++ 5 files changed, 366 insertions(+), 452 deletions(-) diff --git a/lib/twitter/client.rb b/lib/twitter/client.rb index cc6b39957..f5318bf55 100644 --- a/lib/twitter/client.rb +++ b/lib/twitter/client.rb @@ -297,7 +297,7 @@ def blocked_ids(options={}) # @see https://dev.twitter.com/docs/api/1/get/blocks/exists # @requires_authentication Yes # @rate_limited Yes - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @return [Boolean] true if the authenticating user is blocking the target user, otherwise false. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -318,7 +318,7 @@ def block?(user, options={}) # @note Destroys a friendship to the blocked user if it exists. # @rate_limited Yes # @requires_authentication Yes - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @return [Twitter::User] The blocked user. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -336,7 +336,7 @@ def block(user, options={}) # @see https://dev.twitter.com/docs/api/1/post/blocks/destroy # @rate_limited No # @requires_authentication Yes - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @return [Twitter::User] The un-blocked user. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -413,7 +413,7 @@ def direct_message_destroy(id, options={}) # @see https://dev.twitter.com/docs/api/1/post/direct_messages/new # @rate_limited No # @requires_authentication Yes - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param text [String] The text of your direct message, up to 140 characters. # @param options [Hash] A customizable set of options. # @return [Twitter::DirectMessage] The sent message. @@ -460,7 +460,7 @@ def direct_message(id, options={}) # @overload favorites(user, options={}) # Returns the 20 most recent favorite statuses for the specified user # - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100. # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. @@ -528,7 +528,7 @@ def unfavorite(id, options={}) # @overload follower_ids(user, options={}) # Returns an array of numeric IDs for every user following the specified user # - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Integer] :cursor (-1) Breaks the results into pages. This is recommended for users who are following many users. Provide a value of -1 to begin paging. Provide values as returned in the response body's next_cursor and previous_cursor attributes to page back and forth in the list. # @return [Twitter::Cursor] @@ -560,7 +560,7 @@ def follower_ids(*args) # @overload friend_ids(user, options={}) # Returns an array of numeric IDs for every user the specified user is following # - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. # @return [Twitter::Cursor] @@ -582,14 +582,18 @@ def friend_ids(*args) # @note Consider using {Twitter::Client::FriendsAndFollowers#friendship} instead of this method. # @rate_limited Yes # @requires_authentication No unless user_a or user_b is protected - # @param user_a [Integer, String] The ID or screen_name of the subject user. - # @param user_b [Integer, String] The ID or screen_name of the user to test for following. + # @param user_a [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the subject user. + # @param user_b [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the user to test for following. # @param options [Hash] A customizable set of options. # @return [Boolean] true if user_a follows user_b, otherwise false. # @example Return true if @sferik follows @pengwynn # Twitter.friendship?('sferik', 'pengwynn') + # Twitter.friendship?('sferik', 14100886) # Same as above + # Twitter.friendship?(7505382, 14100886) # Same as above def friendship?(user_a, user_b, options={}) - get("/1/friendships/exists.json", options.merge(:user_a => user_a, :user_b => user_b)) + options.merge_user!(user_a, nil, "a") + options.merge_user!(user_b, nil, "b") + get("/1/friendships/exists.json", options) end # Returns an array of numeric IDs for every user who has a pending request to follow the authenticating user @@ -631,24 +635,19 @@ def friendships_outgoing(options={}) # @see https://dev.twitter.com/docs/api/1/get/friendships/show # @rate_limited Yes # @requires_authentication No + # @param source [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the source user. + # @param target [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the target user. # @param options [Hash] A customizable set of options. # @return [Twitter::Relationship] # @example Return the relationship between @sferik and @pengwynn # Twitter.friendship('sferik', 'pengwynn') - # Twitter.friendship(7505382, 14100886) + # Twitter.friendship('sferik', 14100886) # Same as above + # Twitter.friendship(7505382, 14100886) # Same as above def friendship(source, target, options={}) - case source - when Integer - options[:source_id] = source - when String - options[:source_screen_name] = source - end - case target - when Integer - options[:target_id] = target - when String - options[:target_screen_name] = target - end + options.merge_user!(source, "source") + options[:source_id] = options.delete(:source_user_id) unless options[:source_user_id].nil? + options.merge_user!(target, "target") + options[:target_id] = options.delete(:target_user_id) unless options[:target_user_id].nil? relationship = get("/1/friendships/show.json", options)['relationship'] Twitter::Relationship.new(relationship) end @@ -660,7 +659,7 @@ def friendship(source, target, options={}) # @see https://dev.twitter.com/docs/api/1/post/friendships/create # @rate_limited No # @requires_authentication Yes - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Boolean] :follow (false) Enable notifications for the target user. # @return [Twitter::User] The followed user. @@ -682,7 +681,7 @@ def follow(user, options={}) # @see https://dev.twitter.com/docs/api/1/post/friendships/destroy # @rate_limited No # @requires_authentication Yes - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @return [Twitter::User] The unfollowed user. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -703,7 +702,7 @@ def unfollow(user, options={}) # @param options [Hash] A customizable set of options. # @return [Twitter::Relationship] # @overload friendships(*users, options={}) - # @param users [Array, Set] Twitter user IDs or screen names. + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. # @param options [Hash] A customizable set of options. # @return [Array] The requested users. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -725,7 +724,7 @@ def friendships(*args) # @see https://dev.twitter.com/docs/api/1/post/friendships/update # @rate_limited No # @requires_authentication Yes - # @param user [Integer, String] Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Boolean] :device Enable/disable device notifications from the target user. # @option options [Boolean] :retweets Enable/disable retweets from the target user. @@ -744,7 +743,7 @@ def friendship_update(user, options={}) # @see https://dev.twitter.com/docs/api/1/get/friendships/no_retweet_ids # @rate_limited Yes # @requires_authentication Yes - # @param user [Integer, String] Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Boolean] :stringify_ids Many programming environments will not consume our ids due to their size. Provide this option to have ids returned as strings instead. Read more about Twitter IDs, JSON and Snowflake. # @return [Array] @@ -760,7 +759,7 @@ def no_retweet_ids(options={}) # @note Undocumented # @rate_limited No # @requires_authentication Yes - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @return [Twitter::User] The accepted user. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -777,7 +776,7 @@ def accept(user, options={}) # @note Undocumented # @rate_limited No # @requires_authentication Yes - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @return [Twitter::User] The denied user. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -851,7 +850,7 @@ def tos(options={}) # @example Return all lists the authenticating user subscribes to # Twitter.lists_subscribed_to # @overload lists_subscribed_to(user, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @return [Array] # @example Return all lists the specified user subscribes to @@ -883,7 +882,7 @@ def lists_subscribed_to(*args) # Twitter.list_timeline('presidents') # Twitter.list_timeline(8863586) # @overload list_timeline(user, list, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug of the list. # @param options [Hash] A customizable set of options. # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. @@ -922,7 +921,7 @@ def list_timeline(*args) # Twitter.list_remove_member('presidents', 'BarackObama') # Twitter.list_remove_member(8863586, 'BarackObama') # @overload list_remove_member(user, list, user_to_remove, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug of the list. # @param user_to_remove [Integer, String] The user id or screen name of the list member to remove. # @param options [Hash] A customizable set of options. @@ -958,7 +957,7 @@ def list_remove_member(*args) # @example List the lists the authenticated user has been added to # Twitter.memberships # @overload memberships(user, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. # @return [Twitter::Cursor] @@ -990,7 +989,7 @@ def memberships(*args) # Twitter.list_subscribers('presidents') # Twitter.list_subscribers(8863586) # @overload list_subscribers(user, list, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug of the list. # @param options [Hash] A customizable set of options. # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. @@ -1023,7 +1022,7 @@ def list_subscribers(*args) # @example List the lists the authenticated user follows # Twitter.subscriptions # @overload subscriptions(user, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. # @return [Twitter::Cursor] @@ -1054,7 +1053,7 @@ def subscriptions(*args) # Twitter.list_subscribe('presidents') # Twitter.list_subscribe(8863586) # @overload list_subscribe(user, list, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug of the list. # @param options [Hash] A customizable set of options. # @return [Twitter::List] The specified list. @@ -1080,7 +1079,7 @@ def list_subscribe(*args) # @requires_authentication Yes # @overload list_subscriber?(list, user_to_check, options={}) # @param list [Integer, String] The list_id or slug of the list. - # @param user_to_check [Integer, String] The user ID or screen_name of the list member. + # @param user_to_check [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @return [Boolean] true if user is a subscriber of the specified list, otherwise false. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -1089,9 +1088,9 @@ def list_subscribe(*args) # Twitter.list_subscriber?(8863586, 813286) # Twitter.list_subscriber?('presidents', 'BarackObama') # @overload list_subscriber?(user, list, user_to_check, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug of the list. - # @param user_to_check [Integer, String] The user ID or screen_name of the list member. + # @param user_to_check [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @return [Boolean] true if user is a subscriber of the specified list, otherwise false. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -1129,7 +1128,7 @@ def list_subscriber?(*args) # Twitter.list_unsubscribe('presidents') # Twitter.list_unsubscribe(8863586) # @overload list_unsubscribe(user, list, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug of the list. # @param options [Hash] A customizable set of options. # @return [Twitter::List] The specified list. @@ -1154,9 +1153,9 @@ def list_unsubscribe(*args) # @note Lists are limited to having 500 members, and you are limited to adding up to 100 members to a list at a time with this method. # @rate_limited No # @requires_authentication Yes - # @overload list_add_members(list, users_to_add, options={}) + # @overload list_add_members(list, users, options={}) # @param list [Integer, String] The list_id or slug of the list. - # @param users_to_add [Array] The user IDs and/or screen names to add. + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. # @param options [Hash] A customizable set of options. # @return [Twitter::List] The list. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -1165,10 +1164,10 @@ def list_unsubscribe(*args) # Twitter.list_add_members('presidents', [813286, 18755393]) # Twitter.list_add_members(8863586, ['BarackObama', 'pengwynn']) # Twitter.list_add_members(8863586, [813286, 18755393]) - # @overload list_add_members(user, list, users_to_add, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @overload list_add_members(user, list, users, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug of the list. - # @param users_to_add [Array] The user IDs and/or screen names to add. + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. # @param options [Hash] A customizable set of options. # @return [Twitter::List] The list. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -1181,8 +1180,8 @@ def list_unsubscribe(*args) # Twitter.list_add_members(7505382, 8863586, [813286, 18755393]) def list_add_members(*args) options = args.last.is_a?(Hash) ? args.pop : {} - users_to_add = args.pop - options.merge_users!(Array(users_to_add)) + users = args.pop + options.merge_users!(Array(users)) list = args.pop options.merge_list!(list) owner = args.pop || self.current_user.screen_name @@ -1196,9 +1195,9 @@ def list_add_members(*args) # @see https://dev.twitter.com/docs/api/1/post/lists/members/destroy_all # @rate_limited No # @requires_authentication Yes - # @overload list_remove_members(list, users_to_remove, options={}) + # @overload list_remove_members(list, users, options={}) # @param list [Integer, String] The list_id or slug of the list. - # @param users_to_remove [Array] The user IDs and/or screen names to remove. + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. # @param options [Hash] A customizable set of options. # @return [Twitter::List] The list. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -1207,10 +1206,10 @@ def list_add_members(*args) # Twitter.list_remove_members('presidents', [813286, 18755393]) # Twitter.list_remove_members(8863586, ['BarackObama', 'pengwynn']) # Twitter.list_remove_members(8863586, [813286, 18755393]) - # @overload list_remove_members(user, list, users_to_remove, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @overload list_remove_members(user, list, users, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug of the list. - # @param users_to_remove [Array] The user IDs and/or screen names to remove. + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. # @param options [Hash] A customizable set of options. # @return [Twitter::List] The list. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -1223,8 +1222,8 @@ def list_add_members(*args) # Twitter.list_remove_members(7505382, 8863586, [813286, 18755393]) def list_remove_members(*args) options = args.last.is_a?(Hash) ? args.pop : {} - users_to_remove = args.pop - options.merge_users!(Array(users_to_remove)) + users = args.pop + options.merge_users!(Array(users)) list = args.pop options.merge_list!(list) owner = args.pop || self.current_user.screen_name @@ -1248,7 +1247,7 @@ def list_remove_members(*args) # Twitter.list_member?('presidents', 813286) # Twitter.list_member?(8863586, 'BarackObama') # @overload list_member?(user, list, user_to_check, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug of the list. # @param user_to_check [Integer, String] The user ID or screen name of the list member. # @param options [Hash] A customizable set of options. @@ -1287,7 +1286,7 @@ def list_member?(*args) # Twitter.list_members('presidents') # Twitter.list_members(8863586) # @overload list_members(user, list, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug of the list. # @param options [Hash] A customizable set of options. # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. @@ -1324,7 +1323,7 @@ def list_members(*args) # Twitter.list_add_member('presidents', 813286) # Twitter.list_add_member(8863586, 813286) # @overload list_add_member(user, list, user_to_add, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug of the list. # @param user_to_add [Integer, String] The user id or screen name to add to the list. # @param options [Hash] A customizable set of options. @@ -1362,7 +1361,7 @@ def list_add_member(*args) # Twitter.list_destroy('presidents') # Twitter.list_destroy(8863586) # @overload list_destroy(user, list, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug of the list. # @param options [Hash] A customizable set of options. # @return [Twitter::List] The deleted list. @@ -1398,7 +1397,7 @@ def list_destroy(*args) # Twitter.list_update('presidents', :description => "Presidents of the United States of America") # Twitter.list_update(8863586, :description => "Presidents of the United States of America") # @overload list_update(user, list, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug for the list. # @param options [Hash] A customizable set of options. # @option options [String] :mode ('public') Whether your list is public or private. Values can be 'public' or 'private'. @@ -1453,7 +1452,7 @@ def list_create(name, options={}) # @example List the authenticated user's lists # Twitter.lists # @overload lists(user, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. # @return [Twitter::Cursor] @@ -1484,7 +1483,7 @@ def lists(*args) # Twitter.list('presidents') # Twitter.list(8863586) # @overload list(user, list, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param list [Integer, String] The list_id or slug of the list. # @param options [Hash] A customizable set of options. # @return [Twitter::List] The specified list. @@ -1544,7 +1543,7 @@ def trend_locations(options={}) # @see https://dev.twitter.com/docs/api/1/post/notifications/follow # @rate_limited No # @requires_authentication Yes - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @return [Twitter::User] The specified user. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -1562,7 +1561,7 @@ def enable_notifications(user, options={}) # @see https://dev.twitter.com/docs/api/1/post/notifications/leave # @rate_limited No # @requires_authentication Yes - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @return [Twitter::User] The specified user. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. @@ -1759,7 +1758,7 @@ def saved_search_destroy(id, options={}) # @option options [String] :until Optional. Returns tweets generated before the given date. Date should be formatted as YYYY-MM-DD. # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. There are limits to the number of Tweets which can be accessed through the API. If the limit of Tweets has occured since the since_id, the since_id will be forced to the oldest ID available. # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. - # @option options [Boolean, String, Integer] :with_twitter_user_id When set to either true, t or 1, the from_user_id, from_user_id_str, to_user_id, and to_user_id_str values in the response will map to "official" user IDs which will match those returned by the REST API. + # @option options [Boolean, String, Integer] :with_twitter_user_id When set to either true, t or 1, the from_user_id and to_user_id values in the response will map to "official" user IDs which will match those returned by the REST API. # @return [Array] Return tweets that match a specified query # @example Returns tweets related to twitter # Twitter.search('twitter') @@ -1796,7 +1795,7 @@ def phoenix_search(q, options={}) # @see https://dev.twitter.com/docs/api/1/post/report_spam # @rate_limited Yes # @requires_authentication No - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @return [Twitter::User] The reported user. # @example Report @spam for spam @@ -1917,7 +1916,7 @@ def mentions(options={}) # @example Return the 20 most recent retweets posted by the authenticating user # Twitter.retweeted_by('sferik') # @overload retweeted_by(user, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. @@ -1956,7 +1955,7 @@ def retweeted_by(*args) # @example Return the 20 most recent retweets posted by users followed by the authenticating user # Twitter.retweeted_to # @overload retweeted_to(user, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. @@ -2005,7 +2004,7 @@ def retweets_of_me(options={}) # @rate_limited Yes # @requires_authentication No unless the user whose timeline you're trying to view is protected # @overload user_timeline(user, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. @@ -2033,7 +2032,7 @@ def user_timeline(*args) # @rate_limited Yes # @requires_authentication No unless the user whose timeline you're trying to view is protected # @overload media_timeline(user, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. # @option options [Boolean] :filter Include possibly sensitive media when set to false, 'f' or 0. @@ -2294,7 +2293,7 @@ def update_with_media(status, image, options={}) # @rate_limited Yes # @requires_authentication Yes # @overload users(*users, options={}) - # @param users [Array, Set] Twitter user IDs or screen names. + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. # @param options [Hash] A customizable set of options. # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. # @return [Array] The requested users. @@ -2316,8 +2315,13 @@ def users(*args) # @see https://dev.twitter.com/docs/api/1/get/users/profile_image/:screen_name # @rate_limited No # @requires_authentication No + # @overload profile_image(options={}) + # @param options [Hash] A customizable set of options. + # @option options [String] :size ('normal') Specifies the size of image to fetch. Valid options include: 'bigger' (73px by 73px), 'normal' (48px by 48px), and 'mini' (24px by 24px). + # @example Return the URL for the 24px by 24px version of @sferik's profile image + # Twitter.profile_image(:size => 'mini') # @overload profile_image(screen_name, options={}) - # @param screen_name [String] The screen name of the user for whom to return results for. + # @param user [String, Twitter::User] A Twitter screen name or object. # @param options [Hash] A customizable set of options. # @option options [String] :size ('normal') Specifies the size of image to fetch. Valid options include: 'bigger' (73px by 73px), 'normal' (48px by 48px), and 'mini' (24px by 24px). # @example Return the URL for the 24px by 24px version of @sferik's profile image @@ -2325,8 +2329,9 @@ def users(*args) # @return [String] The URL for the requested user's profile image. def profile_image(*args) options = args.last.is_a?(Hash) ? args.pop : {} - screen_name = args.pop || self.current_user.screen_name - get("/1/users/profile_image/#{screen_name}", options, :raw => true).headers['location'] + user = args.pop || self.current_user.screen_name + user = user.screen_name if user.is_a?(Twitter::User) + get("/1/users/profile_image/#{user}", options, :raw => true).headers['location'] end # Returns users that match the given query @@ -2354,7 +2359,7 @@ def user_search(query, options={}) # @rate_limited Yes # @requires_authentication No # @overload user(user, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @return [Twitter::User] The requested user. # @example Return extended information for @sferik @@ -2373,7 +2378,7 @@ def user(*args) # Returns true if the specified user exists # - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @return [Boolean] true if the user exists, otherwise false. # @example Return true if @sferik exists # Twitter.user?('sferik') @@ -2401,8 +2406,8 @@ def user?(user, options={}) # @return [Array] # @example Return the authenticated user's contributees # Twitter.contributees - ## @overload contributees(user, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @overload contributees(user, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Boolean, String, Integer] :skip_status Do not include contributee's statuses when set to true, 't' or 1. # @return [Array] @@ -2432,8 +2437,8 @@ def contributees(*args) # @return [Array] # @example Return the authenticated user's contributors # Twitter.contributors - ## @overload contributors(user, options={}) - # @param user [Integer, String] A Twitter user ID or screen name. + # @overload contributors(user, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Boolean, String, Integer] :skip_status Do not include contributee's statuses when set to true, 't' or 1. # @return [Array] @@ -2455,15 +2460,22 @@ def contributors(*args) # @note {https://dev.twitter.com/discussions/1120 Undocumented} # @rate_limited Yes # @requires_authentication Yes - # @param options [Hash] A customizable set of options. - # @option options [Integer] :limit (20) Specifies the number of records to retrieve. - # @option options [String] :excluded Comma-separated list of user IDs to exclude. - # @option options [String] :screen_name Find users similar to this screen_name - # @option options [Integer] :user_id Find users similar to this user ID. + # @overload recommendations(options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :limit (20) Specifies the number of records to retrieve. + # @option options [String] :excluded Comma-separated list of user IDs to exclude. + # @example Return recommended users for the authenticated user + # Twitter.recommendations + # @overload recommendations(user, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :limit (20) Specifies the number of records to retrieve. + # @option options [String] :excluded Comma-separated list of user IDs to exclude. + # @example Return recommended users for the authenticated user + # Twitter.recommendations("sferik") # @return [Array] # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. - # @example Return recommended users for the authenticated user - # Twitter.recommendations def recommendations(*args) options = {} options.merge!(args.last.is_a?(Hash) ? args.pop : {}) @@ -2491,7 +2503,7 @@ def recommendations(*args) # @overload following_followers_of(user, options={}) # Returns users following followers of the authenticated user # - # @param user [Integer, String] A Twitter user ID or screen name. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. # @param options [Hash] A customizable set of options. # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. # @return [Twitter::Cursor] diff --git a/lib/twitter/core_ext/hash.rb b/lib/twitter/core_ext/hash.rb index a72af810b..9de7dbc5b 100644 --- a/lib/twitter/core_ext/hash.rb +++ b/lib/twitter/core_ext/hash.rb @@ -18,30 +18,42 @@ def deep_merge(hash) # Take a single user ID or screen name and merge it into self with the correct key # - # @param user_id_or_screen_name [Integer, String] A Twitter user ID or screen_name. + # @param user[Integer, String, Twitter::User] A Twitter user ID, screen_name, or object. # @return [Hash] - def merge_user!(user_id_or_screen_name) - case user_id_or_screen_name + def merge_user!(user, prefix=nil, suffix=nil) + case user when Integer - self[:user_id] = user_id_or_screen_name + self[[prefix, "user_id", suffix].compact.join("_").to_sym] = user when String - self[:screen_name] = user_id_or_screen_name + self[[prefix, "screen_name", suffix].compact.join("_").to_sym] = user + when Twitter::User + if user.id + self[[prefix, "user_id", suffix].compact.join("_").to_sym] = user.id + elsif user.screen_name + self[[prefix, "screen_name", suffix].compact.join("_").to_sym] = user.screen_name + end end self end # Take a multiple user IDs and screen names and merge them into self with the correct keys # - # @param users_id_or_screen_names [Array] An array of Twitter user IDs or screen_names. + # @param users [Array, Set] An array of Twitter user IDs, screen_names, or objects. # @return [Hash] - def merge_users!(user_ids_or_screen_names) + def merge_users!(*users) user_ids, screen_names = [], [] - user_ids_or_screen_names.flatten.each do |user_id_or_screen_name| - case user_id_or_screen_name + users.flatten.each do |user| + case user when Integer - user_ids << user_id_or_screen_name + user_ids << user when String - screen_names << user_id_or_screen_name + screen_names << user + when Twitter::User + if user.id + user_ids << user.id + elsif user.screen_name + screen_names << user.screen_name + end end end self[:user_id] = user_ids.join(',') unless user_ids.empty? @@ -52,15 +64,11 @@ def merge_users!(user_ids_or_screen_names) # Take a single owner ID or owner screen name and merge it into self with the correct key # (for Twitter API endpoints that want :owner_id and :owner_screen_name) # - # @param owner_id_or_owner_screen_name [Integer, String] A Twitter user ID or screen_name. + # @param user[Integer, String, Twitter::User] A Twitter user ID, screen_name, or object. # @return [Hash] - def merge_owner!(owner_id_or_owner_screen_name) - case owner_id_or_owner_screen_name - when Integer - self[:owner_id] = owner_id_or_owner_screen_name - when String - self[:owner_screen_name] = owner_id_or_owner_screen_name - end + def merge_owner!(user) + self.merge_user!(user, "owner") + self[:owner_id] = self.delete(:owner_user_id) unless self[:owner_user_id].nil? self end diff --git a/spec/twitter/client/friends_and_followers_spec.rb b/spec/twitter/client/friends_and_followers_spec.rb index 8fa6811d5..22b95e8d0 100644 --- a/spec/twitter/client/friends_and_followers_spec.rb +++ b/spec/twitter/client/friends_and_followers_spec.rb @@ -89,27 +89,74 @@ end describe ".friendship?" do - before do - stub_get("/1/friendships/exists.json"). - with(:query => {:user_a => "sferik", :user_b => "pengwynn"}). - to_return(:body => fixture("true.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - stub_get("/1/friendships/exists.json"). - with(:query => {:user_a => "pengwynn", :user_b => "sferik"}). - to_return(:body => fixture("false.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.friendship?("sferik", "pengwynn") - a_get("/1/friendships/exists.json"). - with(:query => {:user_a => "sferik", :user_b => "pengwynn"}). - should have_been_made + context "with screen names passed" do + before do + stub_get("/1/friendships/exists.json"). + with(:query => {:screen_name_a => "sferik", :screen_name_b => "pengwynn"}). + to_return(:body => fixture("true.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + stub_get("/1/friendships/exists.json"). + with(:query => {:screen_name_a => "pengwynn", :screen_name_b => "sferik"}). + to_return(:body => fixture("false.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + @client.friendship?("sferik", "pengwynn") + a_get("/1/friendships/exists.json"). + with(:query => {:screen_name_a => "sferik", :screen_name_b => "pengwynn"}). + should have_been_made + end + it "should return true if user A follows user B" do + friendship = @client.friendship?("sferik", "pengwynn") + friendship.should be_true + end + it "should return false if user A does not follow user B" do + friendship = @client.friendship?("pengwynn", "sferik") + friendship.should be_false + end end - it "should return true if user_a follows user_b" do - friendship = @client.friendship?("sferik", "pengwynn") - friendship.should be_true + context "with user IDs passed" do + before do + stub_get("/1/friendships/exists.json"). + with(:query => {:user_id_a => "7505382", :user_id_b => "14100886"}). + to_return(:body => fixture("true.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + @client.friendship?(7505382, 14100886) + a_get("/1/friendships/exists.json"). + with(:query => {:user_id_a => "7505382", :user_id_b => "14100886"}). + should have_been_made + end end - it "should return false if user_a does not follows user_b" do - friendship = @client.friendship?("pengwynn", "sferik") - friendship.should be_false + context "with user objects passed" do + context "with screen names" do + before do + stub_get("/1/friendships/exists.json"). + with(:query => {:screen_name_a => "sferik", :screen_name_b => "pengwynn"}). + to_return(:body => fixture("true.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + user1 = Twitter::User.new('screen_name' => 'sferik') + user2 = Twitter::User.new('screen_name' => 'pengwynn') + @client.friendship?(user1, user2) + a_get("/1/friendships/exists.json"). + with(:query => {:screen_name_a => "sferik", :screen_name_b => "pengwynn"}). + should have_been_made + end + end + context "with user IDs" do + before do + stub_get("/1/friendships/exists.json"). + with(:query => {:user_id_a => "7505382", :user_id_b => "14100886"}). + to_return(:body => fixture("true.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + user1 = Twitter::User.new('id' => '7505382') + user2 = Twitter::User.new('id' => '14100886') + @client.friendship?(user1, user2) + a_get("/1/friendships/exists.json"). + with(:query => {:user_id_a => "7505382", :user_id_b => "14100886"}). + should have_been_made + end + end end end @@ -198,6 +245,38 @@ should have_been_made end end + context "with user objects passed" do + context "with screen names" do + before do + stub_get("/1/friendships/show.json"). + with(:query => {:source_screen_name => "sferik", :target_screen_name => "pengwynn"}). + to_return(:body => fixture("relationship.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + user1 = Twitter::User.new('screen_name' => 'sferik') + user2 = Twitter::User.new('screen_name' => 'pengwynn') + @client.friendship(user1, user2) + a_get("/1/friendships/show.json"). + with(:query => {:source_screen_name => "sferik", :target_screen_name => "pengwynn"}). + should have_been_made + end + end + context "with user IDs" do + before do + stub_get("/1/friendships/show.json"). + with(:query => {:source_id => "7505382", :target_id => "14100886"}). + to_return(:body => fixture("relationship.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + user1 = Twitter::User.new('id' => '7505382') + user2 = Twitter::User.new('id' => '14100886') + @client.friendship(user1, user2) + a_get("/1/friendships/show.json"). + with(:query => {:source_id => "7505382", :target_id => "14100886"}). + should have_been_made + end + end + end end describe ".follow" do diff --git a/spec/twitter/client/lists_spec.rb b/spec/twitter/client/lists_spec.rb index 094b5707a..e7406ec7b 100644 --- a/spec/twitter/client/lists_spec.rb +++ b/spec/twitter/client/lists_spec.rb @@ -76,32 +76,6 @@ should have_been_made end end - context "with an Integer list_id passed" do - before do - stub_get("/1/lists/statuses.json"). - with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678'}). - to_return(:body => fixture("statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_timeline("sferik", 12345678) - a_get("/1/lists/statuses.json"). - with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678'}). - should have_been_made - end - end - context "with an Integer user_id passed" do - before do - stub_get("/1/lists/statuses.json"). - with(:query => {:owner_id => '12345678', :slug => 'presidents'}). - to_return(:body => fixture("statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_timeline(12345678, 'presidents') - a_get("/1/lists/statuses.json"). - with(:query => {:owner_id => '12345678', :slug => 'presidents'}). - should have_been_made - end - end end describe ".list_remove_member" do @@ -123,45 +97,6 @@ list.name.should == "presidents" end end - context "with an Integer user_id passed" do - before do - stub_post("/1/lists/members/destroy.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents', :user_id => "813286"}). - to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_remove_member(12345678, "presidents", 813286) - a_post("/1/lists/members/destroy.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents', :user_id => "813286"}). - should have_been_made - end - end - context "with an Integer list_id passed" do - before do - stub_post("/1/lists/members/destroy.json"). - with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => "813286"}). - to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_remove_member('sferik', 12345678, 813286) - a_post("/1/lists/members/destroy.json"). - with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => "813286"}). - should have_been_made - end - end - context "with a screen name to remove" do - before do - stub_post("/1/lists/members/destroy.json"). - with(:body => {:owner_screen_name => 'sferik', :slug => 'presidents', :screen_name => "erebor"}). - to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_remove_member('sferik', 'presidents', 'erebor') - a_post("/1/lists/members/destroy.json"). - with(:body => {:owner_screen_name => 'sferik', :slug => 'presidents', :screen_name => "erebor"}). - should have_been_made - end - end context "without a screen name passed" do before do stub_get("/1/account/verify_credentials.json"). @@ -215,19 +150,6 @@ should have_been_made end end - context "with an Integer user_id passed" do - before do - stub_get("/1/lists/memberships.json"). - with(:query => {:user_id => '12345678', :cursor => "-1"}). - to_return(:body => fixture("lists.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.memberships(12345678) - a_get("/1/lists/memberships.json"). - with(:query => {:user_id => '12345678', :cursor => "-1"}). - should have_been_made - end - end end describe ".list_subscribers" do @@ -251,32 +173,6 @@ list_subscribers.users.first.name.should == "Erik Michaels-Ober" end end - context "with an Integer user passed" do - before do - stub_get("/1/lists/subscribers.json"). - with(:query => {:owner_id => '12345678', :slug => 'presidents', :cursor => "-1"}). - to_return(:body => fixture("users_list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_subscribers(12345678, "presidents") - a_get("/1/lists/subscribers.json"). - with(:query => {:owner_id => '12345678', :slug => 'presidents', :cursor => "-1"}). - should have_been_made - end - end - context "with an Integer list_id passed" do - before do - stub_get("/1/lists/subscribers.json"). - with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678', :cursor => "-1"}). - to_return(:body => fixture("users_list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_subscribers('sferik', 12345678) - a_get("/1/lists/subscribers.json"). - with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678', :cursor => "-1"}). - should have_been_made - end - end context "without a screen name passed" do before do stub_get("/1/account/verify_credentials.json"). @@ -330,19 +226,6 @@ should have_been_made end end - context "with an Integer user_id passed" do - before do - stub_get("/1/lists/subscriptions.json"). - with(:query => {:user_id => '12345678', :cursor => "-1"}). - to_return(:body => fixture("lists.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.subscriptions(12345678) - a_get("/1/lists/subscriptions.json"). - with(:query => {:user_id => '12345678', :cursor => "-1"}). - should have_been_made - end - end end describe ".list_subscribe" do @@ -364,32 +247,6 @@ list.name.should == "presidents" end end - context "with an Integer user_id passed" do - before do - stub_post("/1/lists/subscribers/create.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents'}). - to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_subscribe(12345678, "presidents") - a_post("/1/lists/subscribers/create.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents'}). - should have_been_made - end - end - context "with an Integer list_id passed" do - before do - stub_post("/1/lists/subscribers/create.json"). - with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678'}). - to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_subscribe('sferik', 12345678) - a_post("/1/lists/subscribers/create.json"). - with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678'}). - should have_been_made - end - end context "without a screen name passed" do before do stub_get("/1/account/verify_credentials.json"). @@ -439,7 +296,7 @@ list_subscriber.should be_false end end - context "with an Integer owner_id passed" do + context "with a owner ID passed" do before do stub_get("/1/lists/subscribers/show.json"). with(:query => {:owner_id => '12345678', :slug => 'presidents', :user_id => '813286'}). @@ -452,7 +309,7 @@ should have_been_made end end - context "with an Integer list_id passed" do + context "with a list ID passed" do before do stub_get("/1/lists/subscribers/show.json"). with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => '813286'}). @@ -514,32 +371,6 @@ list.name.should == "presidents" end end - context "with an Integer user_id passed" do - before do - stub_post("/1/lists/subscribers/destroy.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents'}). - to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_unsubscribe(12345678, "presidents") - a_post("/1/lists/subscribers/destroy.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents'}). - should have_been_made - end - end - context "with an Integer list_id passed" do - before do - stub_post("/1/lists/subscribers/destroy.json"). - with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678'}). - to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_unsubscribe('sferik', 12345678) - a_post("/1/lists/subscribers/destroy.json"). - with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678'}). - should have_been_made - end - end context "without a screen name passed" do before do stub_get("/1/account/verify_credentials.json"). @@ -576,32 +407,6 @@ list.name.should == "presidents" end end - context "with an Integer user_id passed" do - before do - stub_post("/1/lists/members/create_all.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents', :user_id => "813286,18755393"}). - to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_add_members(12345678, "presidents", [813286, 18755393]) - a_post("/1/lists/members/create_all.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents', :user_id => "813286,18755393"}). - should have_been_made - end - end - context "with an Integer list_id passed" do - before do - stub_post("/1/lists/members/create_all.json"). - with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => "813286,18755393"}). - to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_add_members('sferik', 12345678, [813286, 18755393]) - a_post("/1/lists/members/create_all.json"). - with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => "813286,18755393"}). - should have_been_made - end - end context "with a combination of member IDs and member screen names to add" do before do stub_post("/1/lists/members/create_all.json"). @@ -651,32 +456,6 @@ list.name.should == "presidents" end end - context "with an Integer user_id passed" do - before do - stub_post("/1/lists/members/destroy_all.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents', :user_id => "813286,18755393"}). - to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_remove_members(12345678, "presidents", [813286, 18755393]) - a_post("/1/lists/members/destroy_all.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents', :user_id => "813286,18755393"}). - should have_been_made - end - end - context "with an Integer list_id passed" do - before do - stub_post("/1/lists/members/destroy_all.json"). - with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => "813286,18755393"}). - to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_remove_members('sferik', 12345678, [813286, 18755393]) - a_post("/1/lists/members/destroy_all.json"). - with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => "813286,18755393"}). - should have_been_made - end - end context "with a combination of member IDs and member screen names to add" do before do stub_post("/1/lists/members/destroy_all.json"). @@ -739,7 +518,7 @@ list_member.should be_false end end - context "with an Integer owner_id passed" do + context "with an owner ID passed" do before do stub_get("/1/lists/members/show.json"). with(:query => {:owner_id => '12345678', :slug => 'presidents', :user_id => '813286'}). @@ -752,7 +531,7 @@ should have_been_made end end - context "with an Integer list_id passed" do + context "with a list ID passed" do before do stub_get("/1/lists/members/show.json"). with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => '813286'}). @@ -816,19 +595,6 @@ list_members.users.first.name.should == "Erik Michaels-Ober" end end - context "with an Integer user_id passed" do - before do - stub_get("/1/lists/members.json"). - with(:query => {:owner_id => '12345678', :slug => 'presidents', :cursor => "-1"}). - to_return(:body => fixture("users_list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_members(12345678, 'presidents') - a_get("/1/lists/members.json"). - with(:query => {:owner_id => '12345678', :slug => 'presidents', :cursor => "-1"}). - should have_been_made - end - end context "without a screen name passed" do before do stub_get("/1/account/verify_credentials.json"). @@ -865,32 +631,6 @@ list.name.should == "presidents" end end - context "with an Integer user_id passed" do - before do - stub_post("/1/lists/members/create.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents', :user_id => "813286"}). - to_return(:body => fixture("users_list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_add_member(12345678, 'presidents', 813286) - a_post("/1/lists/members/create.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents', :user_id => "813286"}). - should have_been_made - end - end - context "with an Integer list_id passed" do - before do - stub_post("/1/lists/members/create.json"). - with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => "813286"}). - to_return(:body => fixture("users_list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_add_member('sferik', 12345678, 813286) - a_post("/1/lists/members/create.json"). - with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => "813286"}). - should have_been_made - end - end context "without a screen name passed" do before do stub_get("/1/account/verify_credentials.json"). @@ -942,7 +682,7 @@ should have_been_made end end - context "with an Integer list_id passed" do + context "with a list ID passed" do before do stub_delete("/1/lists/destroy.json"). with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678'}). @@ -955,19 +695,6 @@ should have_been_made end end - context "with an Integer user_id passed" do - before do - stub_delete("/1/lists/destroy.json"). - with(:query => {:owner_id => '12345678', :slug => 'presidents'}). - to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_destroy(12345678, 'presidents') - a_delete("/1/lists/destroy.json"). - with(:query => {:owner_id => '12345678', :slug => 'presidents'}). - should have_been_made - end - end end describe ".list_update" do @@ -1004,7 +731,7 @@ should have_been_made end end - context "with an Integer list_id passed" do + context "with a list ID passed" do before do stub_post("/1/lists/update.json"). with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678', :description => "Presidents of the United States of America"}). @@ -1017,19 +744,6 @@ should have_been_made end end - context "with an Integer user_id passed" do - before do - stub_post("/1/lists/update.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents', :description => "Presidents of the United States of America"}). - to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.list_update(12345678, 'presidents', :description => "Presidents of the United States of America") - a_post("/1/lists/update.json"). - with(:body => {:owner_id => '12345678', :slug => 'presidents', :description => "Presidents of the United States of America"}). - should have_been_made - end - end end describe ".list_create" do @@ -1072,26 +786,6 @@ lists.lists.first.name.should == "Rubyists" end end - context "with an Integer user id passed" do - before do - stub_get("/1/lists.json"). - with(:query => {:user_id => '12345678', :cursor => "-1"}). - to_return(:body => fixture("lists.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.lists(12345678) - a_get("/1/lists.json"). - with(:query => {:user_id => '12345678', :cursor => "-1"}). - should have_been_made - end - it "should return the updated list" do - lists = @client.lists(12345678) - lists.should be_a Twitter::Cursor - lists.lists.should be_an Array - lists.lists.first.should be_a Twitter::List - lists.lists.first.name.should == "Rubyists" - end - end context "without arguments passed" do before do stub_get("/1/lists.json"). @@ -1133,44 +827,74 @@ list.name.should == "presidents" end end - context "without a screen name passed" do + context "with a user ID passed" do before do - stub_get("/1/account/verify_credentials.json"). - to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"}) stub_get("/1/lists/show.json"). - with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents'}). + with(:query => {:owner_id => '12345678', :slug => 'presidents'}). to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) end it "should request the correct resource" do - @client.list("presidents") + @client.list(12345678, 'presidents') a_get("/1/lists/show.json"). - with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents'}). + with(:query => {:owner_id => '12345678', :slug => 'presidents'}). should have_been_made end end - context "with an Integer list_id passed" do + context "with a user object passed" do + context "with a user ID" do + before do + stub_get("/1/lists/show.json"). + with(:query => {:owner_id => '12345678', :slug => 'presidents'}). + to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + user = Twitter::User.new('id' => '12345678') + @client.list(user, 'presidents') + a_get("/1/lists/show.json"). + with(:query => {:owner_id => '12345678', :slug => 'presidents'}). + should have_been_made + end + end + context "with a screen name" do + before do + stub_get("/1/lists/show.json"). + with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents'}). + to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + user = Twitter::User.new('screen_name' => 'sferik') + @client.list(user, "presidents") + a_get("/1/lists/show.json"). + with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents'}). + should have_been_made + end + end + end + context "without a screen name passed" do before do + stub_get("/1/account/verify_credentials.json"). + to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"}) stub_get("/1/lists/show.json"). - with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678'}). + with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents'}). to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) end it "should request the correct resource" do - @client.list("sferik", 12345678) + @client.list("presidents") a_get("/1/lists/show.json"). - with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678'}). + with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents'}). should have_been_made end end - context "with an Integer user_id passed" do + context "with a list ID passed" do before do stub_get("/1/lists/show.json"). - with(:query => {:owner_id => '12345678', :slug => 'presidents'}). + with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678'}). to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) end it "should request the correct resource" do - @client.list(12345678, 'presidents') + @client.list("sferik", 12345678) a_get("/1/lists/show.json"). - with(:query => {:owner_id => '12345678', :slug => 'presidents'}). + with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678'}). should have_been_made end end diff --git a/spec/twitter/client/users_spec.rb b/spec/twitter/client/users_spec.rb index 90c84877d..d3d97b3f9 100644 --- a/spec/twitter/client/users_spec.rb +++ b/spec/twitter/client/users_spec.rb @@ -65,6 +65,53 @@ should have_been_made end end + context "with user objects passed" do + context "with user IDs" do + before do + stub_get("/1/users/lookup.json"). + with(:query => {:user_id => "7505382,14100886"}). + to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + user1 = Twitter::User.new('id' => '7505382') + user2 = Twitter::User.new('id' => '14100886') + @client.users(user1, user2) + a_get("/1/users/lookup.json"). + with(:query => {:user_id => "7505382,14100886"}). + should have_been_made + end + end + context "with screen_names IDs" do + before do + stub_get("/1/users/lookup.json"). + with(:query => {:screen_name => "sferik,pengwynn"}). + to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + user1 = Twitter::User.new('screen_name' => 'sferik') + user2 = Twitter::User.new('screen_name' => 'pengwynn') + @client.users(user1, user2) + a_get("/1/users/lookup.json"). + with(:query => {:screen_name => "sferik,pengwynn"}). + should have_been_made + end + end + context "with both screen names and user IDs" do + before do + stub_get("/1/users/lookup.json"). + with(:query => {:screen_name => "sferik", :user_id => "14100886"}). + to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + user1 = Twitter::User.new('screen_name' => 'sferik') + user2 = Twitter::User.new('id' => '14100886') + @client.users(user1, user2) + a_get("/1/users/lookup.json"). + with(:query => {:screen_name => "sferik", :user_id => "14100886"}). + should have_been_made + end + end + end end describe ".profile_image" do @@ -96,6 +143,20 @@ profile_image.should == "http://a0.twimg.com/profile_images/323331048/me_normal.jpg" end end + context "with a user object passed" do + before do + stub_get("/1/users/profile_image/sferik"). + to_return(fixture("profile_image.text")) + end + it "should redirect to the correct resource" do + user = Twitter::User.new('screen_name' => 'sferik') + profile_image = @client.profile_image(user) + a_get("/1/users/profile_image/sferik"). + with(:status => 302). + should have_been_made + profile_image.should == "http://a0.twimg.com/profile_images/323331048/me_normal.jpg" + end + end end describe ".user_search" do @@ -176,6 +237,36 @@ should have_been_made end end + context "with a user object passed" do + context "with a user ID" do + before do + stub_get("/1/users/show.json"). + with(:query => {:user_id => "7505382"}). + to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + user = Twitter::User.new('id' => 7505382) + @client.user(user) + a_get("/1/users/show.json"). + with(:query => {:user_id => "7505382"}). + should have_been_made + end + end + context "with a screen_name" do + before do + stub_get("/1/users/show.json"). + with(:query => {:screen_name => "sferik"}). + to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + user = Twitter::User.new('screen_name' => 'sferik') + @client.user(user) + a_get("/1/users/show.json"). + with(:query => {:screen_name => "sferik"}). + should have_been_made + end + end + end context "without a screen name or user ID passed" do before do stub_get("/1/account/verify_credentials.json").