Class: OmniAuth::Strategies::Salesking

Inherits:
OAuth2
  • Object
show all
Defined in:
lib/sk_sdk/omni_auth/salesking.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, client_id, client_secret, sk_url, scope) ⇒ Salesking

SalesKing requires a subdomain up front It needs to be provided in a form and retrieved from the request The one on init is not used as it is grabbed from the session later

Parameters:

  • app (Rack Application)

    rack middleware application

  • client_id (String)

    the application id as registered on SalesKing

  • client_secret (String)

    the application secret as registered on SalesKing

  • sk_url (String)
  • scope (String)

    space separated extended permissions such as ‘api/invoices` or `api/clients:read,delete api/orders`



17
18
19
20
21
22
# File 'lib/sk_sdk/omni_auth/salesking.rb', line 17

def initialize(app, client_id, client_secret, sk_url, scope)
  @base_url = sk_url
  @scope = scope
  client_options = {:access_token_path => '/oauth/token'}
  super(app, :salesking, client_id, client_secret, client_options)
end

Instance Method Details

#auth_hashObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sk_sdk/omni_auth/salesking.rb', line 54

def auth_hash
  OmniAuth::Utils.deep_merge(super, {
#          'uid' => "#{user_data['current_company']['company']['id']}.#{user_data['id']}",
    'uid' => user_data['id'],
    'user_info' => ,
    'credentials' => {
      'expires_in' => @access_token.expires_in
    },
    'extra' => {'user_hash' => user_data}
  })
end

#callback_phaseObject

Monkey-patching to inject subdomain again into OmniAuth



32
33
34
35
# File 'lib/sk_sdk/omni_auth/salesking.rb', line 32

def callback_phase
  set_sk_url
  super
end

#request_phaseObject

inject salesking url and scope into OmniAuth



25
26
27
28
29
# File 'lib/sk_sdk/omni_auth/salesking.rb', line 25

def request_phase
  options[:scope] = @scope
  set_sk_url
  super
end

#set_sk_urlString

Each company has it’s own subdomain so the url must be dynamic. This is achieved by replacing the * with the subdomain from the session

Returns:

  • (String)

    url with subdomain of sk user



69
70
71
# File 'lib/sk_sdk/omni_auth/salesking.rb', line 69

def set_sk_url
  client_options[:site] = @base_url.gsub('*', session[:subdomain]).gsub(/\/\z/, '' )
end

#user_dataHash

Returns user currently logged in.

Returns:

  • (Hash)

    user currently logged in



38
39
40
41
42
43
# File 'lib/sk_sdk/omni_auth/salesking.rb', line 38

def user_data
  @data ||= begin
    ret = MultiJson.decode(@access_token.get('api/users/current'))
    ret['user']
  end
end

#user_infoObject



45
46
47
48
49
50
51
52
# File 'lib/sk_sdk/omni_auth/salesking.rb', line 45

def 
  {
    'email' => (user_data["email"] if user_data["email"]),
    'first_name' => user_data["first_name"],
    'last_name' => user_data["last_name"],
    'name' => "#{user_data['first_name']} #{user_data['last_name']}"
  }
end