Class: SilverMother::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/silver_mother/application.rb

Constant Summary collapse

SENSE_URL =
'https://sen.se/api/v2/'.freeze
RESPONSE_TYPE =
'code'.freeze
GRANT_TYPES =
{ access: 'authorization_code',
refresh: 'refresh_token' }.freeze
DEFAULTS_PATHS =
{ authorization_path: 'oauth2/authorize/',
token_path:         'oauth2/token/',
refresh_path:       'oauth2/refresh/' }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Application

Returns a new instance of Application.



19
20
21
22
23
24
25
# File 'lib/silver_mother/application.rb', line 19

def initialize(params = {})
  @gateway_url          = params.fetch(:gateway_url)
  @redirect_url         = params.fetch(:redirect_url)
  @oauth2_client_id     = params.fetch(:oauth2_client_id)
  @oauth2_client_secret = params.fetch(:oauth2_client_secret)
  @scope                = params.fetch(:scope)
end

Instance Attribute Details

#gateway_urlObject

Returns the value of attribute gateway_url.



12
13
14
# File 'lib/silver_mother/application.rb', line 12

def gateway_url
  @gateway_url
end

#oauth2_client_idObject

Returns the value of attribute oauth2_client_id.



12
13
14
# File 'lib/silver_mother/application.rb', line 12

def oauth2_client_id
  @oauth2_client_id
end

#oauth2_client_secretObject

Returns the value of attribute oauth2_client_secret.



12
13
14
# File 'lib/silver_mother/application.rb', line 12

def oauth2_client_secret
  @oauth2_client_secret
end

#redirect_urlObject

Returns the value of attribute redirect_url.



12
13
14
# File 'lib/silver_mother/application.rb', line 12

def redirect_url
  @redirect_url
end

#scopeObject

Returns the value of attribute scope.



12
13
14
# File 'lib/silver_mother/application.rb', line 12

def scope
  @scope
end

#tokenObject

Returns the value of attribute token.



12
13
14
# File 'lib/silver_mother/application.rb', line 12

def token
  @token
end

Instance Method Details

#authorization_urlObject



27
28
29
30
31
32
33
34
35
# File 'lib/silver_mother/application.rb', line 27

def authorization_url
  url_params = { client_id:     @oauth2_client_id,
                 scope:         @scope,
                 redirect_uri:  html_encode(@redirect_url),
                 response_type: RESPONSE_TYPE }
               .map { |k, v| "#{k}=#{v}" }.join('&')

  SENSE_URL + DEFAULTS_PATHS[:authorization_path] + '?' + url_params
end

#expired?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/silver_mother/application.rb', line 59

def expired?
  Time.now.to_i > @token.expires_on.to_i
end

#get_token(auth_code) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/silver_mother/application.rb', line 37

def get_token(auth_code)
  @token = Api.instance
              .post(DEFAULTS_PATHS[:token_path],
                    nil,
                    url_encoded_params(auth_code, :access))
              .to_ostruct

  @token.expires_on = ttl(@token.expires_in)
  @token
end

#refresh_token(refresh_token) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/silver_mother/application.rb', line 48

def refresh_token(refresh_token)
  @token = Api.instance
              .post(DEFAULTS_PATHS[:refresh_path],
                    nil,
                    url_encoded_params(refresh_token, :refresh))
              .to_ostruct

  @token.expires_on = ttl(@token.expires_in)
  @token
end