Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added options to change the size of the user's image #61

Merged
merged 3 commits into from
Jun 24, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ The options are:

* **force_login** - This option sends the user to a sign-in screen to enter their Twitter credentials, even if they are already signed in. This is handy when your application supports multiple Twitter accounts and you want to ensure the correct user is signed in. *Example:* `http://yoursite.com/auth/twitter?force_login=true`

* **secure_image_url** - Set to true to use https for the avatar image url returned in the auth hash. Default is false.

* **screen_name** - This option implies **force_login**, except the screen name field is pre-filled with a particular value. *Example:* `http://yoursite.com/auth/twitter?screen_name=jim`

* **secure_image_url** - Set to `true` to use https for the user's image url. Default is `false`.

* **image_size**: This option defines the size of the user's image. Valid options include `mini` (24x24), `normal` (48x48), `bigger` (73x73) and `original` (the size of the image originally uploaded). Default is `normal`.

* **x_auth_access_type** - This option (described [here](https://dev.twitter.com/docs/api/1/post/oauth/request_token)) lets you request the level of access that your app will have to the Twitter account in question. *Example:* `http://yoursite.com/auth/twitter?x_auth_access_type=read`

* **use_authorize** - There are actually two URLs you can use against the Twitter API. As mentioned, the default is `https://api.twitter.com/oauth/authenticate`, but you also have `https://api.twitter.com/oauth/authorize`. Passing this option as `true` will use the second URL rather than the first. What's the difference? As described [here](https://dev.twitter.com/docs/api/1/get/oauth/authenticate), with `authenticate`, if your user has already granted permission to your application, Twitter will redirect straight back to your application, whereas `authorize` forces the user to go through the "grant permission" screen again. For certain use cases this may be necessary. *Example:* `http://yoursite.com/auth/twitter?use_authorize=true`. *Note:* You must have "Allow this application to be used to Sign in with Twitter" checked in [your application's settings](https://dev.twitter.com/apps) - without it your user will be asked to authorize your application each time they log in.
Expand Down
18 changes: 17 additions & 1 deletion lib/omniauth/strategies/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Twitter < OmniAuth::Strategies::OAuth
:nickname => raw_info['screen_name'],
:name => raw_info['name'],
:location => raw_info['location'],
:image => options[:secure_image_url] ? raw_info['profile_image_url_https'] : raw_info['profile_image_url'],
:image => image_url(options),
:description => raw_info['description'],
:urls => {
'Website' => raw_info['url'],
Expand Down Expand Up @@ -63,6 +63,22 @@ def request_phase
old_request_phase
end

private

def image_url(options)
original_url = options[:secure_image_url] ? raw_info['profile_image_url_https'] : raw_info['profile_image_url']
case options[:image_size]
when 'mini'
original_url.sub('normal', 'mini')
when 'bigger'
original_url.sub('normal', 'bigger')
when 'original'
original_url.sub('_normal', '')
else
original_url
end
end

end
end
end
34 changes: 31 additions & 3 deletions spec/omniauth/strategies/twitter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

describe OmniAuth::Strategies::Twitter do
subject do
OmniAuth::Strategies::Twitter.new({})
args = ['appid', 'secret', @options || {}].compact
OmniAuth::Strategies::Twitter.new(*args)
end

context 'client options' do
describe 'client options' do
it 'should have correct name' do
expect(subject.options.name).to eq('twitter')
end
Expand All @@ -19,8 +20,35 @@
end
end

describe 'image_size option' do
context 'when user has an image' do
it 'should return image with size specified' do
@options = { :image_size => 'original' }
subject.stub(:raw_info).and_return(
{ 'profile_image_url' => 'http://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_normal.png' }
)
expect(subject.info[:image]).to eq('http://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0.png')
end

it 'should return secure image with size specified' do
@options = { :secure_image_url => 'true', :image_size => 'mini' }
subject.stub(:raw_info).and_return(
{ 'profile_image_url_https' => 'https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_normal.png' }
)
expect(subject.info[:image]).to eq('https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_mini.png')
end

it 'should return normal image by default' do
subject.stub(:raw_info).and_return(
{ 'profile_image_url' => 'http://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_normal.png' }
)
expect(subject.info[:image]).to eq('http://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_normal.png')
end
end
end

describe 'request_phase' do
context 'no request params set and x_auth_access_type specified' do
context 'with no request params set and x_auth_access_type specified' do
before do
subject.options[:request_params] = nil
subject.stub(:session).and_return(
Expand Down