Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add User#description_urls

Instead of providing direct access to entities, expose a method that
returns Twitter::Entity::Url objects, similar to how entities are
accessed on Status objects.

Closes #350.
  • Loading branch information...
commit 19c176cad72507be1f656f7b4a2d0421ae201d63 1 parent 0b4d2e5
@sferik authored
View
4 lib/twitter/tweet.rb
@@ -138,8 +138,8 @@ def user?
# @param key [Symbol]
def entities(klass, key)
if entities?
- Array(@attrs[:entities][key.to_sym]).map do |user_mention|
- klass.fetch_or_new(user_mention)
+ Array(@attrs[:entities][key.to_sym]).map do |entity|
+ klass.fetch_or_new(entity)
end
else
warn "#{Kernel.caller.first}: To get #{key.to_s.tr('_', ' ')}, you must pass `:include_entities => true` when requesting the #{self.class.name}."
View
7 lib/twitter/user.rb
@@ -32,6 +32,13 @@ class User < Twitter::BasicUser
alias update_count statuses_count
alias updates_count statuses_count
+ # @return [Array<Twitter::Entity::Url>]
+ def description_urls
+ @description_urls ||= Array(@attrs[:entities][:description][:urls]).map do |entity|
+ Twitter::Entity::Url.fetch_or_new(entity)
+ end
+ end
+
# Return the URL to the user's profile banner image
#
# @param size [String, Symbol] The size of the image. Must be one of: 'mobile', 'mobile_retina', 'web', 'web_retina', 'ipad', or 'ipad_retina'
View
16 spec/twitter/tweet_spec.rb
@@ -104,13 +104,13 @@
describe "#hashtags" do
it "returns an Array of Entity::Hashtag when entities are set" do
- hashtags_hash = [
+ hashtags_array = [
{
:text => 'twitter',
:indices => [10, 33],
}
]
- hashtags = Twitter::Tweet.new(:id => 28669546014, :entities => {:hashtags => hashtags_hash}).hashtags
+ hashtags = Twitter::Tweet.new(:id => 28669546014, :entities => {:hashtags => hashtags_array}).hashtags
expect(hashtags).to be_an Array
expect(hashtags.first).to be_a Twitter::Entity::Hashtag
expect(hashtags.first.indices).to eq [10, 33]
@@ -234,7 +234,7 @@
end
it "returns true if there are entities set" do
- urls_hash = [
+ urls_array = [
{
:url => 'http://example.com/t.co',
:expanded_url => 'http://example.com/expanded',
@@ -242,14 +242,14 @@
:indices => [10, 33],
}
]
- tweet = Twitter::Tweet.new(:id => 28669546014, :entities => {:urls => urls_hash})
+ tweet = Twitter::Tweet.new(:id => 28669546014, :entities => {:urls => urls_array})
expect(tweet.entities?).to be_true
end
end
describe "#urls" do
it "returns an Array of Entity::Url when entities are set" do
- urls_hash = [
+ urls_array = [
{
:url => 'http://example.com/t.co',
:expanded_url => 'http://example.com/expanded',
@@ -257,7 +257,7 @@
:indices => [10, 33],
}
]
- urls = Twitter::Tweet.new(:id => 28669546014, :entities => {:urls => urls_hash}).urls
+ urls = Twitter::Tweet.new(:id => 28669546014, :entities => {:urls => urls_array}).urls
expect(urls).to be_an Array
expect(urls.first).to be_a Twitter::Entity::Url
expect(urls.first.indices).to eq [10, 33]
@@ -301,7 +301,7 @@
describe "#user_mentions" do
it "returns an Array of Entity::UserMention when entities are set" do
- user_mentions_hash = [
+ user_mentions_array = [
{
:screen_name => 'sferik',
:name => 'Erik Michaels-Ober',
@@ -310,7 +310,7 @@
:id => 7505382,
}
]
- user_mentions = Twitter::Tweet.new(:id => 28669546014, :entities => {:user_mentions => user_mentions_hash}).user_mentions
+ user_mentions = Twitter::Tweet.new(:id => 28669546014, :entities => {:user_mentions => user_mentions_array}).user_mentions
expect(user_mentions).to be_an Array
expect(user_mentions.first).to be_a Twitter::Entity::UserMention
expect(user_mentions.first.indices).to eq [0, 6]
View
22 spec/twitter/user_spec.rb
@@ -31,6 +31,28 @@
end
end
+ describe "#description_urls" do
+ it "returns an Array of Entity::Url" do
+ urls_array = [
+ {
+ :url => 'http://example.com/t.co',
+ :expanded_url => 'http://example.com/expanded',
+ :display_url => 'example.com/expanded',
+ :indices => [10, 33],
+ }
+ ]
+ description_urls = Twitter::User.new(:id => 7505382, :entities => {:description => {:urls => urls_array}}).description_urls
+ expect(description_urls).to be_an Array
+ expect(description_urls.first).to be_a Twitter::Entity::Url
+ expect(description_urls.first.indices).to eq [10, 33]
+ expect(description_urls.first.display_url).to eq 'example.com/expanded'
+ end
+ it "is empty when not set" do
+ description_urls = Twitter::User.new(:id => 7505382, :entities => {:description => {:urls => []}}).description_urls
+ expect(description_urls).to be_empty
+ end
+ end
+
describe "#profile_banner_url" do
it "returns a String when profile_banner_url is set" do
user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581")
Please sign in to comment.
Something went wrong with that request. Please try again.