Skip to content

Instantly share code, notes, and snippets.

@tibbon
Created November 30, 2012 00:12
Show Gist options
  • Save tibbon/4172823 to your computer and use it in GitHub Desktop.
Save tibbon/4172823 to your computer and use it in GitHub Desktop.
Newest Twitter Friends Code
# In lib/twitter/api/friends_and_followers.rb
def friends(*args)
options = extract_options!(args)
merge_user!(options, args.pop || screen_name)
merge_default_cursor!(options)
cursor_from_response(:users, Twitter::User, :get, "/1.1/friends/list.json", options)
end
# In spec/twitter/api/friends_and_followers.rb
describe "#friends" do
context "with a screen_name passed" do
before do
stub_get("/1.1/friends/list.json").with(:query => {:cursor => "-1", :screen_name => "sferik"}).to_return(:body => fixture("friends_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.friends("sferik")
expect(a_get("/1.1/friends/list.json").with(:query => {:cursor => "-1", :screen_name => "sferik"})).to have_been_made
end
end
context "with user_id passed" do
before do
stub_get("/1.1/friends/list.json").with(:query => {:cursor => "-1", :user_id => "14100886"}).to_return(:body => fixture("friends_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.friends(14100886)
expect(a_get("/1.1/friends/list.json").with(:query => {:cursor => "-1", :user_id => "14100886"})).to have_been_made
end
end
context "without arguments passed" do
before do
stub_get("/1.1/friends/list.json").with(:query => {:cursor => "-1"}).to_return(:body => fixture("friends_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.friends
expect(a_get("/1.1/friends/list.json").with(:query => {:cursor => "-1"})).to have_been_made
end
end
end
# Rspec Output
Failures:
1) Twitter::API::FriendsAndFollowers#friends without arguments passed requests the correct resource
Failure/Error: @client.friends
WebMock::NetConnectNotAllowedError:
Real HTTP connections are disabled. Unregistered request: GET https://api.twitter.com/1.1/account/verify_credentials.json with headers {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'OAuth oauth_nonce="d6e831435720596c57a824d188f0731f", oauth_signature="863UhsbDyYFdYa2L7Wnw%2Fjz2tI8%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1354234121", oauth_version="1.0"', 'User-Agent'=>'Twitter Ruby Gem 4.3.0'}
You can stub this request with the following snippet:
stub_request(:get, "https://api.twitter.com/1.1/account/verify_credentials.json").
with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'OAuth oauth_nonce="d6e831435720596c57a824d188f0731f", oauth_signature="863UhsbDyYFdYa2L7Wnw%2Fjz2tI8%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1354234121", oauth_version="1.0"', 'User-Agent'=>'Twitter Ruby Gem 4.3.0'}).
to_return(:status => 200, :body => "", :headers => {})
registered request stubs:
stub_request(:get, "https://api.twitter.com/1.1/friends/list.json?cursor=-1")
============================================================
# ./lib/twitter/request/multipart_with_file.rb:19:in `call'
# ./lib/twitter/client.rb:81:in `request'
# ./lib/twitter/client.rb:64:in `get'
# ./lib/twitter/api/utils.rb:40:in `object_from_response'
# ./lib/twitter/api/users.rb:48:in `verify_credentials'
# ./lib/twitter/api/utils.rb:124:in `screen_name'
# ./lib/twitter/api/friends_and_followers.rb:312:in `friends'
# ./spec/twitter/api/friends_and_followers_spec.rb:496:in `block (4 levels) in <top (required)>'
Finished in 3.99 seconds
349 examples, 1 failure
Failed examples:
rspec ./spec/twitter/api/friends_and_followers_spec.rb:495 # Twitter::API::FriendsAndFollowers#friends without arguments passed requests the correct resource
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment