Skip to content

Commit

Permalink
Add profile banner methods to User class
Browse files Browse the repository at this point in the history
* User#profile_banner_url
* User#profile_banner_url_https
* User#profile_banner_url?
  • Loading branch information
sferik committed Oct 6, 2012
1 parent 2de3f3f commit d0200d7
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 16 deletions.
41 changes: 32 additions & 9 deletions lib/twitter/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/sferik.json
Original file line number Diff line number Diff line change
@@ -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 =&gt; 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 =&gt; 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"}
{"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}
124 changes: 118 additions & 6 deletions spec/twitter/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,135 @@
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
user = Twitter::User.new(:id => 7505382)
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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d0200d7

Please sign in to comment.