Class: FmRest::ConnectionSettings

Inherits:
Object
  • Object
show all
Defined in:
lib/fmrest/connection_settings.rb

Overview

Wrapper class for connection settings hash, with a number of purposes:

  • Provide indifferent access (base hash can have either string or symbol keys)
  • Method access
  • Default values
  • Basic validation
  • Normalization (e.g. aliased settings)
  • Useful error messages

Defined Under Namespace

Classes: MissingSetting

Constant Summary collapse

PROPERTIES =
%i(
  host
  database
  username
  password
  fmid_token
  token
  token_store
  autologin
  ssl
  proxy
  log
  log_level
  coerce_dates
  date_format
  timestamp_format
  time_format
  timezone
  cognito_client_id
  cognito_pool_id
  aws_region
  cloud
).freeze
REQUIRED =

NOTE: password intentionally left non-required since it's only really needed when no token exists, and should only be required when logging in

%i(
  host
  database
).freeze
DEFAULT_DATE_FORMAT =
"MM/dd/yyyy"
DEFAULT_TIME_FORMAT =
"HH:mm:ss"
DEFAULT_TIMESTAMP_FORMAT =
"#{DEFAULT_DATE_FORMAT} #{DEFAULT_TIME_FORMAT}"
DEFAULTS =
{
  autologin:        true,
  log:              false,
  log_level:        :debug,
  date_format:      DEFAULT_DATE_FORMAT,
  time_format:      DEFAULT_TIME_FORMAT,
  timestamp_format: DEFAULT_TIMESTAMP_FORMAT,
  coerce_dates:     false,
  cloud:            :auto,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, skip_validation: false) ⇒ ConnectionSettings

Returns a new instance of ConnectionSettings.



70
71
72
73
74
# File 'lib/fmrest/connection_settings.rb', line 70

def initialize(settings, skip_validation: false)
  @settings = settings.to_h.dup
  normalize
  validate unless skip_validation
end

Class Method Details

.wrap(settings, skip_validation: false) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/fmrest/connection_settings.rb', line 62

def self.wrap(settings, skip_validation: false)
  if settings.kind_of?(self)
    settings.validate unless skip_validation
    return settings
  end
  new(settings, skip_validation: skip_validation)
end

Instance Method Details

#[](key) ⇒ Object

Raises:

  • (ArgumentError)


91
92
93
94
# File 'lib/fmrest/connection_settings.rb', line 91

def [](key)
  raise ArgumentError, "Unknown setting `#{key}'" unless PROPERTIES.include?(key.to_sym)
  get_eval(key)
end

#merge(other, **keyword_args) ⇒ Object



103
104
105
106
# File 'lib/fmrest/connection_settings.rb', line 103

def merge(other, **keyword_args)
  other = self.class.wrap(other, skip_validation: true)
  self.class.new(to_h.merge(other.to_h), **keyword_args)
end

#to_hObject



96
97
98
99
100
101
# File 'lib/fmrest/connection_settings.rb', line 96

def to_h
  PROPERTIES.each_with_object({}) do |p, h|
    v = get(p)
    h[p] = v unless v == DEFAULTS[p]
  end
end

#validateObject

Raises:



108
109
110
111
112
113
114
115
# File 'lib/fmrest/connection_settings.rb', line 108

def validate
  missing = REQUIRED.select { |r| get(r).nil? }.map { |m| "`#{m}'" }
  raise MissingSetting, "Missing required setting(s): #{missing.join(', ')}" unless missing.empty?

  unless username? || fmid_token? || token?
    raise MissingSetting, "A minimum of `username', `fmid_token' or `token' are required to be able to establish a connection"
  end
end