Class: CoreLibrary::Single

Inherits:
Authentication
  • Object
show all
Defined in:
lib/apimatic-core/authentication/multiple/single_auth.rb

Overview

The class to handle the single authentication for a particular request.

Instance Method Summary collapse

Constructor Details

#initialize(auth_participant) ⇒ Single

Initializes a new instance of Single.

Parameters:

  • auth_participant (String)

    Auth participant name.



12
13
14
15
16
17
# File 'lib/apimatic-core/authentication/multiple/single_auth.rb', line 12

def initialize(auth_participant)
  @auth_participant = auth_participant
  @mapped_auth = nil
  @error_message = nil
  @is_valid = false
end

Instance Method Details

#apply(http_request) ⇒ Object

Applies the associated auth to the HTTP request.

Parameters:

  • http_request (HttpRequest)

    The HTTP request on which the auth is to be applied.



41
42
43
44
45
# File 'lib/apimatic-core/authentication/multiple/single_auth.rb', line 41

def apply(http_request)
  return unless @is_valid

  @mapped_auth.apply(http_request)
end

#error_messageString

Getter for the error message for auth.

Returns:

  • (String)

    The error message while applying the auth.



6
7
8
# File 'lib/apimatic-core/authentication/multiple/single_auth.rb', line 6

def error_message
  "[#{@error_message}]"
end

#validBoolean

Checks if the associated auth is valid.

Returns:

  • (Boolean)

    True if the associated auth is valid, false otherwise.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
# File 'lib/apimatic-core/authentication/multiple/single_auth.rb', line 31

def valid
  raise ArgumentError, 'The auth manager entry must not have a nil value.' if @mapped_auth.nil?

  @is_valid = @mapped_auth.valid
  @error_message = @mapped_auth.error_message unless @is_valid
  @is_valid
end

#with_auth_managers(auth_managers) ⇒ Single

Extracts out the auth from the given auth managers.

Parameters:

  • auth_managers (Hash)

    The hash of auth managers.

Returns:

  • (Single)

    An updated instance of itself.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
# File 'lib/apimatic-core/authentication/multiple/single_auth.rb', line 22

def with_auth_managers(auth_managers)
  raise ArgumentError, 'Auth key is invalid.' unless auth_managers.key?(@auth_participant)

  @mapped_auth = auth_managers[@auth_participant]
  self
end