Class: MangoApps::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mangoapps/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain: nil, client_id: nil, client_secret: nil, redirect_uri: nil, scope: nil, token_store: nil, timeout: 30, open_timeout: 10, logger: nil) ⇒ Config

rubocop:disable Metrics/ParameterLists



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mangoapps/config.rb', line 10

def initialize(domain: nil, client_id: nil, client_secret: nil, redirect_uri: nil, scope: nil, # rubocop:disable Metrics/ParameterLists
               token_store: nil, timeout: 30, open_timeout: 10, logger: nil)
  # Load environment variables from .env file
  Dotenv.load if File.exist?(".env")

  @domain        = domain || ENV.fetch("MANGOAPPS_DOMAIN", nil)
  @client_id     = client_id || ENV.fetch("MANGOAPPS_CLIENT_ID", nil)
  @client_secret = client_secret || ENV.fetch("MANGOAPPS_CLIENT_SECRET", nil)
  @redirect_uri  = redirect_uri || ENV["MANGOAPPS_REDIRECT_URI"] || "https://localhost:3000/oauth/callback"
  @scope         = scope || ENV["MANGOAPPS_SCOPE"] || "openid profile email"
  @access_token  = ENV["MANGOAPPS_ACCESS_TOKEN"]
  @refresh_token = ENV["MANGOAPPS_REFRESH_TOKEN"]
  @token_store   = token_store
  @timeout       = timeout
  @open_timeout  = open_timeout
  @logger        = logger

  validate_required_fields!
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def access_token
  @access_token
end

#client_idObject

Returns the value of attribute client_id.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def client_secret
  @client_secret
end

#domainObject

Returns the value of attribute domain.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def domain
  @domain
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def logger
  @logger
end

#open_timeoutObject

Returns the value of attribute open_timeout.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def open_timeout
  @open_timeout
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def redirect_uri
  @redirect_uri
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def refresh_token
  @refresh_token
end

#scopeObject

Returns the value of attribute scope.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def scope
  @scope
end

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def timeout
  @timeout
end

#token_storeObject

Returns the value of attribute token_store.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def token_store
  @token_store
end

Instance Method Details

#api_baseObject



31
# File 'lib/mangoapps/config.rb', line 31

def api_base  = "#{base_url}/api/"

#base_urlObject



30
# File 'lib/mangoapps/config.rb', line 30

def base_url  = "https://#{domain}"

#has_valid_token?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/mangoapps/config.rb', line 38

def has_valid_token?
  @access_token && !token_expired?
end

#token_expired?Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/mangoapps/config.rb', line 33

def token_expired?
  return true unless ENV["MANGOAPPS_TOKEN_EXPIRES_AT"]
  Time.now.to_i >= ENV["MANGOAPPS_TOKEN_EXPIRES_AT"].to_i
end