diff --git a/lib/twitter.rb b/lib/twitter.rb index 7e2a2b2f4..99e23ea6e 100644 --- a/lib/twitter.rb +++ b/lib/twitter.rb @@ -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' diff --git a/lib/twitter/entity/symbol.rb b/lib/twitter/entity/symbol.rb new file mode 100644 index 000000000..e24cc771d --- /dev/null +++ b/lib/twitter/entity/symbol.rb @@ -0,0 +1,9 @@ +require 'twitter/entity' + +module Twitter + class Entity + class Symbol < Twitter::Entity + attr_reader :text + end + end +end diff --git a/lib/twitter/tweet.rb b/lib/twitter/tweet.rb index 85390c81a..77e7aa9aa 100644 --- a/lib/twitter/tweet.rb +++ b/lib/twitter/tweet.rb @@ -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] + def symbols + @symbols ||= entities(Twitter::Entity::Symbol, :symbols) + end + # @note Must include entities in your request for this method to work # @return [Array] def urls diff --git a/spec/twitter/tweet_spec.rb b/spec/twitter/tweet_spec.rb index cfe30ec39..527132b58 100644 --- a/spec/twitter/tweet_spec.rb +++ b/spec/twitter/tweet_spec.rb @@ -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') @@ -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