Class: Luma::Authentication

Inherits:
Connection show all
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

Connection::DEFAULT_ENDPOINT

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#emailObject

Returns the value of attribute email.



3
4
5
# File 'lib/luma/authentication.rb', line 3

def email
  @email
end

#passwordObject

Returns the value of attribute password.



3
4
5
# File 'lib/luma/authentication.rb', line 3

def password
  @password
end

#responseObject

Returns the value of attribute response.



3
4
5
# File 'lib/luma/authentication.rb', line 3

def response
  @response
end

#validation_responseObject

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_headerObject



69
70
71
72
73
# File 'lib/luma/authentication.rb', line 69

def access_header
  return {
    'X-Access-Token' => self.access_token,
  }
end

#access_tokenObject



53
54
55
# File 'lib/luma/authentication.rb', line 53

def access_token
  return @response['token'] if @response
end

#authenticateObject



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

Returns:

  • (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

#expiryObject



57
58
59
# File 'lib/luma/authentication.rb', line 57

def expiry
  return @validation_response['exp'] if @validation_response
end

#validateObject



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