Class: Boxcars::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/boxcars/result.rb

Overview

used by Boxcars to return structured result and additional context

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, answer: nil, explanation: nil, suggestions: nil, **added_context) ⇒ Result

Returns a new instance of Result.

Parameters:

  • status (Symbol)

    :ok or :error

  • answer (String) (defaults to: nil)

    The answer to the question

  • explanation (String) (defaults to: nil)

    The explanation of the answer

  • suggestions (Array<String>) (defaults to: nil)

    The next suggestions for the user

  • added_context (Hash)

    Any additional context to add to the result



13
14
15
16
17
18
19
# File 'lib/boxcars/result.rb', line 13

def initialize(status:, answer: nil, explanation: nil, suggestions: nil, **added_context)
  @status = status
  @answer = answer || explanation
  @explanation = explanation
  @suggestions = suggestions
  @added_context = added_context
end

Instance Attribute Details

#added_contextObject

Returns the value of attribute added_context.



6
7
8
# File 'lib/boxcars/result.rb', line 6

def added_context
  @added_context
end

#answerObject

Returns the value of attribute answer.



6
7
8
# File 'lib/boxcars/result.rb', line 6

def answer
  @answer
end

#explanationObject

Returns the value of attribute explanation.



6
7
8
# File 'lib/boxcars/result.rb', line 6

def explanation
  @explanation
end

#statusObject

Returns the value of attribute status.



6
7
8
# File 'lib/boxcars/result.rb', line 6

def status
  @status
end

#suggestionsObject

Returns the value of attribute suggestions.



6
7
8
# File 'lib/boxcars/result.rb', line 6

def suggestions
  @suggestions
end

Class Method Details

.extract(value) ⇒ Boxcars::Result?

Extract a Boxcars::Result from common boxcar return values.

Parameters:

  • value (Object)

    Usually a ‘Boxcar#conduct` hash or a `Boxcars::Result`.

Returns:



77
78
79
80
81
82
83
84
# File 'lib/boxcars/result.rb', line 77

def self.extract(value)
  return value if value.is_a?(Result)
  return value.answer_result if value.respond_to?(:answer_result)
  return nil unless value.is_a?(Hash)

  candidate = value[:answer] || value["answer"]
  candidate if candidate.is_a?(Result)
end

.from_error(error) ⇒ Boxcars::Result

create a new Result from an error string

Parameters:

  • error (String)

    The error to use for the result

  • kwargs (Hash)

    Any additional kwargs to pass to the result

Returns:



67
68
69
70
71
72
# File 'lib/boxcars/result.rb', line 67

def self.from_error(error, **)
  answer = error
  answer = Regexp.last_match(:answer) if answer =~ /^Error:\s*(?<answer>.*)$/
  explanation = "Error: #{answer}"
  new(status: :error, answer:, explanation:, **)
end

.from_text(text) ⇒ Boxcars::Result

create a new Result from a text string

Parameters:

  • text (String)

    The text to use for the result

  • kwargs (Hash)

    Any additional kwargs to pass to the result

Returns:



55
56
57
58
59
60
61
# File 'lib/boxcars/result.rb', line 55

def self.from_text(text, **)
  str = text.to_s
  answer = str.delete_prefix('"').delete_suffix('"').strip
  answer = Regexp.last_match(:answer) if answer =~ /^Answer:\s*(?<answer>.*)$/
  explanation = "Answer: #{answer}"
  new(status: :ok, answer:, explanation:, **)
end

.valid_conduct_payload?(value) ⇒ Boolean

Validate that a value is a conduct-style payload containing a ‘Boxcars::Result`.

Parameters:

  • value (Object)

    Usually a ‘Boxcar#conduct` hash.

Returns:

  • (Boolean)

    True when a ‘Boxcars::Result` can be extracted.



89
90
91
# File 'lib/boxcars/result.rb', line 89

def self.valid_conduct_payload?(value)
  !extract(value).nil?
end

Instance Method Details

#error?Boolean

Returns True when result status is ‘:error`.

Returns:

  • (Boolean)

    True when result status is ‘:error`.



47
48
49
# File 'lib/boxcars/result.rb', line 47

def error?
  status == :error
end

#ok?Boolean

Returns True when result status is ‘:ok`.

Returns:

  • (Boolean)

    True when result status is ‘:ok`.



42
43
44
# File 'lib/boxcars/result.rb', line 42

def ok?
  status == :ok
end

#to_answerString

Returns The answer data to the question.

Returns:

  • (String)

    The answer data to the question



37
38
39
# File 'lib/boxcars/result.rb', line 37

def to_answer
  answer
end

#to_hHash

Returns The result as a hash.

Returns:

  • (Hash)

    The result as a hash



22
23
24
# File 'lib/boxcars/result.rb', line 22

def to_h
  { status:, answer:, explanation:, suggestions: }.merge(added_context).compact
end

#to_jsonString

Returns The result as a json string.

Returns:

  • (String)

    The result as a json string



27
28
29
# File 'lib/boxcars/result.rb', line 27

def to_json(*)
  JSON.generate(to_h, *)
end

#to_sString

Returns An explanation of the result.

Returns:

  • (String)

    An explanation of the result



32
33
34
# File 'lib/boxcars/result.rb', line 32

def to_s
  explanation
end