Module: Termfront::Mission::EventLoader

Defined in:
lib/termfront/mission/event_loader.rb

Class Method Summary collapse

Class Method Details

.blank?(value) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/termfront/mission/event_loader.rb', line 82

def blank?(value)
  value.nil? || value == ""
end

.load_file(path) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/termfront/mission/event_loader.rb', line 10

def load_file(path)
  return [] unless File.file?(path)

  doc = JSON.parse(File.read(path))
  events = doc.fetch("events") do
    raise ArgumentError, "event file #{path} is missing top-level events array"
  end

  unless events.is_a?(Array)
    raise ArgumentError, "event file #{path} must define events as an array"
  end

  events.map.with_index do |event, index|
    validate_event!(event, path, index)
  end
end

.symbolize_keys(value) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/termfront/mission/event_loader.rb', line 69

def symbolize_keys(value)
  case value
  when Array
    value.map { |item| symbolize_keys(item) }
  when Hash
    value.each_with_object({}) do |(key, item), memo|
      memo[key.to_sym] = symbolize_keys(item)
    end
  else
    value
  end
end

.validate_action!(action, path, event_id, index) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
67
# File 'lib/termfront/mission/event_loader.rb', line 58

def validate_action!(action, path, event_id, index)
  unless action.is_a?(Hash)
    raise ArgumentError, "action #{index} in event #{event_id} (#{path}) must be an object"
  end

  type = action["type"]
  raise ArgumentError, "action #{index} in event #{event_id} (#{path}) is missing type" if blank?(type)

  symbolize_keys(action).merge(type: type.to_sym)
end

.validate_event!(event, path, index) ⇒ Object

Raises:

  • (ArgumentError)


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
54
55
56
# File 'lib/termfront/mission/event_loader.rb', line 27

def validate_event!(event, path, index)
  unless event.is_a?(Hash)
    raise ArgumentError, "event #{index} in #{path} must be an object"
  end

  id = event["id"]
  trigger = event["trigger"]
  actions = event["actions"]

  raise ArgumentError, "event #{index} in #{path} is missing id" if blank?(id)
  raise ArgumentError, "event #{id} in #{path} is missing trigger" unless trigger.is_a?(Hash)
  raise ArgumentError, "event #{id} in #{path} is missing actions" unless actions.is_a?(Array) && !actions.empty?

  trigger_type = trigger["type"]
  raise ArgumentError, "event #{id} in #{path} trigger is missing type" if blank?(trigger_type)

  normalized_actions = actions.map.with_index do |action, action_index|
    validate_action!(action, path, id, action_index)
  end

  normalized_trigger = symbolize_keys(trigger).merge(type: trigger_type.to_sym)
  normalized_trigger[:terminal_id] = normalized_trigger[:terminal_id].to_sym if normalized_trigger[:terminal_id].is_a?(String)

  {
    id: id,
    once: event.fetch("once", true),
    trigger: normalized_trigger,
    actions: normalized_actions
  }
end