mirror of
https://github.com/simi/omniauth-facebook.git
synced 2022-11-09 12:32:45 -05:00
Image dimension can be set using image_size option
This commit is contained in:
parent
b7150215fb
commit
3f221292b6
3 changed files with 27 additions and 6 deletions
|
@ -37,7 +37,7 @@ You can configure several options, which you pass in to the `provider` method vi
|
|||
* `auth_type`: Optionally specifies the requested authentication features as a comma-separated list, as per https://developers.facebook.com/docs/authentication/reauthentication/.
|
||||
Valid values are `https` (checks for the presence of the secure cookie and asks for re-authentication if it is not present), and `reauthenticate` (asks the user to re-authenticate unconditionally). Default is `nil`.
|
||||
* `secure_image_url`: Set to `true` to use https for the avatar image url returned in the auth hash. Default is `false`.
|
||||
* `image_size`: Set the size for the returned image url in the auth hash. Valid options are `square` (50x50), `small` (50 pixels wide, variable height), `normal` (100 pixels wide, variable height), or `large` (about 200 pixels wide, variable height). Default is `square` (50x50).
|
||||
* `image_size`: Set the size for the returned image url in the auth hash. Valid options include `square` (50x50), `small` (50 pixels wide, variable height), `normal` (100 pixels wide, variable height), or `large` (about 200 pixels wide, variable height). You can also specify the dimension by setting it to a hash with `:width` and `:height` as keys.
|
||||
|
||||
For example, to request `email`, `user_birthday` and `read_stream` permissions and display the authentication page in a popup window:
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ module OmniAuth
|
|||
'name' => raw_info['name'],
|
||||
'first_name' => raw_info['first_name'],
|
||||
'last_name' => raw_info['last_name'],
|
||||
'image' => "#{options[:secure_image_url] ? 'https' : 'http'}://graph.facebook.com/#{uid}/picture?type=#{options[:image_size] || 'square'}",
|
||||
'image' => image_url(uid, options),
|
||||
'description' => raw_info['bio'],
|
||||
'urls' => {
|
||||
'Facebook' => raw_info['link'],
|
||||
|
@ -216,6 +216,20 @@ module OmniAuth
|
|||
value += '=' * (4 - value.size.modulo(4))
|
||||
Base64.decode64(value.tr('-_', '+/'))
|
||||
end
|
||||
|
||||
def image_url uid, options
|
||||
uri_class = options[:secure_image_url] ? URI::HTTPS : URI::HTTP
|
||||
url = uri_class.build({host: 'graph.facebook.com', path: "/#{uid}/picture"})
|
||||
|
||||
query = if options[:image_size].is_a?(String)
|
||||
{ type: options[:image_size] }
|
||||
elsif options[:image_size].is_a?(Hash)
|
||||
options[:image_size]
|
||||
end
|
||||
url.query = URI.encode_www_form(query) if query
|
||||
|
||||
url.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
15
test/test.rb
15
test/test.rb
|
@ -107,15 +107,22 @@ class InfoTest < StrategyTestCase
|
|||
@options = { :secure_image_url => true }
|
||||
raw_info = { 'name' => 'Fred Smith', 'id' => '321' }
|
||||
strategy.stubs(:raw_info).returns(raw_info)
|
||||
assert_equal 'https://graph.facebook.com/321/picture?type=square', strategy.info['image']
|
||||
assert_equal 'https://graph.facebook.com/321/picture', strategy.info['image']
|
||||
end
|
||||
|
||||
test 'returns the image size specified in the `image_size` option' do
|
||||
test 'returns the image with size specified in the `image_size` option' do
|
||||
@options = { :image_size => 'normal' }
|
||||
raw_info = { 'name' => 'Fred Smith', 'id' => '321' }
|
||||
strategy.stubs(:raw_info).returns(raw_info)
|
||||
assert_equal 'http://graph.facebook.com/321/picture?type=normal', strategy.info['image']
|
||||
end
|
||||
|
||||
test 'returns the image with width and height specified in the `image_size` option' do
|
||||
@options = { :image_size => { width: 123, height: 987 } }
|
||||
raw_info = { 'name' => 'Fred Smith', 'id' => '321' }
|
||||
strategy.stubs(:raw_info).returns(raw_info)
|
||||
assert_equal 'http://graph.facebook.com/321/picture?width=123&height=987', strategy.info['image']
|
||||
end
|
||||
end
|
||||
|
||||
class InfoTestOptionalDataPresent < StrategyTestCase
|
||||
|
@ -159,9 +166,9 @@ class InfoTestOptionalDataPresent < StrategyTestCase
|
|||
assert_equal 'I am great', strategy.info['description']
|
||||
end
|
||||
|
||||
test 'returns the square format facebook avatar url' do
|
||||
test 'returns the facebook avatar url' do
|
||||
@raw_info['id'] = '321'
|
||||
assert_equal 'http://graph.facebook.com/321/picture?type=square', strategy.info['image']
|
||||
assert_equal 'http://graph.facebook.com/321/picture', strategy.info['image']
|
||||
end
|
||||
|
||||
test 'returns the Facebook link as the Facebook url' do
|
||||
|
|
Loading…
Reference in a new issue