Skip to content

Commit

Permalink
Remove Twitter::Cursor#collection
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jul 25, 2013
1 parent 72be414 commit 9ae4621
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 79 deletions.
2 changes: 1 addition & 1 deletion lib/twitter/api/friends_and_followers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def follow(*args)
# so only send follow if it's true
arguments.options[:follow] = true if !!arguments.options.delete(:follow)
existing_friends = Thread.new do
friend_ids.ids
friend_ids.to_a
end
new_friends = Thread.new do
users(args).map(&:id)
Expand Down
29 changes: 13 additions & 16 deletions lib/twitter/cursor.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,53 @@
module Twitter
class Cursor
include Enumerable
attr_reader :attrs, :collection
attr_reader :attrs
alias to_h attrs
alias to_hash attrs
alias to_hsh attrs

# Initializes a new Cursor object
#
# @param response [Hash]
# @param collection_name [String, Symbol] The name of the method to return the collection
# @param klass [Class] The class to instantiate object in the collection
# @param key [String, Symbol] The key to fetch the data from the response
# @param klass [Class] The class to instantiate objects in the response
# @param client [Twitter::Client]
# @param request_method [String, Symbol]
# @param path [String]
# @param options [Hash]
# @return [Twitter::Cursor]
def self.from_response(response, collection_name, klass, client, request_method, path, options)
new(response[:body], collection_name, klass, client, request_method, path, options)
def self.from_response(response, key, klass, client, request_method, path, options)
new(response[:body], key, klass, client, request_method, path, options)
end

# Initializes a new Cursor
#
# @param attrs [Hash]
# @param collection_name [String, Symbol] The name of the method to return the collection
# @param klass [Class] The class to instantiate object in the collection
# @param key [String, Symbol] The key to fetch the data from the response
# @param klass [Class] The class to instantiate objects in the response
# @param client [Twitter::Client]
# @param request_method [String, Symbol]
# @param path [String]
# @param options [Hash]
# @return [Twitter::Cursor]
def initialize(attrs, collection_name, klass, client, request_method, path, options)
@collection_name = collection_name.to_sym
def initialize(attrs, key, klass, client, request_method, path, options)
@key = key.to_sym
@klass = klass
@client = client
@request_method = request_method.to_sym
@path = path
@options = options
set_attrs(attrs)
singleton_class.class_eval do
alias_method(collection_name.to_sym, :collection)
end
end

# @return [Enumerator]
def each(&block)
return to_enum(:each) unless block_given?
@collection.each do |element|
@page.each do |element|
yield element
end
unless last?
fetch_next
fetch_next_page
each(&block)
end
self
Expand Down Expand Up @@ -78,14 +75,14 @@ def last?

private

def fetch_next
def fetch_next_page
response = @client.send(@request_method, @path, @options.merge(:cursor => next_cursor))
set_attrs(response[:body])
end

