Class: ActiveWorkflowAgent::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/active_workflow_agent/response.rb

Overview

Helper class to construct responses to ActiveWorkflow’s agent API. docs.activeworkflow.org/remote-agent-api#responses

Instance Method Summary collapse

Constructor Details

#initializeResponse

Create an object for responding to ‘receive’ or ‘check’ methods.



10
11
12
13
14
15
# File 'lib/active_workflow_agent/response.rb', line 10

def initialize
  @logs = []
  @errors = []
  @messages = []
  @memory = {}
end

Instance Method Details

#add_errors(*errors) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_workflow_agent/response.rb', line 30

def add_errors(*errors)
  # Validation.
  must_str = "must be (non-empty) strings"
  not_empty = "can not be empty strings"
  errors.each do |err|
    raise(ArgumentError, "Errors #{must_str}.") unless err.is_a?(String)
    raise(ArgumentError, "Errors #{not_empty}.") if err == ""
  end

  errors.each { |err| @errors << err }
end

#add_logs(*logs) ⇒ Object

Add log messages to the response object.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_workflow_agent/response.rb', line 18

def add_logs(*logs)
  # Validation.
  must_str = "must be (non-empty) strings"
  not_empty = "can not be empty strings"
  logs.each do |log|
    raise(ArgumentError, "Logs #{must_str}.") unless log.is_a?(String)
    raise(ArgumentError, "Logs #{not_empty}.") if log == ""
  end

  logs.each { |log| @logs << log }
end

#add_memory(memory) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
59
# File 'lib/active_workflow_agent/response.rb', line 54

def add_memory(memory)
  must_hash = "must be a hash"
  raise(ArgumentError, "Memory #{must_hash}.") unless memory.is_a?(Hash)

  @memory = memory
end

#add_messages(*messages) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/active_workflow_agent/response.rb', line 42

def add_messages(*messages)
  # Validation.
  must_hash = "must be (non-empty) hashes"
  not_empty = "can not be empty hashes"
  messages.each do |msg|
    raise(ArgumentError, "Messages #{must_hash}.") unless msg.is_a?(Hash)
    raise(ArgumentError, "Messages #{not_empty}.") if msg == {}
  end

  messages.each { |msg| @messages << msg }
end

#to_hObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/active_workflow_agent/response.rb', line 61

def to_h
  {
    "result" => {
      "logs" => @logs,
      "errors" => @errors,
      "messages" => @messages,
      "memory" => @memory
    }
  }
end

#to_jsonObject



72
73
74
# File 'lib/active_workflow_agent/response.rb', line 72

def to_json(*)
  JSON.dump(to_h)
end