Class: CommandTower::Deserializers::Intervention::EnvelopeDeserializer

Inherits:
ApplicationDeserializer show all
Defined in:
app/deserializers/command_tower/deserializers/intervention/envelope_deserializer.rb

Overview

Parses a canonical intervention envelope from untrusted params/JSON.

Defined Under Namespace

Classes: Input

Instance Method Summary collapse

Methods inherited from ApplicationDeserializer

call

Instance Method Details

#call(params) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/deserializers/command_tower/deserializers/intervention/envelope_deserializer.rb', line 10

def call(params)
  source = normalize_source(params)
  action = required_text(source[:action] || source["action"], field: "action")
  return action if deserializer_result?(action)

  allowed_raw = source[:allowed].nil? ? source["allowed"] : source[:allowed]
  allowed = allowed_raw == true || allowed_raw.to_s == "true"

  blockers_raw = source[:blockers] || source["blockers"] || []
  unless blockers_raw.is_a?(Array)
    return failure(errors: [{ field: "blockers", code: "invalid_type", message: "must be an array" }])
  end

  blockers = []
  blockers_raw.each_with_index do |row, index|
    parsed = parse_blocker(row, index)
    return parsed if deserializer_result?(parsed)

    blockers << parsed
  end

  success(Input.new(action: action, allowed: allowed, blockers: blockers))
end