Skip to content

Commit

Permalink
Allow URI strings in addition to URI objects
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jul 20, 2013
1 parent 7e68c6b commit 89a46fb
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/twitter/api/lists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module API
module Lists
include Twitter::API::Utils
MAX_USERS_PER_REQUEST = 100
URI_SUBSTRING = "://"

# Returns all lists the authenticating or specified user subscribes to, including their own
#
Expand Down Expand Up @@ -591,7 +592,13 @@ def merge_list!(hash, list)
when Integer
hash[:list_id] = list
when String
hash[:slug] = list
if list[URI_SUBSTRING]
list = list.split("/")
hash[:slug] = list.pop
hash[:owner_screen_name] = list.pop
else
hash[:slug] = list
end
when URI
list = list.path.split("/")
hash[:slug] = list.pop
Expand Down
13 changes: 11 additions & 2 deletions lib/twitter/api/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module API
module Utils

DEFAULT_CURSOR = -1
URI_SUBSTRING = "://"

private

Expand Down Expand Up @@ -163,7 +164,11 @@ def merge_user!(hash, user, prefix=nil)
when Integer
hash[[prefix, "user_id"].compact.join("_").to_sym] = user
when String
hash[[prefix, "screen_name"].compact.join("_").to_sym] = user
if user[URI_SUBSTRING]
hash[[prefix, "screen_name"].compact.join("_").to_sym] = user.split("/").last
else
hash[[prefix, "screen_name"].compact.join("_").to_sym] = user
end
when URI
hash[[prefix, "screen_name"].compact.join("_").to_sym] = user.path.split("/").last
when Twitter::User
Expand Down Expand Up @@ -193,7 +198,11 @@ def merge_users!(hash, users)
when Integer
user_ids << user
when String
screen_names << user
if user[URI_SUBSTRING]
screen_names << user.split("/").last
else
screen_names << user
end
when URI
screen_names << user.path.split("/").last
when Twitter::User
Expand Down
6 changes: 6 additions & 0 deletions spec/twitter/api/direct_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@
expect(a_post("/1.1/direct_messages/new.json").with(:body => {:screen_name => "pengwynn", :text => "Creating a fixture for the Twitter gem"})).to have_been_made
end
end
context "with a URI string passed" do
it "requests the correct resource" do
@client.direct_message_create("https://twitter.com/pengwynn", "Creating a fixture for the Twitter gem")
expect(a_post("/1.1/direct_messages/new.json").with(:body => {:screen_name => "pengwynn", :text => "Creating a fixture for the Twitter gem"})).to have_been_made
end
end
end

end
6 changes: 6 additions & 0 deletions spec/twitter/api/favorites_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
expect(a_get("/1.1/favorites/list.json").with(:query => {:screen_name => "sferik"})).to have_been_made
end
end
context "with a URI string passed" do
it "requests the correct resource" do
@client.favorites("https://twitter.com/sferik")
expect(a_get("/1.1/favorites/list.json").with(:query => {:screen_name => "sferik"})).to have_been_made
end
end
end
context "without arguments passed" do
before do
Expand Down
12 changes: 12 additions & 0 deletions spec/twitter/api/lists_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "sferik", :slug => "presidents"})).to have_been_made
end
end
context "with a URI string passed" do
it "requests the correct resource" do
@client.list_timeline("https://twitter.com/sferik/presidents")
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "sferik", :slug => "presidents"})).to have_been_made
end
end
context "with URI objects passed" do
it "requests the correct resource" do
user = URI.parse("https://twitter.com/sferik")
Expand All @@ -52,6 +58,12 @@
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "sferik", :slug => "presidents"})).to have_been_made
end
end
context "with URI strings passed" do
it "requests the correct resource" do
@client.list_timeline("https://twitter.com/sferik", "https://twitter.com/sferik/presidents")
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "sferik", :slug => "presidents"})).to have_been_made
end
end
end
context "without a screen name passed" do
before do
Expand Down
12 changes: 12 additions & 0 deletions spec/twitter/api/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@
expect(a_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik,pengwynn"})).to have_been_made
end
end
context "with URI strings passed" do
it "requests the correct resource" do
@client.users("https://twitter.com/sferik", "https://twitter.com/pengwynn")
expect(a_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik,pengwynn"})).to have_been_made
end
end
end
context "with numeric screen names passed" do
before do
Expand Down Expand Up @@ -391,6 +397,12 @@
expect(a_get("/1.1/users/lookup.json").with(:query => {:screen_name => "sferik,pengwynn"})).to have_been_made
end
end
context "with URI objects passed" do
it "requests the correct resource" do
@client.users("https://twitter.com/sferik", "https://twitter.com/pengwynn", :method => :get)
expect(a_get("/1.1/users/lookup.json").with(:query => {:screen_name => "sferik,pengwynn"})).to have_been_made
end
end
end
context "with numeric screen names passed" do
before do
Expand Down

0 comments on commit 89a46fb

Please sign in to comment.