class JsonReducer::Mask
Public Class Methods
new(key)
click to toggle source
# File lib/json_reducer/mask.rb, line 3 def initialize(key) @schema = JsonReducer::Schemas.instance.get(key) end
Public Instance Methods
apply(payload)
click to toggle source
# File lib/json_reducer/mask.rb, line 7 def apply(payload) payload = parse_record(payload).dup apply!(payload, @schema) payload end
Private Instance Methods
apply!(payload, schema)
click to toggle source
# File lib/json_reducer/mask.rb, line 16 def apply!(payload, schema) return if schema.dig('properties').nil? sliced = slice!(payload, schema['properties'].keys) handle(schema['properties'], sliced) end
handle(properties, payload)
click to toggle source
# File lib/json_reducer/mask.rb, line 23 def handle(properties, payload) properties.each do |key, property| case property['type'] when 'array' payload[key].each { |hash| apply!(hash, property) } when 'object' apply!(payload[key], property) end end end
parse_record(schema)
click to toggle source
# File lib/json_reducer/mask.rb, line 34 def parse_record(schema) schema.is_a?(String) ? JSON.parse(schema) : schema end
slice!(hash, keys)
click to toggle source
# File lib/json_reducer/mask.rb, line 38 def slice!(hash, keys) sliced = hash.slice(*keys) sliced.default = hash.default sliced.default_proc = hash.default_proc if hash.default_proc hash.replace(sliced) end