Class: Sorcery::Providers::Instagram

Inherits:
Base
  • Object
show all
Includes:
Sorcery::Protocols::Oauth2
Defined in:
lib/sorcery/providers/instagram.rb

Overview

This class adds support for OAuth with Instagram.com.

Instance Attribute Summary collapse

Attributes inherited from Base

#access_token, #callback_url, #key, #original_callback_url, #secret, #site, #state, #user_info_mapping

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sorcery::Protocols::Oauth2

#build_client, #get_access_token, #oauth_version

Methods inherited from Base

#auth_hash, descendants, #has_callback?, name

Constructor Details

#initializeInstagram

Returns a new instance of Instagram.



11
12
13
14
15
16
17
18
19
# File 'lib/sorcery/providers/instagram.rb', line 11

def initialize
  super

  @site = 'https://api.instagram.com'
  @token_url = '/oauth/access_token'
  @authorization_path = '/oauth/authorize/'
  @user_info_path = '/v1/users/self'
  @scope = 'basic'
end

Instance Attribute Details

#access_permissionsObject

Returns the value of attribute access_permissions.



7
8
9
# File 'lib/sorcery/providers/instagram.rb', line 7

def access_permissions
  @access_permissions
end

#authorization_pathObject

Returns the value of attribute authorization_path.



7
8
9
# File 'lib/sorcery/providers/instagram.rb', line 7

def authorization_path
  @authorization_path
end

#scopeObject

Returns the value of attribute scope.



7
8
9
# File 'lib/sorcery/providers/instagram.rb', line 7

def scope
  @scope
end

#token_urlObject

Returns the value of attribute token_url.



7
8
9
# File 'lib/sorcery/providers/instagram.rb', line 7

def token_url
  @token_url
end

#user_info_fieldsObject

Returns the value of attribute user_info_fields.



7
8
9
# File 'lib/sorcery/providers/instagram.rb', line 7

def 
  @user_info_fields
end

#user_info_pathObject

Returns the value of attribute user_info_path.



7
8
9
# File 'lib/sorcery/providers/instagram.rb', line 7

def 
  @user_info_path
end

Class Method Details

.included(base) ⇒ Object



21
22
23
# File 'lib/sorcery/providers/instagram.rb', line 21

def self.included(base)
  base.extend Sorcery::Providers
end

Instance Method Details

#authorize_url(opts = {}) ⇒ Object

overrides oauth2#authorize_url to allow customized scope.



31
32
33
34
# File 'lib/sorcery/providers/instagram.rb', line 31

def authorize_url(opts = {})
  @scope = access_permissions.present? ? access_permissions.join(' ') : scope
  super(opts.merge(token_url: @token_url))
end

#get_user_hash(access_token) ⇒ Object

see ‘user_info_mapping` in config/initializer, given `user_info_mapping` to specify

{:db_attribute_name => 'instagram_attr_name'}

so that Sorcery can build AR model from attr names

NOTE: instead of just getting the user info from the access_token (which already returns them), testing strategy relies on querying user_info_path



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sorcery/providers/instagram.rb', line 57

def get_user_hash(access_token)
  call_api_params = {
    access_token: access_token.token,
    client_id: access_token[:client_id]
  }
  response = access_token.get(
    "#{}?#{call_api_params.to_param}"
  )

  user_attrs = {}
  user_attrs[:user_info] = JSON.parse(response.body)['data']
  user_attrs[:uid] = user_attrs[:user_info]['id']
  user_attrs
end

#login_url(_params, _session) ⇒ Object

provider implements method to build Oauth client



26
27
28
# File 'lib/sorcery/providers/instagram.rb', line 26

def (_params, _session)
  authorize_url(token_url: @token_url)
end

#process_callback(params, _session) ⇒ Object

pass oauth2 param ‘code` provided by instgrm server



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sorcery/providers/instagram.rb', line 37

def process_callback(params, _session)
  args = {}.tap do |a|
    a[:code] = params[:code] if params[:code]
  end
  get_access_token(
    args,
    token_url: @token_url,
    client_id: @key,
    client_secret: @secret
  )
end