From f1d5d1f4c0ea75ebeaf9e7eb760b9efd245a5df2 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Wed, 19 Mar 2014 06:24:30 +0100 Subject: [PATCH] Use expanded URIs when available Closes https://github.com/sferik/twitter/pull/543. --- lib/twitter/entities.rb | 6 +- lib/twitter/user.rb | 56 ++++++++++-- spec/fixtures/sferik.json | 2 +- spec/twitter/direct_message_spec.rb | 25 +++--- spec/twitter/entity/uri_spec.rb | 4 +- spec/twitter/media/photo_spec.rb | 8 +- spec/twitter/oembed_spec.rb | 6 +- spec/twitter/place_spec.rb | 2 +- spec/twitter/trend_spec.rb | 2 +- spec/twitter/tweet_spec.rb | 37 +++----- spec/twitter/user_spec.rb | 133 +++++++++++++++++++++++++--- 11 files changed, 211 insertions(+), 70 deletions(-) diff --git a/lib/twitter/entities.rb b/lib/twitter/entities.rb index d9510eeb5..7e7ef6cc1 100644 --- a/lib/twitter/entities.rb +++ b/lib/twitter/entities.rb @@ -11,7 +11,7 @@ module Entities # @return [Boolean] def entities? - !@attrs[:entities].nil? && @attrs[:entities].any? { |_, array| !array.empty? } + !@attrs[:entities].nil? && @attrs[:entities].any? { |_, array| array.any? } end memoize :entities? @@ -26,6 +26,7 @@ def hashtags def hashtags? hashtags.any? end + memoize :hashtags? # @note Must include entities in your request for this method to work # @return [Array] @@ -38,6 +39,7 @@ def media def media? media.any? end + memoize :media? # @note Must include entities in your request for this method to work # @return [Array] @@ -50,6 +52,7 @@ def symbols def symbols? symbols.any? end + memoize :symbols? # @note Must include entities in your request for this method to work # @return [Array] @@ -76,6 +79,7 @@ def user_mentions def user_mentions? user_mentions.any? end + memoize :user_mentions? private diff --git a/lib/twitter/user.rb b/lib/twitter/user.rb index 569e1acd2..a3dce1699 100644 --- a/lib/twitter/user.rb +++ b/lib/twitter/user.rb @@ -32,14 +32,50 @@ class User < Twitter::BasicUser alias_method :tweet?, :status? alias_method :tweeted?, :status? - # @return [Array] - def description_uris - Array(@attrs[:entities][:description][:urls]).collect do |entity| - Entity::URI.new(entity) + class << self + + private + + # Dynamically define a method for entity URIs + # + # @param key1 [Symbol] + # @param key2 [Symbol] + def define_entity_uris_methods(key1, key2) + array = key1.to_s.split('_') + index = array.index('uris') + array[index] = 'urls' + url_key = array.join('_').to_sym + define_entity_uris_method(key1, key2) + alias_method(url_key, key1) + define_entity_uris_predicate_method(key1) + alias_method(:"#{url_key}?", :"#{key1}?") + end + + def define_entity_uris_method(key1, key2) + define_method(key1) do || + Array(@attrs[:entities] && @attrs[:entities][key2] && @attrs[:entities][key2][:urls]).collect do |url| + Entity::URI.new(url) + end + end + memoize(key1) + end + + def define_entity_uris_predicate_method(key1) + define_method(:"#{key1}?") do || + send(:"#{key1}").any? + end + memoize(:"#{key1}?") end end - memoize :description_uris - alias_method :description_urls, :description_uris + + define_entity_uris_methods :description_uris, :description + define_entity_uris_methods :website_uris, :url + + # @return [Boolean] + def entities? + !@attrs[:entities].nil? && @attrs[:entities].any? { |_, hash| hash[:urls].any? } + end + memoize :entities? # @return [String] The URL to the user. def uri @@ -50,12 +86,16 @@ def uri # @return [String] The URL to the user's website. def website - Addressable::URI.parse(@attrs[:url]) unless @attrs[:url].nil? + if website_urls? + website_urls.first.expanded_url + elsif @attrs[:url] + Addressable::URI.parse(@attrs[:url]) + end end memoize :website def website? - !!@attrs[:url] + !!(website_uris? || @attrs[:url]) end memoize :website? end diff --git a/spec/fixtures/sferik.json b/spec/fixtures/sferik.json index 586872dd2..e61907547 100644 --- a/spec/fixtures/sferik.json +++ b/spec/fixtures/sferik.json @@ -1 +1 @@ -{"id":7505382,"id_str":"7505382","name":"Erik Michaels-Ober","screen_name":"sferik","location":"San Francisco","description":"An ingredient in your recipe.","url":"https:\/\/github.com\/sferik","entities":{"url":{"urls":[{"url":"https:\/\/github.com\/sferik","expanded_url":null,"indices":[0,25]}]},"description":{"urls":[]}},"protected":false,"followers_count":2449,"friends_count":203,"listed_count":130,"created_at":"Mon Jul 16 12:59:01 +0000 2007","favourites_count":4306,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":8554,"lang":"en","status":{"created_at":"Tue Oct 02 23:21:06 +0000 2012","id":253273430739283969,"id_str":"253273430739283969","text":"@dakami RAM drives are the new SSDs","source":"\u003ca href=\"http:\/\/itunes.apple.com\/us\/app\/twitter\/id409789998?mt=12\" rel=\"nofollow\"\u003eTwitter for Mac\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":253272860473298944,"in_reply_to_status_id_str":"253272860473298944","in_reply_to_user_id":8917142,"in_reply_to_user_id_str":"8917142","in_reply_to_screen_name":"dakami","geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"dakami","name":"Dan Kaminsky","id":8917142,"id_str":"8917142","indices":[0,7]}]},"favorited":false,"retweeted":false},"contributors_enabled":false,"is_translator":false,"profile_background_color":"000000","profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/665875854\/bb0b3653dcf0644e344823e0a2eb3382.png","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/665875854\/bb0b3653dcf0644e344823e0a2eb3382.png","profile_background_tile":false,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1759857427\/image1326743606_normal.png","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1759857427\/image1326743606_normal.png","profile_banner_url":"https:\/\/si0.twimg.com\/profile_banners\/7505382\/1348266581","profile_link_color":"0084B4","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false} \ No newline at end of file +{"id":7505382,"id_str":"7505382","name":"Erik Michaels-Ober","screen_name":"sferik","location":"","description":"May contain forward-looking statements.","url":"https:\/\/t.co\/L2xIBazMPf","entities":{"url":{"urls":[{"url":"https:\/\/t.co\/L2xIBazMPf","expanded_url":"https:\/\/github.com\/sferik","display_url":"github.com\/sferik","indices":[0,23]}]},"description":{"urls":[]}},"protected":false,"followers_count":4051,"friends_count":361,"listed_count":238,"created_at":"Mon Jul 16 12:59:01 +0000 2007","favourites_count":8976,"utc_offset":3600,"time_zone":"Berlin","geo_enabled":true,"verified":false,"statuses_count":13844,"lang":"en","status":{"created_at":"Wed Mar 19 00:50:35 +0000 2014","id":446086297866993665,"id_str":"446086297866993665","text":"RT @AntonWSJ: Okean Elzy, #Ukraine's biggest rock band, performed in Berlin tonight amid many \"Glory to Ukraine!\" chants http:\/\/t.co\/dkIF8X\u2026","source":"\u003ca href=\"http:\/\/itunes.apple.com\/us\/app\/twitter\/id409789998?mt=12\" rel=\"nofollow\"\u003eTwitter for Mac\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Tue Mar 18 23:56:08 +0000 2014","id":446072592227909632,"id_str":"446072592227909632","text":"Okean Elzy, #Ukraine's biggest rock band, performed in Berlin tonight amid many \"Glory to Ukraine!\" chants http:\/\/t.co\/dkIF8XvFyw","source":"\u003ca href=\"http:\/\/www.apple.com\" rel=\"nofollow\"\u003eiOS\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":4,"favorite_count":2,"entities":{"hashtags":[{"text":"Ukraine","indices":[12,20]}],"symbols":[],"urls":[],"user_mentions":[],"media":[{"id":446072592047538176,"id_str":"446072592047538176","indices":[107,129],"media_url":"http:\/\/pbs.twimg.com\/media\/BjDEqLkIAAAIpE4.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/BjDEqLkIAAAIpE4.jpg","url":"http:\/\/t.co\/dkIF8XvFyw","display_url":"pic.twitter.com\/dkIF8XvFyw","expanded_url":"http:\/\/twitter.com\/AntonWSJ\/status\/446072592227909632\/photo\/1","type":"photo","sizes":{"medium":{"w":600,"h":450,"resize":"fit"},"large":{"w":1024,"h":768,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":340,"h":255,"resize":"fit"}}}]},"favorited":true,"retweeted":true,"possibly_sensitive":false,"lang":"en"},"retweet_count":4,"favorite_count":0,"entities":{"hashtags":[{"text":"Ukraine","indices":[26,34]}],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"AntonWSJ","name":"Anton Troianovski","id":76773876,"id_str":"76773876","indices":[3,12]}],"media":[{"id":446072592047538176,"id_str":"446072592047538176","indices":[139,140],"media_url":"http:\/\/pbs.twimg.com\/media\/BjDEqLkIAAAIpE4.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/BjDEqLkIAAAIpE4.jpg","url":"http:\/\/t.co\/dkIF8XvFyw","display_url":"pic.twitter.com\/dkIF8XvFyw","expanded_url":"http:\/\/twitter.com\/AntonWSJ\/status\/446072592227909632\/photo\/1","type":"photo","sizes":{"medium":{"w":600,"h":450,"resize":"fit"},"large":{"w":1024,"h":768,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":340,"h":255,"resize":"fit"}}}]},"favorited":true,"retweeted":true,"possibly_sensitive":false,"lang":"en"},"contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"000000","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/677717672\/bb0b3653dcf0644e344823e0a2eb3382.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/677717672\/bb0b3653dcf0644e344823e0a2eb3382.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000757402331\/d8dfba561e2e94112a737f17e7d138ca_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000757402331\/d8dfba561e2e94112a737f17e7d138ca_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/7505382\/1385736840","profile_link_color":"0084B4","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"suspended":false,"needs_phone_verification":false} \ No newline at end of file diff --git a/spec/twitter/direct_message_spec.rb b/spec/twitter/direct_message_spec.rb index 67a468adb..65ad42db9 100644 --- a/spec/twitter/direct_message_spec.rb +++ b/spec/twitter/direct_message_spec.rb @@ -53,27 +53,26 @@ end describe '#entities?' do - it 'returns false if there are no entities set' do - tweet = Twitter::DirectMessage.new(:id => 1_825_786_345) - expect(tweet.entities?).to be false - end - - it 'returns false if there are blank lists of entities set' do - tweet = Twitter::DirectMessage.new(:id => 1_825_786_345, :entities => {:urls => []}) - expect(tweet.entities?).to be false - end it 'returns true if there are entities set' do urls_array = [ { - :url => 'http://example.com/t.co', + :url => 'https://t.co/L2xIBazMPf', :expanded_url => 'http://example.com/expanded', - :display_url => 'example.com/expandedâ¦', + :display_url => 'example.com/expanded…', :indices => [10, 33], } ] tweet = Twitter::DirectMessage.new(:id => 1_825_786_345, :entities => {:urls => urls_array}) expect(tweet.entities?).to be true end + it 'returns false if there are blank lists of entities set' do + tweet = Twitter::DirectMessage.new(:id => 1_825_786_345, :entities => {:urls => []}) + expect(tweet.entities?).to be false + end + it 'returns false if there are no entities set' do + tweet = Twitter::DirectMessage.new(:id => 1_825_786_345) + expect(tweet.entities?).to be false + end end describe '#recipient' do @@ -187,7 +186,7 @@ it 'returns an array of Entity::URIs when entities are set' do urls_array = [ { - :url => 'http://example.com/t.co', + :url => 'https://t.co/L2xIBazMPf', :expanded_url => 'http://example.com/expanded', :display_url => 'example.com/expanded…', :indices => [10, 33], @@ -211,7 +210,7 @@ it 'can handle strange urls' do urls_array = [ { - :url => 'http://with_underscore.example.com/t.co', + :url => 'https://t.co/L2xIBazMPf', :expanded_url => 'http://with_underscore.example.com/expanded', :display_url => 'with_underscore.example.com/expanded…', :indices => [10, 33], diff --git a/spec/twitter/entity/uri_spec.rb b/spec/twitter/entity/uri_spec.rb index c547e57c6..9b135ce23 100644 --- a/spec/twitter/entity/uri_spec.rb +++ b/spec/twitter/entity/uri_spec.rb @@ -29,7 +29,7 @@ describe '#expanded_uri' do it 'returns a URI when the expanded_url is set' do uri = Twitter::Entity::URI.new(:expanded_url => 'https://github.com/sferik') - expect(uri.expanded_uri).to be_a Addressable::URI + expect(uri.expanded_uri).to be_an Addressable::URI expect(uri.expanded_uri.to_s).to eq('https://github.com/sferik') end it 'returns nil when the expanded_url is not set' do @@ -52,7 +52,7 @@ describe '#uri' do it 'returns a URI when the url is set' do uri = Twitter::Entity::URI.new(:url => 'https://github.com/sferik') - expect(uri.uri).to be_a Addressable::URI + expect(uri.uri).to be_an Addressable::URI expect(uri.uri.to_s).to eq('https://github.com/sferik') end it 'returns nil when the url is not set' do diff --git a/spec/twitter/media/photo_spec.rb b/spec/twitter/media/photo_spec.rb index 71e0899ee..e0712b078 100644 --- a/spec/twitter/media/photo_spec.rb +++ b/spec/twitter/media/photo_spec.rb @@ -59,7 +59,7 @@ describe '#expanded_uri' do it 'returns a URI when the expanded_url is set' do photo = Twitter::Media::Photo.new(:id => 1, :expanded_url => 'http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png') - expect(photo.expanded_uri).to be_a Addressable::URI + expect(photo.expanded_uri).to be_an Addressable::URI expect(photo.expanded_uri.to_s).to eq('http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png') end it 'returns nil when the expanded_url is not set' do @@ -82,7 +82,7 @@ describe '#media_uri' do it 'returns a URI when the media_url is set' do photo = Twitter::Media::Photo.new(:id => 1, :media_url => 'http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png') - expect(photo.media_uri).to be_a Addressable::URI + expect(photo.media_uri).to be_an Addressable::URI expect(photo.media_uri.to_s).to eq('http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png') end it 'returns nil when the media_url is not set' do @@ -105,7 +105,7 @@ describe '#media_uri_https' do it 'returns a URI when the media_url_https is set' do photo = Twitter::Media::Photo.new(:id => 1, :media_url_https => 'http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png') - expect(photo.media_uri_https).to be_a Addressable::URI + expect(photo.media_uri_https).to be_an Addressable::URI expect(photo.media_uri_https.to_s).to eq('http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png') end it 'returns nil when the media_url_https is not set' do @@ -128,7 +128,7 @@ describe '#uri' do it 'returns a URI when the url is set' do photo = Twitter::Media::Photo.new(:id => 1, :url => 'http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png') - expect(photo.uri).to be_a Addressable::URI + expect(photo.uri).to be_an Addressable::URI expect(photo.uri.to_s).to eq('http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png') end it 'returns nil when the url is not set' do diff --git a/spec/twitter/oembed_spec.rb b/spec/twitter/oembed_spec.rb index db70230bc..7329e61ac 100644 --- a/spec/twitter/oembed_spec.rb +++ b/spec/twitter/oembed_spec.rb @@ -5,7 +5,7 @@ describe '#author_uri' do it 'returns a URI when the author_url is set' do oembed = Twitter::OEmbed.new(:author_url => 'https://twitter.com/sferik') - expect(oembed.author_uri).to be_a Addressable::URI + expect(oembed.author_uri).to be_an Addressable::URI expect(oembed.author_uri.to_s).to eq('https://twitter.com/sferik') end it 'returns nil when the author_uri is not set' do @@ -87,7 +87,7 @@ describe '#provider_uri' do it 'returns a URI when the provider_url is set' do oembed = Twitter::OEmbed.new(:provider_url => 'http://twitter.com') - expect(oembed.provider_uri).to be_a Addressable::URI + expect(oembed.provider_uri).to be_an Addressable::URI expect(oembed.provider_uri.to_s).to eq('http://twitter.com') end it 'returns nil when the provider_uri is not set' do @@ -136,7 +136,7 @@ describe '#uri' do it 'returns a URI when the url is set' do oembed = Twitter::OEmbed.new(:url => 'https://twitter.com/twitterapi/status/133640144317198338') - expect(oembed.uri).to be_a Addressable::URI + expect(oembed.uri).to be_an Addressable::URI expect(oembed.uri.to_s).to eq('https://twitter.com/twitterapi/status/133640144317198338') end it 'returns nil when the url is not set' do diff --git a/spec/twitter/place_spec.rb b/spec/twitter/place_spec.rb index e828da59c..6cb97f7b6 100644 --- a/spec/twitter/place_spec.rb +++ b/spec/twitter/place_spec.rb @@ -137,7 +137,7 @@ describe '#uri' do it 'returns a URI when the url is set' do place = Twitter::Place.new(:woeid => '247f43d441defc03', :url => 'https://api.twitter.com/1.1/geo/id/247f43d441defc03.json') - expect(place.uri).to be_a Addressable::URI + expect(place.uri).to be_an Addressable::URI expect(place.uri.to_s).to eq('https://api.twitter.com/1.1/geo/id/247f43d441defc03.json') end it 'returns nil when the url is not set' do diff --git a/spec/twitter/trend_spec.rb b/spec/twitter/trend_spec.rb index f6461f1be..ea0be71c9 100644 --- a/spec/twitter/trend_spec.rb +++ b/spec/twitter/trend_spec.rb @@ -28,7 +28,7 @@ describe '#uri' do it 'returns a URI when the url is set' do trend = Twitter::Trend.new(:url => 'http://twitter.com/search/?q=%23sevenwordsaftersex') - expect(trend.uri).to be_a Addressable::URI + expect(trend.uri).to be_an Addressable::URI expect(trend.uri.to_s).to eq('http://twitter.com/search/?q=%23sevenwordsaftersex') end it 'returns nil when the url is not set' do diff --git a/spec/twitter/tweet_spec.rb b/spec/twitter/tweet_spec.rb index 003acad0f..fda79e3d5 100644 --- a/spec/twitter/tweet_spec.rb +++ b/spec/twitter/tweet_spec.rb @@ -53,27 +53,26 @@ end describe '#entities?' do - it 'returns false if there are no entities set' do - tweet = Twitter::Tweet.new(:id => 28_669_546_014) - expect(tweet.entities?).to be false - end - - it 'returns false if there are blank lists of entities set' do - tweet = Twitter::Tweet.new(:id => 28_669_546_014, :entities => {:urls => []}) - expect(tweet.entities?).to be false - end it 'returns true if there are entities set' do urls_array = [ { - :url => 'http://example.com/t.co', + :url => 'https://t.co/L2xIBazMPf', :expanded_url => 'http://example.com/expanded', - :display_url => 'example.com/expandedâ¦', + :display_url => 'example.com/expanded…', :indices => [10, 33], } ] tweet = Twitter::Tweet.new(:id => 28_669_546_014, :entities => {:urls => urls_array}) expect(tweet.entities?).to be true end + it 'returns false if there are blank lists of entities set' do + tweet = Twitter::Tweet.new(:id => 28_669_546_014, :entities => {:urls => []}) + expect(tweet.entities?).to be false + end + it 'returns false if there are no entities set' do + tweet = Twitter::Tweet.new(:id => 28_669_546_014) + expect(tweet.entities?).to be false + end end describe '#filter_level' do @@ -140,11 +139,9 @@ :indices => [10, 33], }] end - let(:subject) do Twitter::Tweet.new(:id => 28_669_546_014, :entities => {:hashtags => hashtags_array}) end - it 'returns an array of Entity::Hashtag' do hashtags = subject.hashtags expect(hashtags).to be_an Array @@ -153,27 +150,21 @@ expect(hashtags.first.text).to eq('twitter') end end - context 'when entities are set, but empty' do subject { Twitter::Tweet.new(:id => 28_669_546_014, :entities => {:hashtags => []}) } - it 'is empty' do expect(subject.hashtags).to be_empty end - it 'does not warn' do subject.hashtags expect($stderr.string).to be_empty end end - context 'when entities are not set' do subject { Twitter::Tweet.new(:id => 28_669_546_014) } - it 'is empty' do expect(subject.hashtags).to be_empty end - it 'warns' do subject.hashtags expect($stderr.string).to match(/To get hashtags, you must pass `:include_entities => true` when requesting the Twitter::Tweet\./) @@ -355,7 +346,7 @@ it 'returns an array of Entity::URIs when entities are set' do urls_array = [ { - :url => 'http://example.com/t.co', + :url => 'https://t.co/L2xIBazMPf', :expanded_url => 'http://example.com/expanded', :display_url => 'example.com/expanded…', :indices => [10, 33], @@ -379,7 +370,7 @@ it 'can handle strange urls' do urls_array = [ { - :url => 'http://with_underscore.example.com/t.co', + :url => 'https://t.co/L2xIBazMPf', :expanded_url => 'http://with_underscore.example.com/expanded', :display_url => 'with_underscore.example.com/expanded…', :indices => [10, 33], @@ -396,7 +387,7 @@ describe '#uri' do it 'returns the URI to the tweet' do tweet = Twitter::Tweet.new(:id => 28_669_546_014, :user => {:id => 7_505_382, :screen_name => 'sferik'}) - expect(tweet.uri).to be_a Addressable::URI + expect(tweet.uri).to be_an Addressable::URI expect(tweet.uri.to_s).to eq('https://twitter.com/sferik/status/28669546014') end end @@ -404,7 +395,7 @@ describe '#uris?' do it 'returns true when the tweet includes urls entities' do entities = { - :urls => [{:url => 'http://with_underscore.example.com/t.co'}] + :urls => [{:url => 'https://t.co/L2xIBazMPf'}] } tweet = Twitter::Tweet.new(:id => 28_669_546_014, :entities => entities) expect(tweet.uris?).to be true diff --git a/spec/twitter/user_spec.rb b/spec/twitter/user_spec.rb index fa9bc31b5..99a937600 100644 --- a/spec/twitter/user_spec.rb +++ b/spec/twitter/user_spec.rb @@ -44,10 +44,10 @@ end describe '#description_uris' do - it 'returns an array of Entity::URIs when entities are set' do + it 'returns an array of Entity::URIs when description URI entities are set' do urls_array = [ { - :url => 'http://example.com/t.co', + :url => 'https://t.co/L2xIBazMPf', :expanded_url => 'http://example.com/expanded', :display_url => 'example.com/expanded…', :indices => [10, 33], @@ -57,7 +57,7 @@ expect(user.description_uris).to be_an Array expect(user.description_uris.first).to be_a Twitter::Entity::URI expect(user.description_uris.first.indices).to eq([10, 33]) - expect(user.description_uris.first.display_uri).to eq('example.com/expanded…') + expect(user.description_uris.first.expanded_uri).to be_an Addressable::URI end it 'is empty when not set' do user = Twitter::User.new(:id => 7_505_382, :entities => {:description => {:urls => []}}) @@ -65,14 +65,56 @@ end end + describe '#description_uris?' do + it 'returns true when the tweet includes description URI entities' do + urls_array = [ + { + :url => 'https://t.co/L2xIBazMPf', + :expanded_url => 'http://example.com/expanded', + :display_url => 'example.com/expanded…', + :indices => [10, 33], + } + ] + user = Twitter::User.new(:id => 7_505_382, :entities => {:description => {:urls => urls_array}}) + expect(user.description_uris?).to be true + end + it 'returns false when no entities are present' do + user = Twitter::User.new(:id => 7_505_382, :entities => {:description => {:urls => []}}) + expect(user.description_uris?).to be false + end + end + + describe '#entities?' do + it 'returns true if there are entities set' do + urls_array = [ + { + :url => 'https://t.co/L2xIBazMPf', + :expanded_url => 'http://example.com/expanded', + :display_url => 'example.com/expanded…', + :indices => [10, 33], + } + ] + user = Twitter::User.new(:id => 7_505_382, :entities => {:description => {:urls => urls_array}}) + expect(user.entities?).to be true + end + it 'returns false if there are blank lists of entities set' do + user = Twitter::User.new(:id => 7_505_382, :entities => {:description => {:urls => []}}) + expect(user.entities?).to be false + end + it 'returns false if there are no entities set' do + user = Twitter::User.new(:id => 7_505_382) + expect(user.entities?).to be false + end + end + describe '#profile_banner_uri' do it 'accepts utf8 urls' do user = Twitter::User.new(:id => 7_505_382, :profile_banner_url => 'https://si0.twimg.com/profile_banners/7_505_382/1348266581©_normal.png') - expect(user.profile_banner_uri).to be_a Addressable::URI + expect(user.profile_banner_uri).to be_an Addressable::URI end it 'returns a URI when profile_banner_url is set' do user = Twitter::User.new(:id => 7_505_382, :profile_banner_url => 'https://si0.twimg.com/profile_banners/7_505_382/1348266581') - expect(user.profile_banner_uri).to be_a Addressable::URI + expect(user.profile_banner_uri).to be_an Addressable::URI end it 'returns nil when profile_banner_uri is not set' do user = Twitter::User.new(:id => 7_505_382) @@ -117,11 +159,11 @@ describe '#profile_banner_uri_https' do it 'accepts utf8 urls' do user = Twitter::User.new(:id => 7_505_382, :profile_banner_url => 'https://si0.twimg.com/profile_banners/7_505_382/1348266581©_normal.png') - expect(user.profile_banner_uri_https).to be_a Addressable::URI + expect(user.profile_banner_uri_https).to be_an Addressable::URI end it 'returns a URI when profile_banner_url is set' do user = Twitter::User.new(:id => 7_505_382, :profile_banner_url => 'https://si0.twimg.com/profile_banners/7_505_382/1348266581') - expect(user.profile_banner_uri_https).to be_a Addressable::URI + expect(user.profile_banner_uri_https).to be_an Addressable::URI end it 'returns nil when created_at is not set' do user = Twitter::User.new(:id => 7_505_382) @@ -177,11 +219,11 @@ describe '#profile_image_uri' do it 'accepts utf8 urls' do user = Twitter::User.new(:id => 7_505_382, :profile_image_url_https => 'https://si0.twimg.com/profile_images/7_505_382/1348266581©_normal.png') - expect(user.profile_image_uri).to be_a Addressable::URI + expect(user.profile_image_uri).to be_an Addressable::URI end it 'returns a URI when profile_image_url_https is set' do user = Twitter::User.new(:id => 7_505_382, :profile_image_url_https => 'https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png') - expect(user.profile_image_uri).to be_a Addressable::URI + expect(user.profile_image_uri).to be_an Addressable::URI end it 'returns nil when created_at is not set' do user = Twitter::User.new(:id => 7_505_382) @@ -222,11 +264,11 @@ describe '#profile_image_uri_https' do it 'accepts utf8 urls' do user = Twitter::User.new(:id => 7_505_382, :profile_image_url_https => 'https://si0.twimg.com/profile_images/7_505_382/1348266581©_normal.png') - expect(user.profile_image_uri_https).to be_a Addressable::URI + expect(user.profile_image_uri_https).to be_an Addressable::URI end it 'returns a URI when profile_image_url_https is set' do user = Twitter::User.new(:id => 7_505_382, :profile_image_url_https => 'https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png') - expect(user.profile_image_uri_https).to be_a Addressable::URI + expect(user.profile_image_uri_https).to be_an Addressable::URI end it 'returns nil when created_at is not set' do user = Twitter::User.new(:id => 7_505_382) @@ -305,7 +347,7 @@ describe '#uri' do it 'returns the URI to the user' do user = Twitter::User.new(:id => 7_505_382, :screen_name => 'sferik') - expect(user.uri).to be_a Addressable::URI + expect(user.uri).to be_an Addressable::URI expect(user.uri.to_s).to eq('https://twitter.com/sferik') end end @@ -313,9 +355,22 @@ describe '#website' do it 'returns a URI when the url is set' do user = Twitter::User.new(:id => 7_505_382, :url => 'https://github.com/sferik') - expect(user.website).to be_a Addressable::URI + expect(user.website).to be_an Addressable::URI expect(user.website.to_s).to eq('https://github.com/sferik') end + it 'returns a URI when the tweet includes website URI entities' do + urls_array = [ + { + :url => 'https://t.co/L2xIBazMPf', + :expanded_url => 'http://example.com/expanded', + :display_url => 'example.com/expanded…', + :indices => [0, 23], + } + ] + user = Twitter::User.new(:id => 7_505_382, :entities => {:url => {:urls => urls_array}}) + expect(user.website).to be_an Addressable::URI + expect(user.website.to_s).to eq('http://example.com/expanded') + end it 'returns nil when the url is not set' do user = Twitter::User.new(:id => 7_505_382) expect(user.website).to be_nil @@ -327,10 +382,62 @@ user = Twitter::User.new(:id => 7_505_382, :url => 'https://github.com/sferik') expect(user.website?).to be true end + it 'returns true when the tweet includes website URI entities' do + urls_array = [ + { + :url => 'https://t.co/L2xIBazMPf', + :expanded_url => 'http://example.com/expanded', + :display_url => 'example.com/expanded…', + :indices => [0, 23], + } + ] + user = Twitter::User.new(:id => 7_505_382, :entities => {:url => {:urls => urls_array}}) + expect(user.website?).to be true + end it 'returns false when the url is not set' do user = Twitter::User.new(:id => 7_505_382) expect(user.website?).to be false end end + describe '#website_uris' do + it 'returns an array of Entity::URIs when website URI entities are set' do + urls_array = [ + { + :url => 'https://t.co/L2xIBazMPf', + :expanded_url => 'http://example.com/expanded', + :display_url => 'example.com/expanded…', + :indices => [0, 23], + } + ] + user = Twitter::User.new(:id => 7_505_382, :entities => {:url => {:urls => urls_array}}) + expect(user.website_uris).to be_an Array + expect(user.website_uris.first).to be_a Twitter::Entity::URI + expect(user.website_uris.first.indices).to eq([0, 23]) + expect(user.website_uris.first.expanded_uri).to be_an Addressable::URI + end + it 'is empty when not set' do + user = Twitter::User.new(:id => 7_505_382, :entities => {:url => {:urls => []}}) + expect(user.website_uris).to be_empty + end + end + + describe '#website_uris?' do + it 'returns true when the tweet includes website URI entities' do + urls_array = [ + { + :url => 'https://t.co/L2xIBazMPf', + :expanded_url => 'http://example.com/expanded', + :display_url => 'example.com/expanded…', + :indices => [0, 23], + } + ] + user = Twitter::User.new(:id => 7_505_382, :entities => {:url => {:urls => urls_array}}) + expect(user.website_uris?).to be true + end + it 'returns false when no entities are present' do + user = Twitter::User.new(:id => 7_505_382, :entities => {}) + expect(user.website_uris?).to be false + end + end end