Class: Dadata::Configuration

Inherits:
Object
  • Object
show all
Includes:
SensitiveData
Defined in:
lib/dadata.rb

Overview

Configuration class for the DaData API client

Examples:

Dadata.configure do |config|
  config.api_key = 'your_api_key'
  config.secret_key = 'your_secret_key'
  config.timeout_sec = 5
end

Constant Summary

Constants included from SensitiveData

SensitiveData::SENSITIVE_HEADERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SensitiveData

#sanitize_headers, #sanitize_message

Constructor Details

#initializeConfiguration

Initialize a new Configuration instance with default values



59
60
61
62
63
64
65
66
67
# File 'lib/dadata.rb', line 59

def initialize
  @suggestions_count = SUGGESTIONS_COUNT
  @timeout_sec = TIMEOUT_SEC
  @log_level = :info
  # Request payloads carry PII (passports, names, phones); never logged unless
  # a developer explicitly opts in for debugging.
  @log_request_bodies = false
  setup_logger
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



52
53
54
# File 'lib/dadata.rb', line 52

def api_key
  @api_key
end

#log_levelObject

Returns the value of attribute log_level.



52
53
54
# File 'lib/dadata.rb', line 52

def log_level
  @log_level
end

#log_request_bodiesObject

Returns the value of attribute log_request_bodies.



52
53
54
# File 'lib/dadata.rb', line 52

def log_request_bodies
  @log_request_bodies
end

#loggerObject

Returns the value of attribute logger.



54
55
56
# File 'lib/dadata.rb', line 54

def logger
  @logger
end

#secret_keyObject

Returns the value of attribute secret_key.



52
53
54
# File 'lib/dadata.rb', line 52

def secret_key
  @secret_key
end

#suggestions_countObject

Returns the value of attribute suggestions_count.



54
55
56
# File 'lib/dadata.rb', line 54

def suggestions_count
  @suggestions_count
end

#timeout_secObject

Returns the value of attribute timeout_sec.



52
53
54
# File 'lib/dadata.rb', line 52

def timeout_sec
  @timeout_sec
end

Instance Method Details

#validate!void

This method returns an undefined value.

Validates the configuration settings

Raises:



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/dadata.rb', line 73

def validate!
  if timeout_sec && timeout_sec <= 0
    raise ConfigurationError, 'Timeout must be positive'
  end
  if suggestions_count && (suggestions_count < 1 || suggestions_count > MAX_SUGGESTIONS)
    raise ConfigurationError, "Suggestions count must be between 1 and #{MAX_SUGGESTIONS}"
  end
  if api_key.nil? || api_key.strip.empty?
    raise ConfigurationError, "API key can't be blank"
  end
end