From 19f5796ba618e634ed56e936eb8f3bcb9822124c Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Wed, 25 Apr 2012 12:46:45 -0700 Subject: [PATCH] Add ability to pass a user to recommendations as an argument --- lib/twitter/client/users.rb | 6 +++- spec/twitter/client/users_spec.rb | 53 +++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/lib/twitter/client/users.rb b/lib/twitter/client/users.rb index 23eea5a85..b59d87fb9 100644 --- a/lib/twitter/client/users.rb +++ b/lib/twitter/client/users.rb @@ -190,7 +190,11 @@ def contributors(*args) # @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(options={}) + def recommendations(*args) + options = {} + options.merge!(args.last.is_a?(Hash) ? args.pop : {}) + user = args.pop || self.current_user.screen_name + options.merge_user!(user) options[:excluded] = options[:excluded].join(',') if options[:excluded].is_a?(Array) get("/1/users/recommendations.json", options).map do |recommendation| Twitter::User.new(recommendation['user']) diff --git a/spec/twitter/client/users_spec.rb b/spec/twitter/client/users_spec.rb index eb62084f0..5aac413ed 100644 --- a/spec/twitter/client/users_spec.rb +++ b/spec/twitter/client/users_spec.rb @@ -303,20 +303,47 @@ end describe ".recommendations" do - before do - stub_get("/1/users/recommendations.json"). - to_return(:body => fixture("recommendations.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - it "should request the correct resource" do - @client.recommendations - a_get("/1/users/recommendations.json"). - should have_been_made + context "with a screen name passed" do + before do + stub_get("/1/users/recommendations.json"). + with(:query => {:screen_name => "sferik"}). + to_return(:body => fixture("recommendations.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + @client.recommendations("sferik") + a_get("/1/users/recommendations.json"). + with(:query => {:screen_name => "sferik"}). + should have_been_made + end + it "should return recommended users for the authenticated user" do + recommendations = @client.recommendations("sferik") + recommendations.should be_an Array + recommendations.first.should be_a Twitter::User + recommendations.first.name.should == "John Trupiano" + end end - it "should return recommended users for the authenticated user" do - recommendations = @client.recommendations - recommendations.should be_an Array - recommendations.first.should be_a Twitter::User - recommendations.first.name.should == "John Trupiano" + context "without arguments 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/users/recommendations.json"). + with(:query => {:screen_name => "sferik"}). + to_return(:body => fixture("recommendations.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "should request the correct resource" do + @client.recommendations + a_get("/1/account/verify_credentials.json"). + should have_been_made + a_get("/1/users/recommendations.json"). + with(:query => {:screen_name => "sferik"}). + should have_been_made + end + it "should return recommended users for the authenticated user" do + recommendations = @client.recommendations + recommendations.should be_an Array + recommendations.first.should be_a Twitter::User + recommendations.first.name.should == "John Trupiano" + end end end