Permalink
Browse files

Add option image_size.

Also add some specs.
Finally, update README to reflect code changes.
  • Loading branch information...
1 parent fee75db commit e8c3e2907b868ba26b555e7f8282a5cf508da076 @guilhermesimoes guilhermesimoes committed Jun 19, 2013
Showing with 42 additions and 4 deletions.
  1. +2 −0 README.md
  2. +17 −1 lib/omniauth/strategies/twitter.rb
  3. +23 −3 spec/omniauth/strategies/twitter_spec.rb
View
@@ -48,6 +48,8 @@ The options are:
* **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.
@@ -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
@@ -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,27 @@
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 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(

0 comments on commit e8c3e29

Please sign in to comment.