Module: PactBroker::Config::RuntimeConfigurationCoercionMethods

Included in:
RuntimeConfiguration
Defined in:
lib/pact_broker/config/runtime_configuration_coercion_methods.rb

Constant Summary collapse

COERCE_FEATURES =
lambda { | value |
  if value.is_a?(String)
    value.split(" ").each_with_object({}) { | k, h | h[k.downcase.to_sym] = true }
  elsif value.is_a?(Array)
    value.each_with_object({}) { | k, h | h[k.downcase.to_sym] = true }
  elsif value.is_a?(Hash)
    value.each_with_object({}) { | (k, v), new_hash | new_hash[k.downcase.to_sym] = Anyway::AutoCast.call(v) }
  else
    raise PactBroker::ConfigurationError, "Expected a String, Hash or Array for features but got a #{value.class.name}"
  end
}
COERCE_WEBHOOKS =
lambda { | value |
  if value.is_a?(Hash) # from env vars
    if RuntimeConfigurationCoercionMethods.all_keys_are_number_strings?(value)
      RuntimeConfigurationCoercionMethods.convert_hash_with_number_string_keys_to_array(value).collect(&:symbolize_keys)
    else
      raise PactBroker::ConfigurationError, "Could not coerce #{value} into an array of webhook configurations. Please check docs for the expected format."
    end
  elsif value.is_a?(Array) # from YAML
    value.collect(&:symbolize_keys)
  else
    raise PactBroker::ConfigurationError, "Webhook certificates cannot be set using a #{value.class}"
  end
}

Class Method Summary collapse

Class Method Details

.all_keys_are_number_strings?(hash) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/pact_broker/config/runtime_configuration_coercion_methods.rb', line 38

def self.all_keys_are_number_strings?(hash)
  hash.keys.all? { | k | k.to_s.to_i.to_s == k } # is an integer as a string
end

.convert_hash_with_number_string_keys_to_array(hash) ⇒ Object



42
43
44
45
46
# File 'lib/pact_broker/config/runtime_configuration_coercion_methods.rb', line 42

def self.convert_hash_with_number_string_keys_to_array(hash)
  hash.keys.collect{ |k| [k, k.to_i]}.sort_by(&:last).collect(&:first).collect do | key |
    hash[key]
  end
end