Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #61 from GuilhermeSimoes/image-size

Added options to change the size of the user's image
  • Loading branch information...
commit b04426169d62de3a41e8556b3c750723266874ef 2 parents 865560d + 266a00a
@arunagw authored
View
6 README.md
@@ -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.
View
18 lib/omniauth/strategies/twitter.rb
@@ -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'],
@@ -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
View
34 spec/omniauth/strategies/twitter_spec.rb
@@ -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
@@ -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(
Please sign in to comment.
Something went wrong with that request. Please try again.