diff --git a/lib/twitter/api/friends_and_followers.rb b/lib/twitter/api/friends_and_followers.rb index 1cb6124bd..f5e8c0bb3 100644 --- a/lib/twitter/api/friends_and_followers.rb +++ b/lib/twitter/api/friends_and_followers.rb @@ -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) diff --git a/lib/twitter/cursor.rb b/lib/twitter/cursor.rb index fa4243143..2ac0105eb 100644 --- a/lib/twitter/cursor.rb +++ b/lib/twitter/cursor.rb @@ -1,7 +1,7 @@ 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 @@ -9,48 +9,45 @@ class Cursor # 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 @@ -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 diff --git a/spec/fixtures/ids_list.json b/spec/fixtures/ids_list.json index 0b3bd67d3..2608a5411 100644 --- a/spec/fixtures/ids_list.json +++ b/spec/fixtures/ids_list.json @@ -1 +1 @@ -{"previous_cursor":0,"next_cursor_str":"1305102810874389703","ids":[14100886,22469930,351223419],"previous_cursor_str":"0","next_cursor":1305102810874389703} \ No newline at end of file +{"previous_cursor":0,"next_cursor_str":"1305102810874389703","ids":[20009713,22469930,351223419],"previous_cursor_str":"0","next_cursor":1305102810874389703} \ No newline at end of file diff --git a/spec/fixtures/ids_list2.json b/spec/fixtures/ids_list2.json index 8e2b6470d..847cbee9b 100644 --- a/spec/fixtures/ids_list2.json +++ b/spec/fixtures/ids_list2.json @@ -1 +1 @@ -{"previous_cursor":-1305101990888327757,"next_cursor_str":"0","ids":[20009713,23621851,14509199],"previous_cursor_str":"-1305101990888327757","next_cursor":0} \ No newline at end of file +{"previous_cursor":-1305101990888327757,"next_cursor_str":"0","ids":[14100886,23621851,14509199],"previous_cursor_str":"-1305101990888327757","next_cursor":0} \ No newline at end of file diff --git a/spec/twitter/api/friends_and_followers_spec.rb b/spec/twitter/api/friends_and_followers_spec.rb index 32ed846db..9fbfb093a 100644 --- a/spec/twitter/api/friends_and_followers_spec.rb +++ b/spec/twitter/api/friends_and_followers_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/twitter/api/lists_spec.rb b/spec/twitter/api/lists_spec.rb index a98561612..b779355a2 100644 --- a/spec/twitter/api/lists_spec.rb +++ b/spec/twitter/api/lists_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/twitter/api/tweets_spec.rb b/spec/twitter/api/tweets_spec.rb index 7701b6a60..d9a7270e5 100644 --- a/spec/twitter/api/tweets_spec.rb +++ b/spec/twitter/api/tweets_spec.rb @@ -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 diff --git a/spec/twitter/api/undocumented_spec.rb b/spec/twitter/api/undocumented_spec.rb index 6f55de1d4..f3f10b069 100644 --- a/spec/twitter/api/undocumented_spec.rb +++ b/spec/twitter/api/undocumented_spec.rb @@ -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 @@ -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 diff --git a/spec/twitter/api/users_spec.rb b/spec/twitter/api/users_spec.rb index 9dd5fe0ba..3bfa715e8 100644 --- a/spec/twitter/api/users_spec.rb +++ b/spec/twitter/api/users_spec.rb @@ -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 @@ -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 diff --git a/spec/twitter/cursor_spec.rb b/spec/twitter/cursor_spec.rb index 7208433d7..faf9dfaaf 100644 --- a/spec/twitter/cursor_spec.rb +++ b/spec/twitter/cursor_spec.rb @@ -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