def set_attrs(attrs)
@attrs = attrs
@collection = Array(attrs[@collection_name]).map do |element|
@page = Array(attrs[@key]).map do |element|
@klass ? @klass.new(element) : element
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/ids_list.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"previous_cursor":0,"next_cursor_str":"1305102810874389703","ids":[14100886,22469930,351223419],"previous_cursor_str":"0","next_cursor":1305102810874389703}
{"previous_cursor":0,"next_cursor_str":"1305102810874389703","ids":[20009713,22469930,351223419],"previous_cursor_str":"0","next_cursor":1305102810874389703}
2 changes: 1 addition & 1 deletion spec/fixtures/ids_list2.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"previous_cursor":-1305101990888327757,"next_cursor_str":"0","ids":[20009713,23621851,14509199],"previous_cursor_str":"-1305101990888327757","next_cursor":0}
{"previous_cursor":-1305101990888327757,"next_cursor_str":"0","ids":[14100886,23621851,14509199],"previous_cursor_str":"-1305101990888327757","next_cursor":0}
36 changes: 13 additions & 23 deletions spec/twitter/api/friends_and_followers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
it "returns an array of numeric IDs for every user the specified user is following" do
friend_ids = @client.friend_ids("sferik")
expect(friend_ids).to be_a Twitter::Cursor
expect(friend_ids.ids).to be_an Array
expect(friend_ids.ids.first).to eq 14100886
expect(friend_ids.first).to eq 20009713
end
context "with each" do
before do
Expand Down Expand Up @@ -64,8 +63,7 @@
it "returns an array of numeric IDs for every user the specified user is following" do
friend_ids = @client.friend_ids
expect(friend_ids).to be_a Twitter::Cursor
expect(friend_ids.ids).to be_an Array
expect(friend_ids.ids.first).to eq 14100886
expect(friend_ids.first).to eq 20009713
end
context "with each" do
before do
Expand Down Expand Up @@ -93,8 +91,7 @@
it "returns an array of numeric IDs for every user following the specified user" do
follower_ids = @client.follower_ids("sferik")
expect(follower_ids).to be_a Twitter::Cursor
expect(follower_ids.ids).to be_an Array
expect(follower_ids.ids.first).to eq 14100886
expect(follower_ids.first).to eq 20009713
end
context "with each" do
before do
Expand Down Expand Up @@ -139,8 +136,7 @@
it "returns an array of numeric IDs for every user following the specified user" do
follower_ids = @client.follower_ids
expect(follower_ids).to be_a Twitter::Cursor
expect(follower_ids.ids).to be_an Array
expect(follower_ids.ids.first).to eq 14100886
expect(follower_ids.first).to eq 20009713
end
context "with each" do
before do
Expand Down Expand Up @@ -259,8 +255,7 @@
it "returns an array of numeric IDs for every user who has a pending request to follow the authenticating user" do
friendships_incoming = @client.friendships_incoming
expect(friendships_incoming).to be_a Twitter::Cursor
expect(friendships_incoming.ids).to be_an Array
expect(friendships_incoming.ids.first).to eq 14100886
expect(friendships_incoming.first).to eq 20009713
end
context "with each" do
before do
Expand All @@ -284,8 +279,7 @@
it "returns an array of numeric IDs for every protected user for whom the authenticating user has a pending follow request" do
friendships_outgoing = @client.friendships_outgoing
expect(friendships_outgoing).to be_a Twitter::Cursor
expect(friendships_outgoing.ids).to be_an Array
expect(friendships_outgoing.ids.first).to eq 14100886
expect(friendships_outgoing.first).to eq 20009713
end
context "with each" do
before do
Expand All @@ -302,7 +296,7 @@
context "with :follow => true passed" do
before do
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_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"}).to_return(:body => fixture("ids_list2.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
Expand All @@ -323,7 +317,7 @@
context "with :follow => false passed" do
before do
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_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"}).to_return(:body => fixture("ids_list2.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
Expand All @@ -338,7 +332,7 @@
context "without :follow passed" do
before do
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_get("/1.1/friends/ids.json").with(:query => {:cursor => "-1", :screen_name => "sferik"}).to_return(:body => fixture("ids_list2.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
Expand Down Expand Up @@ -530,8 +524,7 @@
it "returns a cursor of followers with details for every user the specified user is followed by" do
followers = @client.followers("sferik")
expect(followers).to be_a Twitter::Cursor
expect(followers.users).to be_an Array
expect(followers.users.first).to be_a Twitter::User
expect(followers.first).to be_a Twitter::User
end
context "with each" do
before do
Expand Down Expand Up @@ -576,8 +569,7 @@
it "returns a cursor of followers with details for every user the specified user is followed by" do
followers = @client.followers
expect(followers).to be_a Twitter::Cursor
expect(followers.users).to be_an Array
expect(followers.users.first).to be_a Twitter::User
expect(followers.first).to be_a Twitter::User
end
context "with each" do
before do
Expand Down Expand Up @@ -605,8 +597,7 @@
it "returns a cursor of friends with details for every user the specified user is following" do
friends = @client.friends("sferik")
expect(friends).to be_a Twitter::Cursor
expect(friends.users).to be_an Array
expect(friends.users.first).to be_a Twitter::User
expect(friends.first).to be_a Twitter::User
end
context "with each" do
before do
Expand Down Expand Up @@ -651,8 +642,7 @@
it "returns a cursor of friends with details for every user the specified user is following" do
friends = @client.friends
expect(friends).to be_a Twitter::Cursor
expect(friends.users).to be_an Array
expect(friends.users.first).to be_a Twitter::User
expect(friends.first).to be_a Twitter::User
end
context "with each" do
before do
Expand Down
30 changes: 12 additions & 18 deletions spec/twitter/api/lists_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@
it "returns the lists the specified user has been added to" do
memberships = @client.memberships("sferik")
expect(memberships).to be_a Twitter::Cursor
expect(memberships.lists).to be_an Array
expect(memberships.lists.first).to be_a Twitter::List
expect(memberships.lists.first.name).to eq "developer"
expect(memberships.first).to be_a Twitter::List
expect(memberships.first.name).to eq "developer"
end
context "with each" do
before do
Expand Down Expand Up @@ -184,9 +183,8 @@
it "returns the subscribers of the specified list" do
list_subscribers = @client.list_subscribers("sferik", "presidents")
expect(list_subscribers).to be_a Twitter::Cursor
expect(list_subscribers.users).to be_an Array
expect(list_subscribers.users.first).to be_a Twitter::User
expect(list_subscribers.users.first.id).to eq 7505382
expect(list_subscribers.first).to be_a Twitter::User
expect(list_subscribers.first.id).to eq 7505382
end
context "with each" do
before do
Expand Down Expand Up @@ -488,9 +486,8 @@
it "returns the members of the specified list" do
list_members = @client.list_members("sferik", "presidents")
expect(list_members).to be_a Twitter::Cursor
expect(list_members.users).to be_an Array
expect(list_members.users.first).to be_a Twitter::User
expect(list_members.users.first.id).to eq 7505382
expect(list_members.first).to be_a Twitter::User
expect(list_members.first.id).to eq 7505382
end
context "with each" do
before do
Expand Down Expand Up @@ -755,9 +752,8 @@
it "returns the lists the specified user follows" do
subscriptions = @client.subscriptions("sferik")
expect(subscriptions).to be_a Twitter::Cursor
expect(subscriptions.lists).to be_an Array
expect(subscriptions.lists.first).to be_a Twitter::List
expect(subscriptions.lists.first.name).to eq "Rubyists"
expect(subscriptions.first).to be_a Twitter::List
expect(subscriptions.first.name).to eq "Rubyists"
end
context "with each" do
before do
Expand Down Expand Up @@ -868,9 +864,8 @@
it "returns the requested list" do
lists = @client.lists_owned("sferik")
expect(lists).to be_a Twitter::Cursor
expect(lists.lists).to be_an Array
expect(lists.lists.first).to be_a Twitter::List
expect(lists.lists.first.name).to eq "My favstar.fm list"
expect(lists.first).to be_a Twitter::List
expect(lists.first.name).to eq "My favstar.fm list"
end
end
context "without a screen name passed" do
Expand All @@ -885,9 +880,8 @@
it "returns the requested list" do
lists = @client.lists_owned
expect(lists).to be_a Twitter::Cursor
expect(lists.lists).to be_an Array
expect(lists.lists.first).to be_a Twitter::List
expect(lists.lists.first.name).to eq "My favstar.fm list"
expect(lists.first).to be_a Twitter::List
expect(lists.first.name).to eq "My favstar.fm list"
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/twitter/api/tweets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,7 @@
it "returns a collection of up to 100 user IDs belonging to users who have retweeted the tweet specified by the id parameter" do
retweeters_ids = @client.retweeters_ids(25938088801)
expect(retweeters_ids).to be_a Twitter::Cursor
expect(retweeters_ids.ids).to be_an Array
expect(retweeters_ids.ids.first).to eq 14100886
expect(retweeters_ids.first).to eq 20009713
end
context "with each" do
before do
Expand Down
6 changes: 2 additions & 4 deletions spec/twitter/api/undocumented_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
it "returns an array of numeric IDs for every user following the specified user" do
following_followers_of = @client.following_followers_of("sferik")
expect(following_followers_of).to be_a Twitter::Cursor
expect(following_followers_of.users).to be_an Array
expect(following_followers_of.users.first).to be_a Twitter::User
expect(following_followers_of.first).to be_a Twitter::User
end
context "with each" do
before do
Expand Down Expand Up @@ -64,8 +63,7 @@
it "returns an array of numeric IDs for every user following the specified user" do
following_followers_of = @client.following_followers_of
expect(following_followers_of).to be_a Twitter::Cursor
expect(following_followers_of.users).to be_an Array
expect(following_followers_of.users.first).to be_a Twitter::User
expect(following_followers_of.first).to be_a Twitter::User
end
context "with each" do
before do
Expand Down
8 changes: 3 additions & 5 deletions spec/twitter/api/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,8 @@
it "returns an array of user objects that the authenticating user is blocking" do
blocking = @client.blocking
expect(blocking).to be_a Twitter::Cursor
expect(blocking.users).to be_an Array
expect(blocking.users.first).to be_a Twitter::User
expect(blocking.users.first.id).to eq 7505382
expect(blocking.first).to be_a Twitter::User
expect(blocking.first.id).to eq 7505382
end
context "with each" do
before do
Expand All @@ -209,8 +208,7 @@
it "returns an array of numeric user IDs the authenticating user is blocking" do
blocked_ids = @client.blocked_ids
expect(blocked_ids).to be_a Twitter::Cursor
expect(blocked_ids.ids).to be_an Array
expect(blocked_ids.ids.first).to eq 14100886
expect(blocked_ids.first).to eq 20009713
end
context "with each" do
before do
Expand Down
8 changes: 0 additions & 8 deletions spec/twitter/cursor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

describe Twitter::Cursor do

describe "#collection" do
it "returns a collection" do
collection = Twitter::Cursor.new({:ids => [1, 2, 3, 4, 5]}, :ids, nil, Twitter::Client.new, :get, "/1.1/followers/ids.json", {}).collection
expect(collection).to be_an Array
expect(collection.first).to be_a Fixnum
end
end

describe "#each" do
before do
@client = Twitter::Client.new
Expand Down

0 comments on commit 9ae4621

Please sign in to comment.