class Yoti::Configuration

Attributes

api_endpoint[W]
api_port[RW]
api_url[RW]
api_version[RW]
client_sdk_id[RW]
doc_scan_api_endpoint[W]
key[RW]
key_file_path[RW]
sdk_identifier[RW]

Public Class Methods

new() click to toggle source

Set config variables by using a configuration block

# File lib/yoti/configuration.rb, line 9
def initialize
  @client_sdk_id = ''
  @key_file_path = ''
  @key = ''
  @sdk_identifier = 'Ruby'
  @api_url = 'https://api.yoti.com'
  @api_port = 443
  @api_version = 'v1'
end

Public Instance Methods

api_endpoint() click to toggle source

@return [String] the API endpoint for the selected API version

# File lib/yoti/configuration.rb, line 20
def api_endpoint
  @api_endpoint ||= ENV['YOTI_API_URL'] || "#{@api_url}/api/#{@api_version}"
end
doc_scan_api_endpoint() click to toggle source

@return [String] the Doc Scan API endpoint

# File lib/yoti/configuration.rb, line 25
def doc_scan_api_endpoint
  @doc_scan_api_endpoint ||= ENV['YOTI_DOC_SCAN_API_URL'] || "#{@api_url}/idverify/#{@api_version}"
end
validate() click to toggle source

Validates the configuration values set in instance variables @return [nil]

# File lib/yoti/configuration.rb, line 31
def validate
  validate_required_all(%w[client_sdk_id])
  validate_required_any(%w[key_file_path key])
  validate_value('api_version', ['v1'])
end

Private Instance Methods

config_set?(config) click to toggle source

Checks if a configuration has been set as a instance variable @param config [String] the name of the configuration @return [Boolean]

# File lib/yoti/configuration.rb, line 88
def config_set?(config)
  instance_variable_get("@#{config}").to_s != ''
end
invalid_value?(value, allowed_values) click to toggle source

Checks if an allowed array of values includes the setting value @param value [String] the value to be checked @param allowed_values [Array] an array of allowed values for the variable @return [Boolean]

# File lib/yoti/configuration.rb, line 81
def invalid_value?(value, allowed_values)
  allowed_values.any? && !allowed_values.include?(value)
end
validate_required_all(required_configs) click to toggle source

Loops through the list of required configurations and raises an error if a it can't find all the configuration values set @return [nil]

# File lib/yoti/configuration.rb, line 42
def validate_required_all(required_configs)
  required_configs.each do |config|
    unless config_set?(config)
      message = "Configuration value `#{config}` is required."
      raise ConfigurationError, message
    end
  end
end
validate_required_any(required_configs) click to toggle source

Loops through the list of required configurations and raises an error if a it can't find at least one configuration value set @return [nil]

# File lib/yoti/configuration.rb, line 54
def validate_required_any(required_configs)
  valid = required_configs.select { |config| config_set?(config) }

  return if valid.any?

  config_list = required_configs.map { |conf| "`#{conf}`" }.join(', ')
  message = "At least one of the configuration values has to be set: #{config_list}."
  raise ConfigurationError, message
end
validate_value(config, allowed_values) click to toggle source

Raises an error if the setting receives an invalid value @param config [String] the value to be assigned @param allowed_values [Array] an array of allowed values for the variable @return [nil]

# File lib/yoti/configuration.rb, line 68
def validate_value(config, allowed_values)
  value = instance_variable_get("@#{config}")

  return unless invalid_value?(value, allowed_values)

  message = "Configuration value `#{value}` is not allowed for `#{config}`."
  raise ConfigurationError, message
end