Skip to content

Commit

Permalink
Major changes for Twitter API v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Sep 17, 2012
1 parent 2829710 commit eab13be
Show file tree
Hide file tree
Showing 19 changed files with 476 additions and 324 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ Here are some fun facts about the 3.0 release:

* The entire library is implemented in just 2,000 lines of code
* With over 5,000 lines of specs, the spec-to-code ratio is over 2.5:1
* The spec suite contains 661 examples and runs in under 2 seconds on a MacBook
* The spec suite contains 662 examples and runs in under 2 seconds on a MacBook
* This project has 100% C0 code coverage (the tests execute every line of
source code at least once)
* At the time of release, this library is comprehensive: you can request all
Expand Down
291 changes: 175 additions & 116 deletions lib/twitter/api.rb

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions lib/twitter/core_ext/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ def merge_owner!(user)
#
# @param user[Integer, String, Twitter::User] A Twitter user ID, screen_name, or object.
# @return [Hash]
def merge_user(user, prefix=nil, suffix=nil)
self.dup.merge_user!(user, prefix, suffix)
def merge_user(user, prefix=nil)
self.dup.merge_user!(user, prefix)
end

# Take a user and merge it into the hash with the correct key
#
# @param user[Integer, String, Twitter::User] A Twitter user ID, screen_name, or object.
# @return [Hash]
def merge_user!(user, prefix=nil, suffix=nil)
def merge_user!(user, prefix=nil)
case user
when Integer
self[[prefix, "user_id", suffix].compact.join("_").to_sym] = user
self[[prefix, "user_id"].compact.join("_").to_sym] = user
when String
self[[prefix, "screen_name", suffix].compact.join("_").to_sym] = user
self[[prefix, "screen_name"].compact.join("_").to_sym] = user
when Twitter::User
self[[prefix, "user_id", suffix].compact.join("_").to_sym] = user.id
self[[prefix, "user_id"].compact.join("_").to_sym] = user.id
end
self
end
Expand Down
4 changes: 0 additions & 4 deletions lib/twitter/response/parse_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ def parse(body)
case body
when /\A^\s*$\z/, nil
nil
when 'true'
true
when 'false'
false
else
MultiJson.load(body, :symbolize_keys => true)
end
Expand Down
1 change: 0 additions & 1 deletion spec/fixtures/false.json

This file was deleted.

1 change: 1 addition & 0 deletions spec/fixtures/following.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"relationship":{"target":{"followed_by":true,"id_str":"14100886","following":false,"screen_name":"pengwynn","id":14100886},"source":{"marked_spam":false,"notifications_enabled":false,"followed_by":false,"want_retweets":true,"id_str":"7505382","blocking":false,"all_replies":false,"following":true,"screen_name":"sferik","id":7505382}}}
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions spec/fixtures/not_following.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"relationship":{"target":{"followed_by":false,"id_str":"14100886","following":true,"screen_name":"sferik","id":7505382},"source":{"marked_spam":false,"notifications_enabled":false,"followed_by":true,"want_retweets":true,"id_str":"7505382","blocking":false,"all_replies":false,"following":false,"screen_name":"pengwynn","id":14100886}}}
1 change: 0 additions & 1 deletion spec/fixtures/relationship.json

This file was deleted.

2 changes: 1 addition & 1 deletion spec/fixtures/statuses.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion spec/fixtures/true.json

This file was deleted.

