Skip to content

Commit

Permalink
Add Twitter::Status#all_urls method
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmoon committed Oct 20, 2011
1 parent edf2ed4 commit b5feeb7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/twitter/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def ==(other)
super || (other.class == self.class && other.id == self.id)
end

def all_urls
@all_urls ||= begin
all_urls = [ urls, expanded_urls ].compact.flatten.uniq
all_urls.length > 0 ? all_urls : nil
end
end

def expanded_urls
@expanded_urls ||= Array(@attrs['entities']['urls']).map do |url|
url['expanded_url']
Expand Down
18 changes: 17 additions & 1 deletion spec/twitter/status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
end
end

describe "#url" do
describe "#urls" do
it "should return urls" do
urls = Twitter::Status.new('text' => "This Tweet contains a http://example.com.").urls
urls.should be_an Array
Expand All @@ -102,6 +102,22 @@
end
end

describe "#all_urls" do
it "should return urls in the text, and in all fields from the entities" do
urls = [{'url' => 'http://t.co/example', 'expanded_url' => 'http://example.com'}]
status_attributes = { 'text' => "This tweet contains a http://t.co/example.", 'entities' => {'urls' => urls} }
all_urls = Twitter::Status.new(status_attributes).all_urls
all_urls.should be_an Array
all_urls.first.should == "http://t.co/example"
all_urls.last.should == "http://example.com"
end

it "should return nil when not set" do
all_urls = Twitter::Status.new.all_urls
all_urls.should be_nil
end
end

describe "#user" do
it "should return a User when user is set" do
user = Twitter::Status.new('user' => {}).user
Expand Down

0 comments on commit b5feeb7

Please sign in to comment.