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



5
6
7
8
9
# File 'lib/getaround_utils/utils/deep_key_value.rb', line 5

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

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



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

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/getaround_utils/utils/deep_key_value.rb', line 11

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