119 changes: 89 additions & 30 deletions spec/twitter/api/blocks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,119 @@
describe "#blocking" do
before do
stub_get("/1.1/blocks/list.json").
to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
with(:query => {:cursor => "-1"}).
to_return(:body => fixture("users_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.blocking
a_get("/1.1/blocks/list.json").
with(:query => {:cursor => "-1"}).
should have_been_made
end
it "returns an array of user objects that the authenticating user is blocking" do
users = @client.blocking
users.should be_an Array
users.first.should be_a Twitter::User
users.first.id.should eq 7505382
blocking = @client.blocking
blocking.should be_a Twitter::Cursor
blocking.users.should be_an Array
blocking.users.first.should be_a Twitter::User
blocking.users.first.id.should eq 7505382
end
end

describe "#blocked_ids" do
before do
stub_get("/1.1/blocks/ids.json").
to_return(:body => fixture("ids.json"), :headers => {:content_type => "application/json; charset=utf-8"})
with(:query => {:cursor => "-1"}).
to_return(:body => fixture("ids_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.blocked_ids
a_get("/1.1/blocks/ids.json").
with(:query => {:cursor => "-1"}).
should have_been_made
end
it "returns an array of numeric user IDs the authenticating user is blocking" do
ids = @client.blocked_ids
ids.should be_an Array
ids.first.should eq 47
blocked_ids = @client.blocked_ids
blocked_ids.should be_a Twitter::Cursor
blocked_ids.ids.should be_an Array
blocked_ids.ids.first.should eq 14100886
end
end

describe "#block?" do
before do
stub_get("/1.1/blocks/exists.json").
with(:query => {:screen_name => "sferik"}).
to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/blocks/exists.json").
with(:query => {:screen_name => "pengwynn"}).
to_return(:body => fixture("not_found.json"), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.block?("sferik")
a_get("/1.1/blocks/exists.json").
with(:query => {:screen_name => "sferik"}).
should have_been_made
end
it "returns true if block exists" do
block = @client.block?("sferik")
block.should be_true
end
it "returns false if block does not exist" do
block = @client.block?("pengwynn")
block.should be_false
context "with a screen name passed" do
before do
stub_get("/1.1/blocks/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/blocks/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/users/show.json").
with(:query => {:screen_name => "pengwynn"}).
to_return(:body => fixture("pengwynn.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/users/show.json").
with(:query => {:screen_name => "sferik"}).
to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.block?("sferik")
a_get("/1.1/blocks/ids.json").
with(:query => {:cursor => "-1"}).
should have_been_made
a_get("/1.1/blocks/ids.json").
with(:query => {:cursor => "1305102810874389703"}).
should have_been_made
a_get("/1.1/users/show.json").
with(:query => {:screen_name => "sferik"}).
should have_been_made
end
it "returns true if block exists" do
block = @client.block?("pengwynn")
block.should be_true
end
it "returns false if block does not exist" do
block = @client.block?("sferik")
block.should be_false
end
end
context "with a user ID passed" do
before do
stub_get("/1.1/blocks/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/blocks/ids.json").
with(:query => {:cursor => "1305102810874389703"}).
to_return(:body => fixture("ids_list2.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resources" do
@client.block?(7505382)
a_get("/1.1/blocks/ids.json").
with(:query => {:cursor => "-1"}).
should have_been_made
a_get("/1.1/blocks/ids.json").
with(:query => {:cursor => "1305102810874389703"}).
should have_been_made
end
end
context "with a user object passed" do
before do
stub_get("/1.1/blocks/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/blocks/ids.json").
with(:query => {:cursor => "1305102810874389703"}).
to_return(:body => fixture("ids_list2.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resources" do
user = Twitter::User.new(:id => '7505382')
@client.block?(user)
a_get("/1.1/blocks/ids.json").
with(:query => {:cursor => "-1"}).
should have_been_made
a_get("/1.1/blocks/ids.json").
with(:query => {:cursor => "1305102810874389703"}).
should have_been_made
end
end
end

Expand Down
6 changes: 4 additions & 2 deletions spec/twitter/api/direct_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@
describe "#direct_messages" do
context "with ids passed" do
before do
stub_get("/1.1/direct_messages/show/1825786345.json").
stub_get("/1.1/direct_messages/show.json").
with(:query => {:id => "1825786345"}).
to_return(:body => fixture("direct_message.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.direct_messages(1825786345)
a_get("/1.1/direct_messages/show/1825786345.json").
a_get("/1.1/direct_messages/show.json").
with(:query => {:id => "1825786345"}).
should have_been_made
end
it "returns an array of direct messages" do
Expand Down
Loading

0 comments on commit eab13be

Please sign in to comment.