Module: GetaroundUtils::Utils::DeepKeyValue

Defined in:
lib/getaround_utils/utils/deep_key_value.rb

Class Method Summary collapse

Class Method Details

.escape(value, max_length = 512) ⇒ Object



8
9
10
11
12
# File 'lib/getaround_utils/utils/deep_key_value.rb', line 8

def self.escape(value, max_length = 512)
  value = value[1...-1] if /^".*"$/.match?(value)
  value = "#{value[0...max_length]} ..." if value.length >= max_length
  value.inspect
end

.flattify(value, result = {}, path = [], max_depth = 5) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/getaround_utils/utils/deep_key_value.rb', line 30

def self.flattify(value, result = {}, path = [], max_depth = 5)
  if path.length > max_depth
    result[path.join(".")] = '...'
    return result
  end
  case value
  when Array
    value.each.with_index(0) do |v, i|
      flattify(v, result, path + [i], max_depth)
    end
  when Hash
    value.each do |k, v|
      flattify(v, result, path + [k], max_depth)
    end
  when Numeric
    result[path.join(".")] = value
  when String
    result[path.join(".")] = value
  else
    flattify(value.to_s, result, path, max_depth)
  end
  result
end

.serialize(data) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/getaround_utils/utils/deep_key_value.rb', line 14

def self.serialize(data)
  case data
  when Array
    flattify(data).map { |key, value| "#{key}=#{serialize(value)}" }.join(' ')
  when Hash
    flattify(data).map { |key, value| "#{key}=#{serialize(value)}" }.join(' ')
  when Numeric
    data.to_s
  when String
    escape(data)
  else
    escape(data.to_s)
  end
end