Class: OmniAuth::Slack::OAuth2::Client

Inherits:
OAuth2::Client
  • Object
show all
Includes:
Debug
Defined in:
lib/omniauth-slack/oauth2/client.rb

Constant Summary collapse

HISTORY_DEFAULT =

If this is an array, request history will be stored. Only store request history if each Client instance is relatively short-lived.

From your app, you can set this: OmniAuth::Slack::OAuth2::Client::HISTORY_DEFAULT ||= []

Then, in your authorization callback action, you can direct the OAuth2::Client request history to the AuthHash#['extra']['raw_info']: @auth_hash = env['omniauth.auth'] @access_token = env['omniauth.strategy'].access_token @access_token.client.history = @auth_hash.extra.raw_info

TODO: The above seems a little messy. Maybe use a proc to rediredct Client request history to wherever. Or maybe don't offer any history storage at all.

nil
SUBDOMAIN_DEFAULT =
nil

Constants included from Debug

Debug::LOG_ALL, Debug::LOG_NONE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Debug

#debug, included

Methods included from CallerMethodName

#caller_method_name, included

Constructor Details

#initialize(*args, **options) ⇒ Client

Returns a new instance of Client.



39
40
41
42
43
44
45
46
47
# File 'lib/omniauth-slack/oauth2/client.rb', line 39

def initialize(*args, **options)
  debug{"args: #{args}"}
  super
  self.logger = OmniAuth.logger
  self.history ||= (options[:history] || HISTORY_DEFAULT)
  # Moved 'dup' to it's own line, cuz we can't dup nil in older ruby version.
  self.history && self.history = self.history.dup
  self.subdomain ||= options[:subdomain] || SUBDOMAIN_DEFAULT
end

Instance Attribute Details

#historyObject

Returns the value of attribute history.



37
38
39
# File 'lib/omniauth-slack/oauth2/client.rb', line 37

def history
  @history
end

#loggerObject

Returns the value of attribute logger.



37
38
39
# File 'lib/omniauth-slack/oauth2/client.rb', line 37

def logger
  @logger
end

#subdomainObject

Returns the value of attribute subdomain.



37
38
39
# File 'lib/omniauth-slack/oauth2/client.rb', line 37

def subdomain
  @subdomain
end

Instance Method Details

#get_token(params, access_token_opts = {}, access_token_class = OmniAuth::Slack::OAuth2::AccessToken) ⇒ Object

Wraps OAuth2::Client#get_token to pass in the omniauth-slack AccessToken class.



50
51
52
53
54
55
# File 'lib/omniauth-slack/oauth2/client.rb', line 50

def get_token(params, access_token_opts = {}, access_token_class = OmniAuth::Slack::OAuth2::AccessToken) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  debug{"params #{params}, access_token_opts #{access_token_opts}"}
  rslt = super(params, access_token_opts, access_token_class)
  debug{"Client #{self} built AccessToken #{rslt}"}
  rslt
end

#request(*args) ⇒ Object

Logs each API request and stores the API result in History array (if exists).

Storage can be disabled by setting client_options: false. Storage can be enabled by setting client_options: Array.new. Storage is enabled by default, when client is created from Strategy.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/omniauth-slack/oauth2/client.rb', line 64

def request(*args)
  logger.debug "(slack) API request '#{args[0..1]}'."  # in thread '#{Thread.current.object_id}'."  # by Client '#{self}'
  debug{"API request args #{args}"}
  request_output = super(*args)
  uri = args[1].to_s.gsub(/^.*\/([^\/]+)/, '\1') # use single-quote or double-back-slash for replacement.
  if history.is_a?(Array)
    debug{"Saving response to history object #{history.object_id}"}
    history << OmniAuth::Slack::AuthHash.new(
      {api_call: uri.to_s, time: Time.now, response: request_output}
    )
  end
  #debug{"API response (#{args[0..1]}) #{request_output.class}"}
  debug{"API response #{request_output.response.env.body}"}
  request_output
end

#site(*args) ⇒ Object

Wraps #site to insert custom subdomain for API calls.



81
82
83
84
85
86
87
88
89
90
# File 'lib/omniauth-slack/oauth2/client.rb', line 81

def site(*args)
  if !@subdomain.to_s.empty?
    site_uri = URI.parse(super)
    site_uri.host = "#{@subdomain}.#{site_uri.host}"
    logger.debug "(slack) Oauth site uri with custom team_domain #{site_uri}"
    site_uri.to_s
  else
    super
  end
end