class FmRest::ConnectionSettings
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
Constants
- DEFAULTS
- DEFAULT_DATE_FORMAT
- DEFAULT_TIMESTAMP_FORMAT
- DEFAULT_TIME_FORMAT
- PROPERTIES
- 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
Public Class Methods
new(settings, skip_validation: false)
click to toggle source
# File lib/fmrest/connection_settings.rb, line 65 def initialize(settings, skip_validation: false) @settings = settings.to_h.dup normalize validate unless skip_validation end
wrap(settings, skip_validation: false)
click to toggle source
# File lib/fmrest/connection_settings.rb, line 57 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
Public Instance Methods
[](key)
click to toggle source
# File lib/fmrest/connection_settings.rb, line 86 def [](key) raise ArgumentError, "Unknown setting `#{key}'" unless PROPERTIES.include?(key.to_sym) get_eval(key) end
merge(other, **keyword_args)
click to toggle source
# File lib/fmrest/connection_settings.rb, line 98 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_h()
click to toggle source
# File lib/fmrest/connection_settings.rb, line 91 def to_h PROPERTIES.each_with_object({}) do |p, h| v = get(p) h[p] = v unless v == DEFAULTS[p] end end
validate()
click to toggle source
# File lib/fmrest/connection_settings.rb, line 103 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
Private Instance Methods
get(key)
click to toggle source
# File lib/fmrest/connection_settings.rb, line 119 def get(key) return @settings[key.to_sym] if @settings.has_key?(key.to_sym) return @settings[key.to_s] if @settings.has_key?(key.to_s) DEFAULTS[key.to_sym] end
get_eval(key)
click to toggle source
# File lib/fmrest/connection_settings.rb, line 114 def get_eval(key) c = get(key) c.kind_of?(Proc) ? c.call : c end
normalize()
click to toggle source
# File lib/fmrest/connection_settings.rb, line 125 def normalize if !get(:username) && account_name = get(:account_name) @settings[:username] = account_name end end