Class: SilverMother::Application
- Inherits:
-
Object
- Object
- SilverMother::Application
- 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
-
#gateway_url ⇒ Object
Returns the value of attribute gateway_url.
-
#oauth2_client_id ⇒ Object
Returns the value of attribute oauth2_client_id.
-
#oauth2_client_secret ⇒ Object
Returns the value of attribute oauth2_client_secret.
-
#redirect_url ⇒ Object
Returns the value of attribute redirect_url.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #authorization_url ⇒ Object
- #expired? ⇒ Boolean
- #get_token(auth_code) ⇒ Object
-
#initialize(params = {}) ⇒ Application
constructor
A new instance of Application.
- #refresh_token(refresh_token) ⇒ Object
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_url ⇒ Object
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_id ⇒ Object
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_secret ⇒ Object
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_url ⇒ Object
Returns the value of attribute redirect_url.
12 13 14 |
# File 'lib/silver_mother/application.rb', line 12 def redirect_url @redirect_url end |
#scope ⇒ Object
Returns the value of attribute scope.
12 13 14 |
# File 'lib/silver_mother/application.rb', line 12 def scope @scope end |
#token ⇒ Object
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_url ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/silver_mother/application.rb', line 27 def 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
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 |