Skip to content

Commit

Permalink
Add support for tweet entities
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Oct 12, 2011
1 parent 5f53536 commit 5216710
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/twitter/status.rb
Expand Up @@ -3,6 +3,7 @@
require 'twitter/geo_factory'
require 'twitter/place'
require 'twitter/user'
require 'twitter-text'

module Twitter
class Status < Twitter::Base
Expand All @@ -22,13 +23,30 @@ def geo
Twitter::GeoFactory.new(@geo) if @geo
end

def hashtags
Twitter::Extractor.extract_hashtags(@text) if @text
end

def media
@entities['media'] if @entities
end

def place
Twitter::Place.new(@place) if @place
end

def urls
Twitter::Extractor.extract_urls(@text) if @text
end

def user
Twitter::User.new(@user.merge(:status => self.to_hash.delete(:user))) if @user
end

def user_mentions
Twitter::Extractor.extract_mentioned_screen_names(@text) if @text
end
alias :mentions :user_mentions

end
end
2 changes: 1 addition & 1 deletion spec/twitter/client/tweets_spec.rb
Expand Up @@ -67,7 +67,7 @@
status.text.should include("You always have options")
end

pending 'should return the media as an entity' do
it 'should return the media as an entity' do
status = @client.update_with_media("You always have options", fixture("me.jpeg"))
status.media.should be
end
Expand Down
60 changes: 60 additions & 0 deletions spec/twitter/status_spec.rb
Expand Up @@ -46,6 +46,36 @@

end

describe "#hashtags" do

it "should return hashtags" do
hashtags = Twitter::Status.new(:text => "This tweet contains a #hashtag.").hashtags
hashtags.should be_an Array
hashtags.first.should == "hashtag"
end

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

end

describe "#media" do

it "should return media" do
media = Twitter::Status.new(:entities => {'media' => [{'type' => 'photo'}]}).media
media.should be_an Array
media.first['type'].should == 'photo'
end

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

end

describe "#place" do

it "should return a Twitter::Place when set" do
Expand All @@ -60,6 +90,21 @@

end

describe "#url" do

it "should return urls" do
urls = Twitter::Status.new(:text => "This tweet contains a http://example.com.").urls
urls.should be_an Array
urls.first.should == "http://example.com"
end

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

end

describe "#user" do

it "should return a User when user is set" do
Expand All @@ -74,4 +119,19 @@

end

describe "#user_mentions" do

it "should return urls" do
user_mentions = Twitter::Status.new(:text => "This tweet contains a @mention.").user_mentions
user_mentions.should be_an Array
user_mentions.first.should == "mention"
end

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

end

end
1 change: 1 addition & 0 deletions twitter.gemspec
Expand Up @@ -5,6 +5,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'faraday', '~> 0.7.4'
gem.add_dependency 'multi_json', '~> 1.0.0'
gem.add_dependency 'simple_oauth', '~> 0.1.5'
gem.add_dependency 'twitter-text', '~> 1.4.2'
gem.add_development_dependency 'json', '~> 1.6'
gem.add_development_dependency 'rake', '~> 0.9'
gem.add_development_dependency 'rdiscount', '~> 1.6'
Expand Down

0 comments on commit 5216710

Please sign in to comment.