Skip to content

Commit

Permalink
Merge pull request #61 from GuilhermeSimoes/image-size
Browse files Browse the repository at this point in the history
Added options to change the size of the user's image
  • Loading branch information
arunagw committed Jun 24, 2013
2 parents 865560d + 266a00a commit b044261
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Original file line 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` * **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` * **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` * **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. * **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 Original file line Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Twitter < OmniAuth::Strategies::OAuth
:nickname => raw_info['screen_name'], :nickname => raw_info['screen_name'],
:name => raw_info['name'], :name => raw_info['name'],
:location => raw_info['location'], :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'], :description => raw_info['description'],
:urls => { :urls => {
'Website' => raw_info['url'], 'Website' => raw_info['url'],
Expand Down Expand Up @@ -63,6 +63,22 @@ def request_phase
old_request_phase old_request_phase
end 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 end
end end
34 changes: 31 additions & 3 deletions spec/omniauth/strategies/twitter_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@


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


context 'client options' do describe 'client options' do
it 'should have correct name' do it 'should have correct name' do
expect(subject.options.name).to eq('twitter') expect(subject.options.name).to eq('twitter')
end end
Expand All @@ -19,8 +20,35 @@
end end
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 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 before do
subject.options[:request_params] = nil subject.options[:request_params] = nil
subject.stub(:session).and_return( subject.stub(:session).and_return(
Expand Down

0 comments on commit b044261

Please sign in to comment.