Class: GetaroundUtils::Utils::DeepKeyValueSerializer
- Inherits:
-
Object
- Object
- GetaroundUtils::Utils::DeepKeyValueSerializer
- Defined in:
- lib/getaround_utils/utils/deep_key_value_serializer.rb
Direct Known Subclasses
Instance Method Summary collapse
- #flattify(value, result = {}, path = []) ⇒ Object
-
#initialize(max_depth: 5, max_length: 512) ⇒ DeepKeyValueSerializer
constructor
A new instance of DeepKeyValueSerializer.
- #serialize(data) ⇒ Object
Constructor Details
#initialize(max_depth: 5, max_length: 512) ⇒ DeepKeyValueSerializer
Returns a new instance of DeepKeyValueSerializer.
5 6 7 8 |
# File 'lib/getaround_utils/utils/deep_key_value_serializer.rb', line 5 def initialize(max_depth: 5, max_length: 512) @max_depth = max_depth @max_length = max_length end |
Instance Method Details
#flattify(value, result = {}, path = []) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/getaround_utils/utils/deep_key_value_serializer.rb', line 26 def flattify(value, result = {}, path = []) 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]) end when Hash value.each do |k, v| flattify(v, result, path + [k]) end when Numeric result[path.join(".")] = value.to_s when String value = if value =~ /^".*"$/ value.length >= @max_length ? %{#{value[0...@max_length]} ..."} : value else value.length >= @max_length ? %{#{value[0...@max_length]} ...}.inspect : value.inspect end result[path.join(".")] = value else flattify(value.to_s, result, path) end result end |
#serialize(data) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/getaround_utils/utils/deep_key_value_serializer.rb', line 10 def serialize(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 |