Class: Luma::Authentication
- Inherits:
-
Connection
- Object
- Connection
- Luma::Authentication
- Defined in:
- lib/luma/authentication.rb
Constant Summary collapse
- BASE_ENDPOINT =
'/api'.freeze
- AUTH_ENDPOINT =
"#{BASE_ENDPOINT}/users/login".freeze
- VALIDATE_ENDPOINT =
"#{BASE_ENDPOINT}/tokens/validate".freeze
Constants inherited from Connection
Instance Attribute Summary collapse
-
#email ⇒ Object
Returns the value of attribute email.
-
#password ⇒ Object
Returns the value of attribute password.
-
#response ⇒ Object
Returns the value of attribute response.
-
#validation_response ⇒ Object
Returns the value of attribute validation_response.
Instance Method Summary collapse
- #access_header ⇒ Object
- #access_token ⇒ Object
- #authenticate ⇒ Object
- #expires? ⇒ Boolean
- #expiry ⇒ Object
-
#initialize(email: nil, password: nil) ⇒ Authentication
constructor
A new instance of Authentication.
- #validate ⇒ Object
Methods inherited from Connection
#add_headers_and_body, #request
Constructor Details
#initialize(email: nil, password: nil) ⇒ Authentication
Returns a new instance of Authentication.
9 10 11 12 13 14 |
# File 'lib/luma/authentication.rb', line 9 def initialize(email: nil, password: nil) @email = email @password = password @response = nil @validation_response = nil end |
Instance Attribute Details
#email ⇒ Object
Returns the value of attribute email.
3 4 5 |
# File 'lib/luma/authentication.rb', line 3 def email @email end |
#password ⇒ Object
Returns the value of attribute password.
3 4 5 |
# File 'lib/luma/authentication.rb', line 3 def password @password end |
#response ⇒ Object
Returns the value of attribute response.
3 4 5 |
# File 'lib/luma/authentication.rb', line 3 def response @response end |
#validation_response ⇒ Object
Returns the value of attribute validation_response.
3 4 5 |
# File 'lib/luma/authentication.rb', line 3 def validation_response @validation_response end |
Instance Method Details
#access_header ⇒ Object
69 70 71 72 73 |
# File 'lib/luma/authentication.rb', line 69 def access_header return { 'X-Access-Token' => self.access_token, } end |
#access_token ⇒ Object
53 54 55 |
# File 'lib/luma/authentication.rb', line 53 def access_token return @response['token'] if @response end |
#authenticate ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/luma/authentication.rb', line 16 def authenticate if (self.expires?) request = { body: { email: email, password: password }, endpoint: AUTH_ENDPOINT } response = self.request(**request, auth: false) if (false == response.ok?) @response = nil raise LumaException.from_response(response, msg: 'Authentication') else @response = response validate end end return self end |
#expires? ⇒ Boolean
61 62 63 64 65 66 67 |
# File 'lib/luma/authentication.rb', line 61 def expires? if (self.expiry) return (self.expiry / 1000) <= Time.now.to_f.floor else return true end end |
#expiry ⇒ Object
57 58 59 |
# File 'lib/luma/authentication.rb', line 57 def expiry return @validation_response['exp'] if @validation_response end |
#validate ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/luma/authentication.rb', line 37 def validate request = { body: { token: access_token }, endpoint: VALIDATE_ENDPOINT } response = self.request(**request, auth: false) if (false == response.ok?) @response = nil raise LumaException.from_response(response, msg: 'Authentication Validation') else @validation_response = response end end |