Skip to content

Commit

Permalink
Merge pull request #245 from tomykaira/status_entities
Browse files Browse the repository at this point in the history
Add entities attr_reader to Twitter::Status
  • Loading branch information
sferik committed Mar 27, 2012
2 parents 469d452 + 2d20f23 commit 32375d9
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/twitter/entity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'twitter/base'

module Twitter
class Entity < Twitter::Base
lazy_attr_reader :indices
end
end
7 changes: 7 additions & 0 deletions lib/twitter/entity/hashtag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'twitter/entity'

module Twitter
class Entity::Hashtag < Twitter::Entity
lazy_attr_reader :text
end
end
7 changes: 7 additions & 0 deletions lib/twitter/entity/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'twitter/entity'

module Twitter
class Entity::Url < Twitter::Entity
lazy_attr_reader :display_url, :expanded_url, :url
end
end
7 changes: 7 additions & 0 deletions lib/twitter/entity/user_mention.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'twitter/entity'

module Twitter
class Entity::UserMention < Twitter::Entity
lazy_attr_reader :screen_name, :name, :id, :id_str
end
end
48 changes: 48 additions & 0 deletions lib/twitter/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@
require 'twitter/oembed'
require 'twitter/place'
require 'twitter/user'
require 'twitter/entity/url'
require 'twitter/entity/user_mention'
require 'twitter/entity/hashtag'

module Twitter
class Status < Twitter::Base
class NoEntityError < StandardError
def initialize
super("Entities are not available. Pass :include_entities => true on fetching the Twitter::Status object")
end
end

include Twitter::Creatable
lazy_attr_reader :favorited, :from_user, :from_user_id, :from_user_name, :id,
:in_reply_to_screen_name, :in_reply_to_attrs_id, :in_reply_to_status_id,
Expand Down Expand Up @@ -76,5 +85,44 @@ def oembed(options={})
@client.oembed(@attrs['id'], options) unless @attrs['id'].nil?
end

# @note Must :include_entities in your request for this method to work
# @return [Array<Twitter::Entity::Url>]
# @raise [Twitter::Status::NoEntityError] Error raised when entities are not available
def urls
@urls ||= if @attrs['entities'].nil?
raise NoEntityError
else
Array(@attrs['entities']['urls']).map do |url|
Twitter::Entity::Url.new(url)
end
end
end

# @note Must :include_entities in your request for this method to work
# @return [Array<Twitter::Entity::Hashtag>]
# @raise [Twitter::Status::NoEntityError] Error raised when entities are not available
def hashtags
@hashtags ||= if @attrs['entities'].nil?
raise NoEntityError
else
Array(@attrs['entities']['hashtags']).map do |hashtag|
Twitter::Entity::Hashtag.new(hashtag)
end
end
end

# @note Must :include_entities in your request for this method to work
# @return [Array<Twitter::Entity::UserMention>]
# @raise [Twitter::Status::NoEntityError] Error raised when entities are not available
def user_mentions
@user_mentions ||= if @attrs['entities'].nil?
raise NoEntityError
else
Array(@attrs['entities']['user_mentions']).map do |user_mention|
Twitter::Entity::UserMention.new(user_mention)
end
end
end

end
end
50 changes: 50 additions & 0 deletions spec/twitter/status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,54 @@
end
end

describe "#urls" do
it "should return an Array of Entity::Url when entities are set" do
urls_hash = [{'url' => 'http://example.com/t.co',
'expanded_url' => 'http://example.com/expanded',
'display_url' => 'example.com/expanded',
'indices' => [10, 33]}]
urls = Twitter::Status.new('entities' => {'urls' => urls_hash}).urls
urls.should be_an Array
urls.first.should be_an Twitter::Entity::Url
urls.first.indices.should == [10, 33]
urls.first.display_url.should == 'example.com/expanded'
end
it "should raise NoEntityError when entities are not set" do
expect { Twitter::Status.new.urls }.to raise_error(Twitter::Status::NoEntityError)
end
end

describe "#hashtags" do
it "should return an Array of Entity::Hashtag when entities are set" do
hashtags_hash = [{'text' => 'twitter',
'indices' => [10, 33]}]
hashtags = Twitter::Status.new('entities' => {'hashtags' => hashtags_hash}).hashtags
hashtags.should be_an Array
hashtags.first.should be_an Twitter::Entity::Hashtag
hashtags.first.indices.should == [10, 33]
hashtags.first.text.should == 'twitter'
end
it "should raise NoEntityError when entities are not set" do
expect { Twitter::Status.new.hashtags }.to raise_error(Twitter::Status::NoEntityError)
end
end

describe "#user_mentions" do
it "should return an Array of Entity::User_Mention when entities are set" do
user_mentions_hash = [{'screen_name'=>'sferik',
'name'=>'Erik Michaels-Ober',
'id_str'=>'7505382',
'indices'=>[0, 6],
'id'=>7505382}]
user_mentions = Twitter::Status.new('entities' => {'user_mentions' => user_mentions_hash}).user_mentions
user_mentions.should be_an Array
user_mentions.first.should be_an Twitter::Entity::UserMention
user_mentions.first.indices.should == [0, 6]
user_mentions.first.screen_name.should == 'sferik'
end
it "should raise NoEntityError when entities are not set" do
expect { Twitter::Status.new.user_mentions }.to raise_error(Twitter::Status::NoEntityError)
end
end

end

0 comments on commit 32375d9

Please sign in to comment.