Class: Usps::Imis::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/usps/imis/config.rb

Overview

API Configuration

Constant Summary collapse

IMIS_ROOT_URL_PROD =
'https://portal.americasboatingclub.org'
IMIS_ROOT_URL_DEV =
'https://abcdev.imiscloud.com'
DEFAULT_GLOBAL_LOG_PATH =
'/var/log/imis/imis.log'
REQUIRED =
%w[imis_id_query_name username password].freeze
OPTIONAL =
%w[environment logger_file global_log_path].freeze
SETTABLE =
(REQUIRED + OPTIONAL).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Config

Returns a new instance of Config.

Yields:

  • (_self)

Yield Parameters:



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/usps/imis/config.rb', line 24

def initialize
  @environment = default_environment
  @imis_id_query_name = ENV.fetch('IMIS_ID_QUERY_NAME', nil)
  @username = ENV.fetch('IMIS_USERNAME', nil)
  @password = ENV.fetch('IMIS_PASSWORD', nil)
  @base_logger = Logger.new($stdout, level: :info)
  @logger = ActiveSupport::TaggedLogging.new(@base_logger)

  yield self if block_given?

  @logger_level = logger.class::SEV_LABEL[logger.level].downcase.to_sym
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



22
23
24
# File 'lib/usps/imis/config.rb', line 22

def environment
  @environment
end

#imis_id_query_nameObject

Returns the value of attribute imis_id_query_name.



21
22
23
# File 'lib/usps/imis/config.rb', line 21

def imis_id_query_name
  @imis_id_query_name
end

#loggerObject

Returns the value of attribute logger.



22
23
24
# File 'lib/usps/imis/config.rb', line 22

def logger
  @logger
end

#logger_fileObject

Returns the value of attribute logger_file.



22
23
24
# File 'lib/usps/imis/config.rb', line 22

def logger_file
  @logger_file
end

#logger_levelObject (readonly)

Returns the value of attribute logger_level.



22
23
24
# File 'lib/usps/imis/config.rb', line 22

def logger_level
  @logger_level
end

#passwordObject

Returns the value of attribute password.



21
22
23
# File 'lib/usps/imis/config.rb', line 21

def password
  @password
end

#usernameObject

Returns the value of attribute username.



21
22
23
# File 'lib/usps/imis/config.rb', line 21

def username
  @username
end

Instance Method Details

#filtered_parametersObject

Parameters to filter out of logging



112
# File 'lib/usps/imis/config.rb', line 112

def filtered_parameters = %i[password]

#global_log_pathObject

Current path for the global mirror debug logger



76
77
78
79
80
# File 'lib/usps/imis/config.rb', line 76

def global_log_path
  return @global_log_path if defined?(@global_log_path)

  self.global_log_path = DEFAULT_GLOBAL_LOG_PATH
end

#global_log_path=(path) ⇒ Object

Define the global mirror debug logger

Parameters:

  • path (path, nil)

    Path to write log to, or nil to disable



86
87
88
89
# File 'lib/usps/imis/config.rb', line 86

def global_log_path=(path)
  @global_log_path = path
  @global_logger = build_global_logger
end

#global_loggerObject

Current global mirror debug logger



67
68
69
70
71
72
# File 'lib/usps/imis/config.rb', line 67

def global_logger
  return @global_logger if @global_logger

  global_log_path
  @global_logger
end

#hostnameObject

Environment-specific API endpoint hostname

Returns:

  • The API hostname for the current environment

Raises:



99
100
101
102
103
104
# File 'lib/usps/imis/config.rb', line 99

def hostname
  return IMIS_ROOT_URL_PROD if environment.production?
  return IMIS_ROOT_URL_DEV if environment.development?

  raise Errors::ConfigError, "Unexpected API environment: #{environment}"
end

#instance_variables_to_inspectObject

Ruby 3.5 instance variable filter



108
# File 'lib/usps/imis/config.rb', line 108

def instance_variables_to_inspect = instance_variables - %i[@password @base_logger @logger @global_logger]

#silence!Object



91
92
93
# File 'lib/usps/imis/config.rb', line 91

def silence!
  self.logger = Logger.new(nil)
end

#validate!Object



114
115
116
117
118
119
# File 'lib/usps/imis/config.rb', line 114

def validate!
  missing_config = REQUIRED.filter_map { it if public_send(it).nil? }
  return if missing_config.empty?

  raise Errors::ConfigError, "Missing required configuration: #{missing_config.join(', ')}"
end