Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add entities attr_reader to Twitter::Status #245

Merged
merged 5 commits into from
Mar 27, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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