Sha256: 4e5ea924f2429287867f9fad3c13a6dc191123d4a3c6ff7fc7cf97da502dedcd

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

Contents

module GetaroundUtils; end
module GetaroundUtils::LogFormatters; end

class GetaroundUtils::LogFormatters::DeepKeyValue
  def call(data)
    case data
    when Array
      flattify(data).map { |key, value| "#{key}=#{value}" }.join(' ')
    when Hash
      flattify(data).map { |key, value| "#{key}=#{value}" }.join(' ')
    when Numeric
      data.to_s
    when String
      data =~ /^".*"$/ ? data : data.inspect
    else
      data.to_s.inspect
    end
  end

  # https://stackoverflow.com/questions/48836464/how-to-flatten-a-hash-making-each-key-a-unique-value
  def flattify(value, result = {}, path = [])
    case value
    when Array
      value.each.with_index(0) do |v, i|
        flattify(v, result, path + [i])
      end
    when Hash
      value.each do |k, v|
        flattify(v, result, path + [k])
      end
    when Numeric
      result[path.join(".")] = value.to_s
    when String
      result[path.join(".")] = value =~ /^".*"$/ ? value : value.inspect
    else
      result[path.join(".")] = value.to_s.inspect
    end
    result
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
getaround_utils-0.1.5 lib/getaround_utils/log_formatters/deep_key_value.rb
getaround_utils-0.1.4 lib/getaround_utils/log_formatters/deep_key_value.rb
getaround_utils-0.1.3 lib/getaround_utils/log_formatters/deep_key_value.rb
getaround_utils-0.1.2 lib/getaround_utils/log_formatters/deep_key_value.rb
getaround_utils-0.1.1 lib/getaround_utils/log_formatters/deep_key_value.rb
getaround_utils-0.1.0 lib/getaround_utils/log_formatters/deep_key_value.rb