Class: Attio::Util::Configuration
- Inherits:
-
Object
- Object
- Attio::Util::Configuration
- Defined in:
- lib/attio/util/configuration.rb
Overview
Configuration management for the Attio gem
Defined Under Namespace
Classes: ConfigurationError
Constant Summary collapse
- REQUIRED_SETTINGS =
Settings that must be configured
%i[api_key].freeze
- OPTIONAL_SETTINGS =
Optional settings with defaults
%i[ api_base api_version timeout open_timeout max_retries logger debug ca_bundle_path verify_ssl_certs use_faraday won_statuses lost_statuses open_statuses in_progress_statuses ].freeze
- ALL_SETTINGS =
All available configuration settings
(REQUIRED_SETTINGS + OPTIONAL_SETTINGS).freeze
- DEFAULT_SETTINGS =
Default values for optional settings
{ api_base: "https://api.attio.com", api_version: "v2", timeout: 30, open_timeout: 10, max_retries: 3, logger: nil, debug: false, ca_bundle_path: nil, verify_ssl_certs: true, use_faraday: true, won_statuses: ["Won 🎉"].freeze, lost_statuses: ["Lost"].freeze, open_statuses: ["Lead"].freeze, in_progress_statuses: ["In Progress"].freeze }.freeze
Instance Method Summary collapse
- #apply_env_vars! ⇒ Object
- #configure ⇒ Object
-
#dup ⇒ Configuration
Create a duplicate configuration.
-
#finalize! ⇒ Object
Call this to make configuration immutable.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#merge(options) ⇒ Configuration
Create a new configuration with merged options.
-
#reset! ⇒ void
Reset configuration to defaults.
-
#to_h ⇒ Hash
Convert configuration to hash.
- #validate! ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
54 55 56 57 58 |
# File 'lib/attio/util/configuration.rb', line 54 def initialize @mutex = Mutex.new @configured = false reset_without_lock! end |
Instance Method Details
#apply_env_vars! ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/attio/util/configuration.rb', line 110 def apply_env_vars! raise ConfigurationError, "Cannot modify frozen configuration" if frozen? @mutex.synchronize do @api_key = ENV.fetch("ATTIO_API_KEY", @api_key) @api_base = ENV.fetch("ATTIO_API_BASE", @api_base) @api_version = ENV.fetch("ATTIO_API_VERSION", @api_version) @timeout = ENV.fetch("ATTIO_TIMEOUT", @timeout).to_i if ENV.key?("ATTIO_TIMEOUT") @open_timeout = ENV.fetch("ATTIO_OPEN_TIMEOUT", @open_timeout).to_i if ENV.key?("ATTIO_OPEN_TIMEOUT") @max_retries = ENV.fetch("ATTIO_MAX_RETRIES", @max_retries).to_i if ENV.key?("ATTIO_MAX_RETRIES") @debug = ENV.fetch("ATTIO_DEBUG", @debug).to_s.downcase == "true" if ENV.key?("ATTIO_DEBUG") @ca_bundle_path = ENV.fetch("ATTIO_CA_BUNDLE_PATH", @ca_bundle_path) if ENV.key?("ATTIO_CA_BUNDLE_PATH") @verify_ssl_certs = ENV.fetch("ATTIO_VERIFY_SSL_CERTS", @verify_ssl_certs).to_s.downcase != "false" if ENV.key?("ATTIO_VERIFY_SSL_CERTS") @use_faraday = ENV.fetch("ATTIO_USE_FARADAY", @use_faraday).to_s.downcase != "false" if ENV.key?("ATTIO_USE_FARADAY") if ENV.key?("ATTIO_LOGGER") logger_class = ENV["ATTIO_LOGGER"] @logger = (logger_class == "STDOUT") ? Logger.new($stdout) : nil end end end |
#configure ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/attio/util/configuration.rb', line 69 def configure raise ConfigurationError, "Configuration has already been finalized" if frozen? @mutex.synchronize do yield(self) if block_given? validate! @configured = true end end |
#dup ⇒ Configuration
Create a duplicate configuration
147 148 149 150 151 152 153 |
# File 'lib/attio/util/configuration.rb', line 147 def dup self.class.new.tap do |config| ALL_SETTINGS.each do |setting| config.instance_variable_set("@#{setting}", instance_variable_get("@#{setting}")) end end end |
#finalize! ⇒ Object
Call this to make configuration immutable
80 81 82 83 84 85 |
# File 'lib/attio/util/configuration.rb', line 80 def finalize! @mutex.synchronize do validate! freeze unless frozen? end end |
#merge(options) ⇒ Configuration
Create a new configuration with merged options
135 136 137 138 139 140 141 142 143 |
# File 'lib/attio/util/configuration.rb', line 135 def merge() dup.tap do |config| .each do |key, value| if ALL_SETTINGS.include?(key.to_sym) config.instance_variable_set("@#{key}", value) end end end end |
#reset! ⇒ void
This method returns an undefined value.
Reset configuration to defaults
62 63 64 65 66 67 |
# File 'lib/attio/util/configuration.rb', line 62 def reset! @mutex.synchronize do reset_without_lock! @configured = false end end |
#to_h ⇒ Hash
Convert configuration to hash
104 105 106 107 108 |
# File 'lib/attio/util/configuration.rb', line 104 def to_h ALL_SETTINGS.each_with_object({}) do |setting, hash| hash[setting] = instance_variable_get("@#{setting}") end end |
#validate! ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/attio/util/configuration.rb', line 87 def validate! REQUIRED_SETTINGS.each do |setting| value = instance_variable_get("@#{setting}") if value.nil? || (value.respond_to?(:empty?) && value.empty?) raise ConfigurationError, "#{setting} must be configured" end end raise ConfigurationError, "timeout must be positive" if @timeout && @timeout <= 0 raise ConfigurationError, "open_timeout must be positive" if @open_timeout && @open_timeout <= 0 raise ConfigurationError, "max_retries must be non-negative" if @max_retries&.negative? true end |