Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rename utility methods

  • Loading branch information...
commit 541d0a1fe8d3facbf977a136f375d2f6b44fbcf9 1 parent ecf657b
@sferik authored
View
4 lib/twitter/api/direct_messages.rb
@@ -23,7 +23,7 @@ module DirectMessages
# @example Return the 20 most recent direct messages sent to the authenticating user
# Twitter.direct_messages_received
def direct_messages_received(options={})
- collection_from_response(Twitter::DirectMessage, :get, "/1.1/direct_messages.json", options)
+ objects_from_response(Twitter::DirectMessage, :get, "/1.1/direct_messages.json", options)
end
# Returns the 20 most recent direct messages sent by the authenticating user
@@ -42,7 +42,7 @@ def direct_messages_received(options={})
# @example Return the 20 most recent direct messages sent by the authenticating user
# Twitter.direct_messages_sent
def direct_messages_sent(options={})
- collection_from_response(Twitter::DirectMessage, :get, "/1.1/direct_messages/sent.json", options)
+ objects_from_response(Twitter::DirectMessage, :get, "/1.1/direct_messages/sent.json", options)
end
# Returns a direct message
View
2  lib/twitter/api/favorites.rb
@@ -36,7 +36,7 @@ def favorites(*args)
if user = args.pop
merge_user!(options, user)
end
- collection_from_response(Twitter::Tweet, :get, "/1.1/favorites/list.json", options)
+ objects_from_response(Twitter::Tweet, :get, "/1.1/favorites/list.json", options)
end
alias favourites favorites
View
12 lib/twitter/api/friends_and_followers.rb
@@ -31,7 +31,7 @@ module FriendsAndFollowers
# Twitter.friend_ids('sferik')
# Twitter.friend_ids(7505382) # Same as above
def friend_ids(*args)
- ids_from_response(:get, "/1.1/friends/ids.json", args, :friend_ids)
+ cursor_from_response_with_user(:ids, nil, :get, "/1.1/friends/ids.json", args, :friend_ids)
end
# @see https://dev.twitter.com/docs/api/1.1/get/followers/ids
@@ -56,7 +56,7 @@ def friend_ids(*args)
# Twitter.follower_ids('sferik')
# Twitter.follower_ids(7505382) # Same as above
def follower_ids(*args)
- ids_from_response(:get, "/1.1/followers/ids.json", args, :follower_ids)
+ cursor_from_response_with_user(:ids, nil, :get, "/1.1/followers/ids.json", args, :follower_ids)
end
# Returns the relationship of the authenticating user to the comma separated list of up to 100 screen_names or user_ids provided. Values for connections can be: following, following_requested, followed_by, none.
@@ -78,7 +78,7 @@ def follower_ids(*args)
def friendships(*args)
options = extract_options!(args)
merge_users!(options, Array(args))
- collection_from_response(Twitter::User, :get, "/1.1/friendships/lookup.json", options)
+ objects_from_response(Twitter::User, :get, "/1.1/friendships/lookup.json", options)
end
# Returns an array of numeric IDs for every user who has a pending request to follow the authenticating user
@@ -188,7 +188,7 @@ def follow!(*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 unfollow(*args)
- threaded_users_from_response(:post, "/1.1/friendships/destroy.json", args)
+ threaded_user_objects_from_response(:post, "/1.1/friendships/destroy.json", args)
end
alias friendship_destroy unfollow
@@ -280,7 +280,7 @@ def friendship?(source, target, options={})
# Twitter.followers('sferik')
# Twitter.followers(7505382) # Same as above
def followers(*args)
- cursor_object_from_response(:users, Twitter::User, :get, "/1.1/followers/list.json", args, :followers)
+ cursor_from_response_with_user(:users, Twitter::User, :get, "/1.1/followers/list.json", args, :followers)
end
# Returns a cursored collection of user objects for every user the specified user is following (otherwise known as their "friends").
@@ -311,7 +311,7 @@ def followers(*args)
# Twitter.friends('sferik')
# Twitter.friends(7505382) # Same as above
def friends(*args)
- cursor_object_from_response(:users, Twitter::User, :get, "/1.1/friends/list.json", args, :friends)
+ cursor_from_response_with_user(:users, Twitter::User, :get, "/1.1/friends/list.json", args, :friends)
end
alias following friends
View
2  lib/twitter/api/help.rb
@@ -30,7 +30,7 @@ def configuration(options={})
# @example Return the list of languages Twitter supports
# Twitter.languages
def languages(options={})
- collection_from_response(Twitter::Language, :get, "/1.1/help/languages.json", options)
+ objects_from_response(Twitter::Language, :get, "/1.1/help/languages.json", options)
end
# Returns {https://twitter.com/privacy Twitter's Privacy Policy}
View
26 lib/twitter/api/lists.rb
@@ -30,7 +30,7 @@ module Lists
# Twitter.lists('sferik')
# Twitter.lists(7505382)
def lists(*args)
- objects_from_response(Twitter::List, :get, "/1.1/lists/list.json", args)
+ objects_from_response_with_user(Twitter::List, :get, "/1.1/lists/list.json", args)
end
alias lists_subscribed_to lists
@@ -66,7 +66,7 @@ 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]
- collection_from_response(Twitter::Tweet, :get, "/1.1/lists/statuses.json", options)
+ objects_from_response(Twitter::Tweet, :get, "/1.1/lists/statuses.json", options)
end
# Removes the specified member from the list
@@ -95,7 +95,7 @@ def list_timeline(*args)
# Twitter.list_remove_member('sferik', 8863586, 'BarackObama')
# Twitter.list_remove_member(7505382, 'presidents', 813286)
def list_remove_member(*args)
- list_modify_member(:post, "/1.1/lists/members/destroy.json", args)
+ list_from_response_with_user(:post, "/1.1/lists/members/destroy.json", args)
end
# List the lists the specified user has been added to
@@ -120,7 +120,7 @@ def list_remove_member(*args)
# Twitter.memberships('sferik')
# Twitter.memberships(7505382)
def memberships(*args)
- cursor_object_from_response(:lists, Twitter::List, :get, "/1.1/lists/memberships.json", args, :memberships)
+ cursor_from_response_with_user(:lists, Twitter::List, :get, "/1.1/lists/memberships.json", args, :memberships)
end
# Returns the subscribers of the specified list
@@ -147,7 +147,7 @@ def memberships(*args)
# Twitter.list_subscribers('sferik', 8863586)
# Twitter.list_subscribers(7505382, 'presidents')
def list_subscribers(*args)
- list_users(:get, "/1.1/lists/subscribers.json", args, :list_subscribers)
+ cursor_from_response_with_list(:get, "/1.1/lists/subscribers.json", args, :list_subscribers)
end
# Make the authenticated user follow the specified list
@@ -260,7 +260,7 @@ def list_unsubscribe(*args)
# Twitter.list_add_members(7505382, 8863586, ['BarackObama', 'pengwynn'])
# Twitter.list_add_members(7505382, 8863586, [813286, 18755393])
def list_add_members(*args)
- list_modify_members(:post, "/1.1/lists/members/create_all.json", args)
+ list_from_response_with_users(:post, "/1.1/lists/members/create_all.json", args)
end
# Check if a user is a member of the specified list
@@ -315,7 +315,7 @@ def list_member?(*args)
# Twitter.list_members(7505382, 'presidents')
# Twitter.list_members(7505382, 8863586)
def list_members(*args)
- list_users(:get, "/1.1/lists/members.json", args, :list_members)
+ cursor_from_response_with_list(:get, "/1.1/lists/members.json", args, :list_members)
end
# Add a member to a list
@@ -344,7 +344,7 @@ def list_members(*args)
# Twitter.list_add_member(7505382, 'presidents', 813286)
# Twitter.list_add_member(7505382, 8863586, 813286)
def list_add_member(*args)
- list_modify_member(:post, "/1.1/lists/members/create.json", args)
+ list_from_response_with_user(:post, "/1.1/lists/members/create.json", args)
end
# Deletes the specified list
@@ -469,7 +469,7 @@ def list(*args)
# Twitter.subscriptions('sferik')
# Twitter.subscriptions(7505382)
def subscriptions(*args)
- cursor_object_from_response(:lists, Twitter::List, :get, "/1.1/lists/subscriptions.json", args, :subscriptions)
+ cursor_from_response_with_user(:lists, Twitter::List, :get, "/1.1/lists/subscriptions.json", args, :subscriptions)
end
# Removes specified members from the list
@@ -501,7 +501,7 @@ def subscriptions(*args)
# Twitter.list_remove_members(7505382, 8863586, ['BarackObama', 'pengwynn'])
# Twitter.list_remove_members(7505382, 8863586, [813286, 18755393])
def list_remove_members(*args)
- list_modify_members(:post, "/1.1/lists/members/destroy_all.json", args)
+ list_from_response_with_users(:post, "/1.1/lists/members/destroy_all.json", args)
end
private
@@ -517,7 +517,7 @@ def list_from_response(request_method, path, args)
object_from_response(Twitter::List, request_method, path, options)
end
- def list_users(request_method, path, args, calling_method)
+ 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]
@@ -535,7 +535,7 @@ def list_user?(request_method, path, args)
false
end
- def list_modify_member(request_method, path, args)
+ def list_from_response_with_user(request_method, path, args)
options = extract_options!(args)
merge_user!(options, args.pop)
merge_list!(options, args.pop)
@@ -543,7 +543,7 @@ def list_modify_member(request_method, path, args)
object_from_response(Twitter::List, request_method, path, options)
end
- def list_modify_members(request_method, path, args)
+ def list_from_response_with_users(request_method, path, args)
options = extract_options!(args)
members = args.pop
merge_list!(options, args.pop)
View
10 lib/twitter/api/places_and_geo.rb
@@ -38,7 +38,7 @@ def place(place_id, options={})
# @example Return an array of places within the specified region
# Twitter.reverse_geocode(:lat => "37.7821120598956", :long => "-122.400612831116")
def reverse_geocode(options={})
- geo_collection_from_response(:get, "/1.1/geo/reverse_geocode.json", options)
+ geo_objects_from_response(:get, "/1.1/geo/reverse_geocode.json", options)
end
# Search for places that can be attached to a {Twitter::API::Tweets#update}
@@ -61,7 +61,7 @@ def reverse_geocode(options={})
# @example Return an array of places near the IP address 74.125.19.104
# Twitter.geo_search(:ip => "74.125.19.104")
def geo_search(options={})
- geo_collection_from_response(:get, "/1.1/geo/search.json", options)
+ geo_objects_from_response(:get, "/1.1/geo/search.json", options)
end
alias places_nearby geo_search
@@ -82,7 +82,7 @@ def geo_search(options={})
# @example Return an array of places similar to Twitter HQ
# Twitter.similar_places(:lat => "37.7821120598956", :long => "-122.400612831116", :name => "Twitter HQ")
def similar_places(options={})
- geo_collection_from_response(:get, "/1.1/geo/similar_places.json", options)
+ geo_objects_from_response(:get, "/1.1/geo/similar_places.json", options)
end
alias places_similar similar_places
@@ -112,8 +112,8 @@ def place_create(options={})
# @param path [String]
# @param params [Hash]
# @return [Array]
- def geo_collection_from_response(request_method, path, params={})
- collection_from_array(Twitter::Place, send(request_method.to_sym, path, params)[:body][:result][:places])
+ def geo_objects_from_response(request_method, path, params={})
+ objects_from_array(Twitter::Place, send(request_method.to_sym, path, params)[:body][:result][:places])
end
end
View
2  lib/twitter/api/saved_searches.rb
@@ -33,7 +33,7 @@ module SavedSearches
def saved_searches(*args)
options = extract_options!(args)
if args.empty?
- collection_from_response(Twitter::SavedSearch, :get, "/1.1/saved_searches/list.json", options)
+ objects_from_response(Twitter::SavedSearch, :get, "/1.1/saved_searches/list.json", options)
else
args.flatten.threaded_map do |id|
saved_search(id)
View
2  lib/twitter/api/spam_reporting.rb
@@ -22,7 +22,7 @@ module SpamReporting
# @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 report_spam(*args)
- threaded_users_from_response(:post, "/1.1/users/report_spam.json", args)
+ threaded_user_objects_from_response(:post, "/1.1/users/report_spam.json", args)
end
end
View
4 lib/twitter/api/suggested_users.rb
@@ -31,7 +31,7 @@ def suggestions(*args)
if slug = args.pop
object_from_response(Twitter::Suggestion, :get, "/1.1/users/suggestions/#{slug}.json", options)
else
- collection_from_response(Twitter::Suggestion, :get, "/1.1/users/suggestions.json", options)
+ objects_from_response(Twitter::Suggestion, :get, "/1.1/users/suggestions.json", options)
end
end
@@ -46,7 +46,7 @@ def suggestions(*args)
# @example Return the users in the Art & Design category and their most recent Tweet if they are not a protected user
# Twitter.suggest_users("art-design")
def suggest_users(slug, options={})
- collection_from_response(Twitter::User, :get, "/1.1/users/suggestions/#{slug}/members.json", options)
+ objects_from_response(Twitter::User, :get, "/1.1/users/suggestions/#{slug}/members.json", options)
end
end
View
8 lib/twitter/api/timelines.rb
@@ -25,7 +25,7 @@ module Timelines
# @example Return the 20 most recent mentions (statuses containing @username) for the authenticating user
# Twitter.mentions
def mentions_timeline(options={})
- collection_from_response(Twitter::Tweet, :get, "/1.1/statuses/mentions_timeline.json", options)
+ objects_from_response(Twitter::Tweet, :get, "/1.1/statuses/mentions_timeline.json", options)
end
alias mentions mentions_timeline
@@ -50,7 +50,7 @@ def mentions_timeline(options={})
# @example Return the 20 most recent Tweets posted by @sferik
# Twitter.user_timeline('sferik')
def user_timeline(*args)
- objects_from_response(Twitter::Tweet, :get, "/1.1/statuses/user_timeline.json", args)
+ objects_from_response_with_user(Twitter::Tweet, :get, "/1.1/statuses/user_timeline.json", args)
end
# Returns the 20 most recent retweets posted by the specified user
@@ -123,7 +123,7 @@ def retweeted_by_me(options={})
# @example Return the 20 most recent Tweets, including retweets if they exist, posted by the authenticating user and the users they follow
# Twitter.home_timeline
def home_timeline(options={})
- collection_from_response(Twitter::Tweet, :get, "/1.1/statuses/home_timeline.json", options)
+ objects_from_response(Twitter::Tweet, :get, "/1.1/statuses/home_timeline.json", options)
end
# Returns the 20 most recent retweets posted by users the authenticating user follow.
@@ -167,7 +167,7 @@ def retweeted_to_me(options={})
# @example Return the 20 most recent tweets of the authenticated user that have been retweeted by others
# Twitter.retweets_of_me
def retweets_of_me(options={})
- collection_from_response(Twitter::Tweet, :get, "/1.1/statuses/retweets_of_me.json", options)
+ objects_from_response(Twitter::Tweet, :get, "/1.1/statuses/retweets_of_me.json", options)
end
private
View
6 lib/twitter/api/trends.rb
@@ -22,7 +22,7 @@ module Trends
def trends(id=1, options={})
options[:id] = id
response = get("/1.1/trends/place.json", options)
- collection_from_array(Twitter::Trend, response[:body].first[:trends])
+ objects_from_array(Twitter::Trend, response[:body].first[:trends])
end
alias local_trends trends
alias trends_place trends
@@ -38,7 +38,7 @@ def trends(id=1, options={})
# @example Return the locations that Twitter has trending topic information for
# Twitter.trends_available
def trends_available(options={})
- collection_from_response(Twitter::Place, :get, "/1.1/trends/available.json", options)
+ objects_from_response(Twitter::Place, :get, "/1.1/trends/available.json", options)
end
alias trend_locations trends_available
@@ -55,7 +55,7 @@ def trends_available(options={})
# @example Return the locations that Twitter has trending topic information for
# Twitter.trends_closest
def trends_closest(options={})
- collection_from_response(Twitter::Place, :get, "/1.1/trends/closest.json", options)
+ objects_from_response(Twitter::Place, :get, "/1.1/trends/closest.json", options)
end
end
View
2  lib/twitter/api/tweets.rb
@@ -23,7 +23,7 @@ module Tweets
# @example Return up to 100 of the first retweets of the Tweet with the ID 28561922516
# Twitter.retweets(28561922516)
def retweets(id, options={})
- collection_from_response(Twitter::Tweet, :get, "/1.1/statuses/retweets/#{id}.json", options)
+ objects_from_response(Twitter::Tweet, :get, "/1.1/statuses/retweets/#{id}.json", options)
end
# Show up to 100 users who retweeted the Tweet
View
8 lib/twitter/api/undocumented.rb
@@ -22,7 +22,7 @@ module Undocumented
# @example Return activity about me
# Twitter.activity_about_me
def activity_about_me(options={})
- collection_from_response(Twitter::ActionFactory, :get, "/i/activity/about_me.json", options)
+ objects_from_response(Twitter::ActionFactory, :get, "/i/activity/about_me.json", options)
end
# Returns activity by friends
@@ -38,7 +38,7 @@ def activity_about_me(options={})
# @example Return activity by friends
# Twitter.activity_by_friends
def activity_by_friends(options={})
- collection_from_response(Twitter::ActionFactory, :get, "/i/activity/by_friends.json", options)
+ objects_from_response(Twitter::ActionFactory, :get, "/i/activity/by_friends.json", options)
end
# @note Undocumented
@@ -66,9 +66,7 @@ def activity_by_friends(options={})
# Twitter.following_followers_of('sferik')
# Twitter.following_followers_of(7505382) # Same as above
def following_followers_of(*args)
- options = extract_options!(args)
- merge_user!(options, args.pop || screen_name) unless options[:user_id] || options[:screen_name]
- cursor_from_response(:users, Twitter::User, :get, "/users/following_followers_of.json", options, :following_followers_of)
+ cursor_from_response_with_user(:users, Twitter::User, :get, "/users/following_followers_of.json", args, :following_followers_of)
end
# Returns activity summary for a Tweet
View
16 lib/twitter/api/users.rb
@@ -162,7 +162,9 @@ def blocking(options={})
# @overload block(options={})
# @param options [Hash] A customizable set of options.
def blocked_ids(*args)
- ids_from_response(:get, "/1.1/blocks/ids.json", args, :blocked_ids)
+ options = extract_options!(args)
+ merge_user!(options, args.pop)
+ cursor_from_response(:ids, nil, :get, "/1.1/blocks/ids.json", options, :blocked_ids)
end
# Returns true if the authenticating user is blocking a target user
@@ -207,7 +209,7 @@ def block?(user, options={})
# @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 block(*args)
- threaded_users_from_response(:post, "/1.1/blocks/create.json", args)
+ threaded_user_objects_from_response(:post, "/1.1/blocks/create.json", args)
end
# Un-blocks the users specified by the authenticating user
@@ -226,7 +228,7 @@ def block(*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 unblock(*args)
- threaded_users_from_response(:post, "/1.1/blocks/destroy.json", args)
+ threaded_user_objects_from_response(:post, "/1.1/blocks/destroy.json", args)
end
# Returns extended information for up to 100 users
@@ -253,7 +255,7 @@ def users(*args)
options = extract_options!(args)
method = options.delete(:method) || :post
args.flatten.each_slice(MAX_USERS_PER_REQUEST).threaded_map do |users|
- collection_from_response(Twitter::User, method, "/1.1/users/lookup.json", merge_users(options, users))
+ objects_from_response(Twitter::User, method, "/1.1/users/lookup.json", merge_users(options, users))
end.flatten
end
@@ -319,7 +321,7 @@ def user?(user, options={})
# @example Return users that match "Erik Michaels-Ober"
# Twitter.user_search("Erik Michaels-Ober")
def user_search(query, options={})
- collection_from_response(Twitter::User, :get, "/1.1/users/search.json", options.merge(:q => query))
+ objects_from_response(Twitter::User, :get, "/1.1/users/search.json", options.merge(:q => query))
end
# Returns an array of users that the specified user can contribute to
@@ -342,7 +344,7 @@ def user_search(query, options={})
# Twitter.contributees('sferik')
# Twitter.contributees(7505382) # Same as above
def contributees(*args)
- users_from_response(:get, "/1.1/users/contributees.json", args)
+ user_objects_from_response(:get, "/1.1/users/contributees.json", args)
end
# Returns an array of users who can contribute to the specified account
@@ -365,7 +367,7 @@ def contributees(*args)
# Twitter.contributors('sferik')
# Twitter.contributors(7505382) # Same as above
def contributors(*args)
- users_from_response(:get, "/1.1/users/contributors.json", args)
+ user_objects_from_response(:get, "/1.1/users/contributors.json", args)
end
# Removes the authenticating user's profile banner image
View
115 lib/twitter/api/utils.rb
@@ -11,32 +11,55 @@ module Utils
private
- # @param klass [Class]
- # @param array [Array]
- # @return [Array]
- def collection_from_array(klass, array)
- array.map do |element|
- klass.fetch_or_new(element)
+ # @param request_method [Symbol]
+ # @param path [String]
+ # @param args [Array]
+ # @return [Array<Twitter::User>]
+ def threaded_user_objects_from_response(request_method, path, args)
+ options = extract_options!(args)
+ args.flatten.threaded_map do |user|
+ object_from_response(Twitter::User, request_method, path, merge_user(options, user))
end
end
+ # @param request_method [Symbol]
+ # @param path [String]
+ # @param args [Array]
+ # @return [Array<Twitter::User>]
+ def user_objects_from_response(request_method, path, args)
+ options = extract_options!(args)
+ merge_user!(options, args.pop || screen_name) unless options[:user_id] || options[:screen_name]
+ objects_from_response(Twitter::User, request_method, path, options)
+ end
+
# @param klass [Class]
# @param request_method [Symbol]
# @param path [String]
- # @param params [Hash]
+ # @param args [Array]
# @return [Array]
- def collection_from_response(klass, request_method, path, params={})
- collection_from_array(klass, send(request_method.to_sym, path, params)[:body])
+ def objects_from_response_with_user(klass, request_method, path, args)
+ options = extract_options!(args)
+ merge_user!(options, args.pop)
+ objects_from_response(klass, request_method, path, options)
end
# @param klass [Class]
# @param request_method [Symbol]
# @param path [String]
- # @param params [Hash]
- # @return [Object]
- def object_from_response(klass, request_method, path, params={})
- response = send(request_method.to_sym, path, params)
- klass.from_response(response)
+ # @param options [Hash]
+ # @return [Array]
+ def objects_from_response(klass, request_method, path, options={})
+ response = send(request_method.to_sym, path, options)[:body]
+ objects_from_array(klass, response)
+ end
+
+ # @param klass [Class]
+ # @param array [Array]
+ # @return [Array]
+ def objects_from_array(klass, array)
+ array.map do |element|
+ klass.fetch_or_new(element)
+ end
end
# @param klass [Class]
@@ -44,20 +67,21 @@ def object_from_response(klass, request_method, path, params={})
# @param path [String]
# @param args [Array]
# @return [Array]
- def objects_from_response(klass, request_method, path, args)
+ def threaded_object_from_response(klass, request_method, path, args)
options = extract_options!(args)
- merge_user!(options, args.pop)
- collection_from_response(klass, request_method, path, options)
+ args.flatten.threaded_map do |id|
+ object_from_response(klass, request_method, path, options.merge(:id => id))
+ end
end
+ # @param klass [Class]
# @param request_method [Symbol]
# @param path [String]
- # @param args [Array]
- # @return [Array<Integer>]
- def ids_from_response(request_method, path, args, method_name)
- options = extract_options!(args)
- merge_user!(options, args.pop)
- cursor_from_response(:ids, nil, request_method, path, options, method_name)
+ # @param options [Hash]
+ # @return [Object]
+ def object_from_response(klass, request_method, path, options={})
+ response = send(request_method.to_sym, path, options)
+ klass.from_response(response)
end
# @param collection_name [Symbol]
@@ -67,7 +91,7 @@ def ids_from_response(request_method, path, args, method_name)
# @param args [Array]
# @param method_name [Symbol]
# @return [Twitter::Cursor]
- def cursor_object_from_response(collection_name, klass, request_method, path, args, method_name)
+ def cursor_from_response_with_user(collection_name, klass, request_method, path, args, method_name)
options = extract_options!(args)
merge_user!(options, args.pop || screen_name) unless options[:user_id] || options[:screen_name]
cursor_from_response(collection_name, klass, request_method, path, options, method_name)
@@ -77,46 +101,13 @@ def cursor_object_from_response(collection_name, klass, request_method, path, ar
# @param klass [Class]
# @param request_method [Symbol]
# @param path [String]
- # @param params [Hash]
+ # @param options [Hash]
# @param method_name [Symbol]
# @return [Twitter::Cursor]
- def cursor_from_response(collection_name, klass, request_method, path, params, method_name)
- merge_default_cursor!(params)
- response = send(request_method.to_sym, path, params)
- Twitter::Cursor.from_response(response, collection_name.to_sym, klass, self, method_name, params)
- end
-
- # @param request_method [Symbol]
- # @param path [String]
- # @param args [Array]
- # @return [Array<Twitter::User>]
- def users_from_response(request_method, path, args)
- options = extract_options!(args)
- merge_user!(options, args.pop || screen_name) unless options[:user_id] || options[:screen_name]
- collection_from_response(Twitter::User, request_method, path, options)
- end
-
- # @param request_method [Symbol]
- # @param path [String]
- # @param args [Array]
- # @return [Array<Twitter::User>]
- def threaded_users_from_response(request_method, path, args)
- options = extract_options!(args)
- args.flatten.threaded_map do |user|
- object_from_response(Twitter::User, request_method, path, merge_user(options, user))
- end
- end
-
- # @param klass [Class]
- # @param request_method [Symbol]
- # @param path [String]
- # @param args [Array]
- # @return [Array]
- def threaded_object_from_response(klass, request_method, path, args)
- options = extract_options!(args)
- args.flatten.threaded_map do |id|
- object_from_response(klass, request_method, path, options.merge(:id => id))
- end
+ def cursor_from_response(collection_name, klass, request_method, path, options, method_name)
+ merge_default_cursor!(options)
+ response = send(request_method.to_sym, path, options)
+ Twitter::Cursor.from_response(response, collection_name.to_sym, klass, self, method_name, options)
end
def handle_forbidden_error(klass, error)
View
60 spec/twitter/api/friends_and_followers_spec.rb
@@ -53,11 +53,13 @@
end
context "without arguments passed" do
before do
- stub_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1"}).to_return(:body => fixture("ids_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ stub_get("/1.1/account/verify_credentials.json").to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ stub_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"}).to_return(:body => fixture("ids_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.friend_ids
- expect(a_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1"})).to have_been_made
+ expect(a_get("/1.1/account/verify_credentials.json")).to have_been_made
+ expect(a_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"})).to have_been_made
end
it "returns an array of numeric IDs for every user the specified user is following" do
friend_ids = @client.friend_ids
@@ -67,12 +69,13 @@
end
context "with all" do
before do
- stub_get("/1.1/friends/ids.json").with(:query => {:cursor => "1305102810874389703"}).to_return(:body => fixture("ids_list2.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ stub_get("/1.1/friends/ids.json").with(:query => {:cursor => "1305102810874389703", :screen_name => "sferik"}).to_return(:body => fixture("ids_list2.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.friend_ids.all
- expect(a_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1"})).to have_been_made
- expect(a_get("/1.1/friends/ids.json").with(:query => {:cursor => "1305102810874389703"})).to have_been_made
+ expect(a_get("/1.1/account/verify_credentials.json")).to have_been_made
+ expect(a_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"})).to have_been_made
+ expect(a_get("/1.1/friends/ids.json").with(:query => {:cursor => "1305102810874389703", :screen_name => "sferik"})).to have_been_made
end
end
end
@@ -125,11 +128,13 @@
end
context "without arguments passed" do
before do
- stub_get("/1.1/followers/ids.json").with(:query => {:cursor => "-1"}).to_return(:body => fixture("ids_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ stub_get("/1.1/account/verify_credentials.json").to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ stub_get("/1.1/followers/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"}).to_return(:body => fixture("ids_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.follower_ids
- expect(a_get("/1.1/followers/ids.json").with(:query => {:cursor => "-1"})).to have_been_made
+ expect(a_get("/1.1/account/verify_credentials.json")).to have_been_made
+ expect(a_get("/1.1/followers/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"})).to have_been_made
end
it "returns an array of numeric IDs for every user following the specified user" do
follower_ids = @client.follower_ids
@@ -139,12 +144,13 @@
end
context "with all" do
before do
- stub_get("/1.1/followers/ids.json").with(:query => {:cursor => "1305102810874389703"}).to_return(:body => fixture("ids_list2.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ stub_get("/1.1/followers/ids.json").with(:query => {:cursor => "1305102810874389703", :screen_name => "sferik"}).to_return(:body => fixture("ids_list2.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.follower_ids.all
- expect(a_get("/1.1/followers/ids.json").with(:query => {:cursor => "-1"})).to have_been_made
- expect(a_get("/1.1/followers/ids.json").with(:query => {:cursor => "1305102810874389703"})).to have_been_made
+ expect(a_get("/1.1/account/verify_credentials.json")).to have_been_made
+ expect(a_get("/1.1/followers/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"})).to have_been_made
+ expect(a_get("/1.1/followers/ids.json").with(:query => {:cursor => "1305102810874389703", :screen_name => "sferik"})).to have_been_made
end
end
end
@@ -295,13 +301,15 @@
describe "#follow" do
context "with :follow => true passed" do
before do
- stub_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1"}).to_return(:body => fixture("ids_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ stub_get("/1.1/account/verify_credentials.json").to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ stub_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"}).to_return(:body => fixture("ids_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik,pengwynn"}).to_return(:body => fixture("friendships.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_post("/1.1/friendships/create.json").with(:body => {:user_id => "7505382", :follow => "true"}).to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.follow("sferik", "pengwynn", :follow => true)
- expect(a_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1"})).to have_been_made
+ expect(a_get("/1.1/account/verify_credentials.json")).to have_been_made
+ expect(a_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"})).to have_been_made
expect(a_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik,pengwynn"})).to have_been_made
expect(a_post("/1.1/friendships/create.json").with(:body => {:user_id => "7505382", :follow => "true"})).to have_been_made
end
@@ -314,41 +322,33 @@
end
context "with :follow => false passed" do
before do
- stub_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1"}).to_return(:body => fixture("ids_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ stub_get("/1.1/account/verify_credentials.json").to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ stub_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"}).to_return(:body => fixture("ids_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik,pengwynn"}).to_return(:body => fixture("friendships.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_post("/1.1/friendships/create.json").with(:body => {:user_id => "7505382"}).to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.follow("sferik", "pengwynn", :follow => false)
- expect(a_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1"})).to have_been_made
+ expect(a_get("/1.1/account/verify_credentials.json")).to have_been_made
+ expect(a_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"})).to have_been_made
expect(a_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik,pengwynn"})).to have_been_made
expect(a_post("/1.1/friendships/create.json").with(:body => {:user_id => "7505382"})).to have_been_made
end
- it "returns an array of befriended users" do
- users = @client.follow("sferik", "pengwynn", :follow => false)
- expect(users).to be_an Array
- expect(users.first).to be_a Twitter::User
- expect(users.first.id).to eq 7505382
- end
end
context "without :follow passed" do
before do
- stub_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1"}).to_return(:body => fixture("ids_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ stub_get("/1.1/account/verify_credentials.json").to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ stub_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"}).to_return(:body => fixture("ids_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik,pengwynn"}).to_return(:body => fixture("friendships.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_post("/1.1/friendships/create.json").with(:body => {:user_id => "7505382"}).to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.follow("sferik", "pengwynn")
- expect(a_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1"})).to have_been_made
+ expect(a_get("/1.1/account/verify_credentials.json")).to have_been_made
+ expect(a_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"})).to have_been_made
expect(a_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik,pengwynn"})).to have_been_made
expect(a_post("/1.1/friendships/create.json").with(:body => {:user_id => "7505382"})).to have_been_made
end
- it "returns an array of befriended users" do
- users = @client.follow("sferik", "pengwynn")
- expect(users).to be_an Array
- expect(users.first).to be_a Twitter::User
- expect(users.first.id).to eq 7505382
- end
end
end
@@ -570,6 +570,7 @@
end
it "requests the correct resource" do
@client.followers
+ expect(a_get("/1.1/account/verify_credentials.json")).to have_been_made
expect(a_get("/1.1/followers/list.json").with(:query => {:cursor => "-1", :screen_name => "sferik"})).to have_been_made
end
it "returns a cursor of followers with details for every user the specified user is followed by" do
@@ -584,6 +585,7 @@
end
it "requests the correct resource" do
@client.followers.all
+ expect(a_get("/1.1/account/verify_credentials.json")).to have_been_made
expect(a_get("/1.1/followers/list.json").with(:query => {:cursor => "-1", :screen_name => "sferik"})).to have_been_made
expect(a_get("/1.1/followers/list.json").with(:query => {:cursor => "1419103567112105362", :screen_name => "sferik"})).to have_been_made
end
@@ -643,6 +645,7 @@
end
it "requests the correct resource" do
@client.friends
+ expect(a_get("/1.1/account/verify_credentials.json")).to have_been_made
expect(a_get("/1.1/friends/list.json").with(:query => {:cursor => "-1", :screen_name => "sferik"})).to have_been_made
end
it "returns a cursor of friends with details for every user the specified user is following" do
@@ -657,6 +660,7 @@
end
it "requests the correct resource" do
@client.friends.all
+ expect(a_get("/1.1/account/verify_credentials.json")).to have_been_made
expect(a_get("/1.1/friends/list.json").with(:query => {:cursor => "-1", :screen_name => "sferik"})).to have_been_made
expect(a_get("/1.1/friends/list.json").with(:query => {:cursor => "1418947360875712729", :screen_name => "sferik"})).to have_been_made
end
Please sign in to comment.
Something went wrong with that request. Please try again.