diff --git a/lib/twitter/user.rb b/lib/twitter/user.rb index e91b1d701..dbee2bfa9 100644 --- a/lib/twitter/user.rb +++ b/lib/twitter/user.rb @@ -41,18 +41,33 @@ class User < Twitter::BasicUser alias updates_count statuses_count alias verified? verified + # 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' + # @return [String] + def profile_banner_url(size=:web) + insecure_url([@attrs[:profile_banner_url], size].join('/')) if profile_banner_url? + end + + # Return the secure 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' + # @return [String] + def profile_banner_url_https(size=:web) + [@attrs[:profile_banner_url], size].join('/') if profile_banner_url? + end + + def profile_banner_url? + !@attrs[:profile_banner_url].nil? + end + alias profile_banner_url_https? profile_banner_url? + # Return the URL to the user's profile image # # @param size [String, Symbol] The size of the image. Must be one of: 'mini', 'normal', 'bigger' or 'original' # @return [String] def profile_image_url(size=:normal) - # The profile image URL comes in looking like like this: - # http://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png - # It can be converted to any of the following sizes: - # http://a0.twimg.com/profile_images/1759857427/image1326743606.png - # http://a0.twimg.com/profile_images/1759857427/image1326743606_mini.png - # http://a0.twimg.com/profile_images/1759857427/image1326743606_bigger.png - resize_profile_image_url(@attrs[:profile_image_url], size) + insecure_url(profile_image_url_https(size)) if profile_image_url? end # Return the secure URL to the user's profile image @@ -66,9 +81,14 @@ def profile_image_url_https(size=:normal) # https://a0.twimg.com/profile_images/1759857427/image1326743606.png # https://a0.twimg.com/profile_images/1759857427/image1326743606_mini.png # https://a0.twimg.com/profile_images/1759857427/image1326743606_bigger.png - resize_profile_image_url(@attrs[:profile_image_url_https], size) + resize_profile_image_url(@attrs[:profile_image_url_https], size) if profile_image_url? end + def profile_image_url? + !@attrs[:profile_image_url_https].nil? + end + alias profile_image_url_https? profile_image_url? + # @return [Twitter::Tweet] def status @status ||= Twitter::Tweet.fetch_or_new(@attrs.dup[:status].merge(:user => @attrs.except(:status))) if status? @@ -80,8 +100,11 @@ def status? private + def insecure_url(url) + url.sub(/^https/i, 'http') + end + def resize_profile_image_url(url, size) - return if url.nil? url.sub(PROFILE_IMAGE_SUFFIX_REGEX, profile_image_suffix(size)) end diff --git a/spec/fixtures/sferik.json b/spec/fixtures/sferik.json index dd670bf21..586872dd2 100644 --- a/spec/fixtures/sferik.json +++ b/spec/fixtures/sferik.json @@ -1 +1 @@ -{"show_all_inline_media":true,"lang":"en","geo_enabled":true,"profile_background_image_url":"http:\/\/a2.twimg.com\/profile_background_images\/162641967\/we_concept_bg2.png","created_at":"Mon Jul 16 12:59:01 +0000 2007","description":"A mind forever voyaging through strange seas of thought, alone.","screen_name":"sferik","url":"https:\/\/github.com\/sferik","status":{"retweeted_status":{"coordinates":null,"retweeted":false,"retweet_count":8,"created_at":"Sun Jan 16 20:57:21 +0000 2011","in_reply_to_user_id":null,"place":null,"source":"\u003Ca href=\"http:\/\/www.echofon.com\/\" rel=\"nofollow\"\u003EEchofon\u003C\/a\u003E","in_reply_to_status_id":null,"truncated":false,"favorited":false,"in_reply_to_status_id_str":null,"id_str":"26744838716133376","geo":null,"contributors":null,"in_reply_to_screen_name":null,"id":26744838716133376,"in_reply_to_user_id_str":null,"text":"[ANN] sqlite3-ruby => sqlite3"},"coordinates":null,"retweeted":false,"retweet_count":8,"created_at":"Sun Jan 16 21:38:25 +0000 2011","in_reply_to_user_id":null,"place":null,"source":"\u003Ca href=\"http:\/\/itunes.apple.com\/us\/app\/twitter\/id409789998?mt=12\" rel=\"nofollow\"\u003ETwitter for Mac\u003C\/a\u003E","in_reply_to_status_id":null,"truncated":false,"favorited":false,"in_reply_to_status_id_str":null,"id_str":"26755176471724032","geo":null,"contributors":null,"in_reply_to_screen_name":null,"id":26755176471724032,"in_reply_to_user_id_str":null,"text":"RT @tenderlove: [ANN] sqlite3-ruby => sqlite3"},"profile_text_color":"333333","followers_count":1048,"listed_count":41,"following":false,"favourites_count":1040,"profile_sidebar_fill_color":"DDEEF6","location":"San Francisco","profile_background_tile":false,"time_zone":"Pacific Time (US & Canada)","contributors_enabled":false,"statuses_count":3479,"profile_link_color":"0084B4","is_translator":false,"profile_sidebar_border_color":"C0DEED","protected":false,"id_str":"7505382","name":"Erik Michaels-Ober","verified":false,"notifications":false,"profile_use_background_image":true,"friends_count":197,"profile_image_url":"http:\/\/a1.twimg.com\/profile_images\/1186912733\/Github_Square_normal.jpg","id":7505382,"follow_request_sent":false,"utc_offset":-28800,"profile_background_color":"000000"} \ No newline at end of file +{"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 diff --git a/spec/twitter/user_spec.rb b/spec/twitter/user_spec.rb index 9c60e6bec..41974ded4 100644 --- a/spec/twitter/user_spec.rb +++ b/spec/twitter/user_spec.rb @@ -31,9 +31,110 @@ 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") + user.profile_banner_url.should be_a String + end + it "returns nil when created_at is not set" do + user = Twitter::User.new(:id => 7505382) + user.profile_banner_url.should be_nil + end + it "returns the web-sized image" do + user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581") + user.profile_banner_url.should eq "http://si0.twimg.com/profile_banners/7505382/1348266581/web" + end + context "with :web_retina passed" do + it "returns the web retina-sized image" do + user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581") + user.profile_banner_url(:web_retina).should eq "http://si0.twimg.com/profile_banners/7505382/1348266581/web_retina" + end + end + context "with :mobile passed" do + it "returns the mobile-sized image" do + user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581") + user.profile_banner_url(:mobile).should eq "http://si0.twimg.com/profile_banners/7505382/1348266581/mobile" + end + end + context "with :mobile_retina passed" do + it "returns the mobile retina-sized image" do + user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581") + user.profile_banner_url(:mobile_retina).should eq "http://si0.twimg.com/profile_banners/7505382/1348266581/mobile_retina" + end + end + context "with :ipad passed" do + it "returns the mobile-sized image" do + user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581") + user.profile_banner_url(:ipad).should eq "http://si0.twimg.com/profile_banners/7505382/1348266581/ipad" + end + end + context "with :ipad_retina passed" do + it "returns the mobile retina-sized image" do + user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581") + user.profile_banner_url(:ipad_retina).should eq "http://si0.twimg.com/profile_banners/7505382/1348266581/ipad_retina" + end + end + end + + describe "#profile_banner_url_https" 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") + user.profile_banner_url_https.should be_a String + end + it "returns nil when created_at is not set" do + user = Twitter::User.new(:id => 7505382) + user.profile_banner_url_https.should be_nil + end + it "returns the web-sized image" do + user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581") + user.profile_banner_url_https.should eq "https://si0.twimg.com/profile_banners/7505382/1348266581/web" + end + context "with :web_retina passed" do + it "returns the web retina-sized image" do + user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581") + user.profile_banner_url_https(:web_retina).should eq "https://si0.twimg.com/profile_banners/7505382/1348266581/web_retina" + end + end + context "with :mobile passed" do + it "returns the mobile-sized image" do + user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581") + user.profile_banner_url_https(:mobile).should eq "https://si0.twimg.com/profile_banners/7505382/1348266581/mobile" + end + end + context "with :mobile_retina passed" do + it "returns the mobile retina-sized image" do + user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581") + user.profile_banner_url_https(:mobile_retina).should eq "https://si0.twimg.com/profile_banners/7505382/1348266581/mobile_retina" + end + end + context "with :ipad passed" do + it "returns the mobile-sized image" do + user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581") + user.profile_banner_url_https(:ipad).should eq "https://si0.twimg.com/profile_banners/7505382/1348266581/ipad" + end + end + context "with :ipad_retina passed" do + it "returns the mobile retina-sized image" do + user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581") + user.profile_banner_url_https(:ipad_retina).should eq "https://si0.twimg.com/profile_banners/7505382/1348266581/ipad_retina" + end + end + end + + describe "#profile_banner_url?" do + it "returns true when profile_banner_url is set" do + profile_banner_url = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581").profile_banner_url? + profile_banner_url.should be_true + end + it "returns false when status is not set" do + profile_banner_url = Twitter::User.new(:id => 7505382).profile_banner_url? + profile_banner_url.should be_false + end + end + describe "#profile_image_url" do - it "returns a String when profile_image_url is set" do - user = Twitter::User.new(:id => 7505382, :profile_image_url => "http://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png") + it "returns a String when profile_image_url_https is set" do + user = Twitter::User.new(:id => 7505382, :profile_image_url_https => "https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png") user.profile_image_url.should be_a String end it "returns nil when created_at is not set" do @@ -41,24 +142,24 @@ user.profile_image_url.should be_nil end it "returns the normal-sized image" do - user = Twitter::User.new(:id => 7505382, :profile_image_url => "http://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png") + user = Twitter::User.new(:id => 7505382, :profile_image_url_https => "https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png") user.profile_image_url.should eq "http://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png" end context "with :original passed" do it "returns the original image" do - user = Twitter::User.new(:id => 7505382, :profile_image_url => "http://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png") + user = Twitter::User.new(:id => 7505382, :profile_image_url_https => "https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png") user.profile_image_url(:original).should eq "http://a0.twimg.com/profile_images/1759857427/image1326743606.png" end end context "with :bigger passed" do it "returns the bigger-sized image" do - user = Twitter::User.new(:id => 7505382, :profile_image_url => "http://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png") + user = Twitter::User.new(:id => 7505382, :profile_image_url_https => "https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png") user.profile_image_url(:bigger).should eq "http://a0.twimg.com/profile_images/1759857427/image1326743606_bigger.png" end end context "with :mini passed" do it "returns the mini-sized image" do - user = Twitter::User.new(:id => 7505382, :profile_image_url => "http://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png") + user = Twitter::User.new(:id => 7505382, :profile_image_url_https => "https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png") user.profile_image_url(:mini).should eq "http://a0.twimg.com/profile_images/1759857427/image1326743606_mini.png" end end @@ -97,6 +198,17 @@ end end + describe "#profile_image_url?" do + it "returns true when profile_banner_url is set" do + profile_image_url = Twitter::User.new(:id => 7505382, :profile_image_url_https => "https://si0.twimg.com/profile_banners/7505382/1348266581").profile_image_url? + profile_image_url.should be_true + end + it "returns false when status is not set" do + profile_image_url= Twitter::User.new(:id => 7505382).profile_image_url? + profile_image_url.should be_false + end + end + describe "#status" do it "returns a Status when status is set" do tweet = Twitter::User.new(:id => 7505382, :status => {:id => 25938088801}).status