Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add accessor for expanded_urls from tweet entities #203

Merged
merged 1 commit into from

2 participants

@seanmoon

Hey there,

The current Twitter::Status class doesn't provide a convenient way to access the 'expanded_urls' field that the Twitter API returns in the tweet's entities.

These 'expanded_urls' point to, for example, a yfrog URL where a photo has been uploaded, rather than Twitter's shortener link. Unlike the shortened link, these often point directly at media such as from yfrog which a developer (such as myself!) might like to embed with the tweet's text.

Newer tweets (after October 10th, 2011) sometimes include this in 'media' within the entities, but older tweets don't exhibit this behavior.

Thanks!

Sean

@sferik sferik merged commit a8c96d9 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 18, 2011
  1. @seanmoon
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 0 deletions.
  1. +6 −0 lib/twitter/status.rb
  2. +13 −0 spec/twitter/status_spec.rb
View
6 lib/twitter/status.rb
@@ -22,6 +22,12 @@ def ==(other)
super || (other.class == self.class && other.id == self.id)
end
+ def expanded_urls
+ @expanded_urls ||= Array(@attributes['entities']['urls']).map do |url|
+ url['expanded_url']
+ end unless @attributes['entities'].nil?
+ end
+
def geo
@geo ||= Twitter::GeoFactory.new(@attributes['geo']) unless @attributes['geo'].nil?
end
View
13 spec/twitter/status_spec.rb
@@ -89,6 +89,19 @@
end
end
+ describe "#expanded_urls" do
+ it "should return the expanded urls" do
+ urls = [{'expanded_url' => 'http://example.com'}]
+ expanded_urls = Twitter::Status.new('entities' => {'urls' => urls}).expanded_urls
+ expanded_urls.should be_an Array
+ expanded_urls.first.should == "http://example.com"
+ end
+ it "should return nil when not set" do
+ expanded_urls = Twitter::Status.new.expanded_urls
+ expanded_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
Something went wrong with that request. Please try again.