Skip to content

Commit

Permalink
added support for Symbol entities in Tweets
Browse files Browse the repository at this point in the history
  • Loading branch information
stve committed Apr 26, 2013
1 parent c890e7d commit a14a0cd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'twitter/direct_message'
require 'twitter/entity'
require 'twitter/entity/hashtag'
require 'twitter/entity/symbol'
require 'twitter/entity/url'
require 'twitter/entity/user_mention'
require 'twitter/geo_factory'
Expand Down
9 changes: 9 additions & 0 deletions lib/twitter/entity/symbol.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'twitter/entity'

module Twitter
class Entity
class Symbol < Twitter::Entity
attr_reader :text
end
end
end
6 changes: 6 additions & 0 deletions lib/twitter/tweet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ def retweeters_count
end
alias retweet_count retweeters_count

# @note Must include entities in your request for this method to work
# @return [Array<Twitter::Entity::Symbol>]
def symbols
@symbols ||= entities(Twitter::Entity::Symbol, :symbols)
end

# @note Must include entities in your request for this method to work
# @return [Array<Twitter::Entity::Url>]
def urls
Expand Down
55 changes: 39 additions & 16 deletions spec/twitter/tweet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@
end
end

describe "#entities?" do
it "returns false if there are no entities set" do
tweet = Twitter::Tweet.new(:id => 28669546014)
expect(tweet.entities?).to be_false
end

it "returns true if there are entities set" do
urls_array = [
{
:url => 'http://example.com/t.co',
:expanded_url => 'http://example.com/expanded',
:display_url => 'example.com/expanded',
:indices => [10, 33],
}
]
tweet = Twitter::Tweet.new(:id => 28669546014, :entities => {:urls => urls_array})
expect(tweet.entities?).to be_true
end
end

describe "#favoriters_count" do
it "returns the count of favoriters when favoriters_count is set" do
tweet = Twitter::Tweet.new(:id => 28669546014, :favoriters_count => '1')
Expand Down Expand Up @@ -239,23 +259,26 @@
end
end

describe "#entities?" do
it "returns false if there are no entities set" do
tweet = Twitter::Tweet.new(:id => 28669546014)
expect(tweet.entities?).to be_false
end

it "returns true if there are entities set" do
urls_array = [
{
:url => 'http://example.com/t.co',
:expanded_url => 'http://example.com/expanded',
:display_url => 'example.com/expanded',
:indices => [10, 33],
}
describe "#symbols" do
it "returns an Array of Entity::Symbol when symbols are set" do
symbols_array = [
{ :text => 'PEP', :indices => [114, 118] },
{ :text => 'COKE', :indices => [128, 133] }
]
tweet = Twitter::Tweet.new(:id => 28669546014, :entities => {:urls => urls_array})
expect(tweet.entities?).to be_true
symbols = Twitter::Tweet.new(:id => 28669546014, :entities => {:symbols => symbols_array}).symbols
expect(symbols).to be_an Array
expect(symbols.size).to eq 2
expect(symbols.first).to be_a Twitter::Entity::Symbol
expect(symbols.first.indices).to eq [114, 118]
expect(symbols.first.text).to eq 'PEP'
end
it "is empty when not set" do
symbols = Twitter::Tweet.new(:id => 28669546014).symbols
expect(symbols).to be_empty
end
it "warns when not set" do
Twitter::Tweet.new(:id => 28669546014).symbols
expect($stderr.string).to match(/To get symbols, you must pass `:include_entities => true` when requesting the Twitter::Tweet\./)
end
end

Expand Down

1 comment on commit a14a0cd

@philspitler
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a small stake in that do you ;)

Please sign in to comment.