Module: Paradocs::Whitelist::ClassMethods

Defined in:
lib/paradocs/whitelist.rb

Instance Method Summary collapse

Instance Method Details

#filter!(payload, source_schema) ⇒ Object



27
28
29
30
31
# File 'lib/paradocs/whitelist.rb', line 27

def filter!(payload, source_schema)
  schema  = source_schema.clone
  context = Context.new(nil, Top.new, @environment, source_schema.subschemes.clone)
  resolve(payload, schema, context)
end

#resolve(payload, schema, context) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/paradocs/whitelist.rb', line 33

def resolve(payload, schema, context)
  filtered_payload = {}
  coercion_block = Paradocs.config.whitelist_coercion
  coercion_block = coercion_block.is_a?(Proc) && coercion_block
  payload.dup.each do |key, value|
    key    = key.to_sym
    schema = Schema.new if schema.nil?
    schema.send(:flush!)
    schema.send(:invoke_subschemes!, payload, context)
    meta = (schema, key)
    if value.is_a?(Hash)
      field_schema = find_schema_by(schema, key)
      value        = resolve(value, field_schema, context)
    elsif value.is_a?(Array)
      value = value.map do |v|
        if v.is_a?(Hash)
          field_schema = find_schema_by(schema, key)
          resolve(v, field_schema, context)
        else
          v = FILTERED unless whitelisted?(meta, key)
          v
        end
      end
    else

      value = if whitelisted?(meta, key)
        coercion_block ? coercion_block.call(value, meta) : value
      elsif value.nil? || value.try(:blank?) || value.try(:empty?)
        !!value == value ? value : EMPTY
      else
        FILTERED
      end
      value
    end
    filtered_payload[key] = value
  end

  filtered_payload
end