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



54
55
56
57
58
# File 'lib/luma/authentication.rb', line 54

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

#access_tokenObject



50
51
52
# File 'lib/luma/authentication.rb', line 50

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
# File 'lib/luma/authentication.rb', line 16

def authenticate
  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
  end

  return self
end

#validateObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/luma/authentication.rb', line 34

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