Skip to content

Commit

Permalink
Add size option to Twitter::User#profile_image_url and Twitter::User#…
Browse files Browse the repository at this point in the history
…profile_image_url_https
  • Loading branch information
sferik committed Jul 6, 2012
1 parent 0d60180 commit bd4c63c
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 12 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ better performance than calling these methods multiple times in serial.
The `Twitter::Client#direct_messages` method has been renamed to
`Twitter::Client#direct_messages_received`.

The `Twitter::Status#expanded_urls` method has been removed. Use
`Twitter::Status#urls` instead.

The `Twitter::Client#profile_image` method has been removed. Use
`Twitter::User#profile_image_url` (or `Twitter::User#profile_image_url_https`)
instead.

The `Twitter::Status#expanded_urls` method has been removed. Use
`Twitter::Status#urls` instead.

The `Twitter::Client#follow` method now checks to make sure the user isn't
already being followed. If you don't wish to perform that check (which does
require an extra HTTP request), you can use the new `Twitter::Client#follow!`
Expand Down Expand Up @@ -111,13 +111,13 @@ options:
Twitter.connection_options[:headers][:user_agent] = 'Custom User Agent'

### Authentication
This library now attempts to pull credentials from `ENV` if they are not
otherwise specified. In `bash`:
This library now attempts to pull credentials from from the following
environment variables:

export TWITTER_CONSUMER_KEY=YOUR_CONSUMER_KEY
export TWITTER_CONSUMER_SECRET=YOUR_CONSUMER_SECRET
export TWITTER_OAUTH_TOKEN=YOUR_OAUTH_TOKEN
export TWITTER_OAUTH_TOKEN_SECRET=YOUR_OAUTH_TOKEN_SECRET
TWITTER_CONSUMER_KEY
TWITTER_CONSUMER_SECRET
TWITTER_OAUTH_TOKEN
TWITTER_OAUTH_TOKEN_SECRET

### Identity Map
This version introduces an identity map, which ensures that the same objects
Expand Down Expand Up @@ -160,9 +160,9 @@ vulnerabilities are discovered.

Here are some fun facts about the 3.0 release:

* The entire library is implemented in just 2,201 lines of code
* The entire library is implemented in just 2,216 lines of code
* With over 5,000 lines of specs, the spec-to-code ratio is well over 2:1
* The spec suite contains 631 examples and runs in under 2 seconds on a MacBook
* The spec suite contains 643 examples and runs in under 2 seconds on a MacBook
* This project has 100% C0 code coverage (the tests execute every line of
source code at least once)
* At the time of release, this library is comprehensive: you can request all
Expand All @@ -171,7 +171,7 @@ Here are some fun facts about the 3.0 release:
* This gem works on every major Ruby implementation, including JRuby and
Rubinius
* The first version was released on November 26, 2006 (over 5 years ago)
* This gem has just three dependencies: `faraday`, `multi_json`, and
* This gem has only three dependencies: `faraday`, `multi_json`, and
`simple_oauth`
* Previous versions of this gem have been [downloaded over half a million
times][stats]
Expand Down
32 changes: 32 additions & 0 deletions lib/twitter/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,42 @@ class User < Twitter::Identity
alias verified? verified
alias want_retweets? want_retweets

# @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
@attrs[:profile_image_url].sub(/_normal(\.gif|\.jpe?g|\.png)$/, profile_image_suffix(size)) unless @attrs[:profile_image_url].nil?
end

# @return [String]
def profile_image_url_https(size=:normal)
# The profile image URL comes in looking like like this:
# https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png
# It can be converted to any of the following sizes:
# 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
@attrs[:profile_image_url_https].sub(/_normal(\.gif|\.jpe?g|\.png)$/, profile_image_suffix(size)) unless @attrs[:profile_image_url_https].nil?
end

# @return [Twitter::Status]
def status
@status ||= Twitter::Status.fetch_or_new(@attrs.dup[:status].merge(:user => @attrs.except(:status))) unless @attrs[:status].nil?
end

private

def profile_image_suffix(size)
if :original == size.to_sym
"\\1"
else
"_#{size}\\1"
end
end

end
end
66 changes: 66 additions & 0 deletions spec/twitter/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,72 @@
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")
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.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.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.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.profile_image_url(:mini).should eq "http://a0.twimg.com/profile_images/1759857427/image1326743606_mini.png"
end
end
end

describe "#profile_image_url_https" do
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_https.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_https.should be_nil
end
it "returns the normal-sized image" 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_https.should eq "https://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_https => "https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png")
user.profile_image_url_https(:original).should eq "https://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_https => "https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png")
user.profile_image_url_https(:bigger).should eq "https://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_https => "https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png")
user.profile_image_url_https(:mini).should eq "https://a0.twimg.com/profile_images/1759857427/image1326743606_mini.png"
end
end
end

describe "#status" do
it "returns a Status when status is set" do
status = Twitter::User.new(:id => 7505382, :status => {:id => 25938088801}).status
Expand Down

0 comments on commit bd4c63c

Please sign in to comment.