Class: Boxcars::Result
- Inherits:
-
Object
- Object
- Boxcars::Result
- Defined in:
- lib/boxcars/result.rb
Overview
used by Boxcars to return structured result and additional context
Instance Attribute Summary collapse
-
#added_context ⇒ Object
Returns the value of attribute added_context.
-
#answer ⇒ Object
Returns the value of attribute answer.
-
#explanation ⇒ Object
Returns the value of attribute explanation.
-
#status ⇒ Object
Returns the value of attribute status.
-
#suggestions ⇒ Object
Returns the value of attribute suggestions.
Class Method Summary collapse
-
.from_error(error, **kwargs) ⇒ Boxcars::Result
create a new Result from an error string.
-
.from_text(text, **kwargs) ⇒ Boxcars::Result
create a new Result from a text string.
Instance Method Summary collapse
-
#initialize(status:, answer: nil, explanation: nil, suggestions: nil, **added_context) ⇒ Result
constructor
A new instance of Result.
-
#to_answer ⇒ String
The answer data to the question.
-
#to_h ⇒ Hash
The result as a hash.
-
#to_json(*args) ⇒ String
The result as a json string.
-
#to_s ⇒ String
An explanation of the result.
Constructor Details
#initialize(status:, answer: nil, explanation: nil, suggestions: nil, **added_context) ⇒ Result
Returns a new instance of 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_context ⇒ Object
Returns the value of attribute added_context.
6 7 8 |
# File 'lib/boxcars/result.rb', line 6 def added_context @added_context end |
#answer ⇒ Object
Returns the value of attribute answer.
6 7 8 |
# File 'lib/boxcars/result.rb', line 6 def answer @answer end |
#explanation ⇒ Object
Returns the value of attribute explanation.
6 7 8 |
# File 'lib/boxcars/result.rb', line 6 def explanation @explanation end |
#status ⇒ Object
Returns the value of attribute status.
6 7 8 |
# File 'lib/boxcars/result.rb', line 6 def status @status end |
#suggestions ⇒ Object
Returns the value of attribute suggestions.
6 7 8 |
# File 'lib/boxcars/result.rb', line 6 def suggestions @suggestions end |
Class Method Details
.from_error(error, **kwargs) ⇒ Boxcars::Result
create a new Result from an error string
61 62 63 64 65 66 |
# File 'lib/boxcars/result.rb', line 61 def self.from_error(error, **kwargs) answer = error answer = Regexp.last_match(:answer) if answer =~ /^Error:\s*(?<answer>.*)$/ explanation = "Error: #{answer}" new(status: :error, answer: answer, explanation: explanation, **kwargs) end |
.from_text(text, **kwargs) ⇒ Boxcars::Result
create a new Result from a text string
50 51 52 53 54 55 |
# File 'lib/boxcars/result.rb', line 50 def self.from_text(text, **kwargs) answer = text.delete_prefix('"').delete_suffix('"').strip answer = Regexp.last_match(:answer) if answer =~ /^Answer:\s*(?<answer>.*)$/ explanation = "Answer: #{answer}" new(status: :ok, answer: answer, explanation: explanation, **kwargs) end |
Instance Method Details
#to_answer ⇒ String
Returns The answer data to the question.
42 43 44 |
# File 'lib/boxcars/result.rb', line 42 def to_answer answer end |
#to_h ⇒ Hash
Returns The result as a hash.
22 23 24 25 26 27 28 29 |
# File 'lib/boxcars/result.rb', line 22 def to_h { status: status, answer: answer, explanation: explanation, suggestions: suggestions }.merge(added_context).compact end |
#to_json(*args) ⇒ String
Returns The result as a json string.
32 33 34 |
# File 'lib/boxcars/result.rb', line 32 def to_json(*args) JSON.generate(to_h, *args) end |
#to_s ⇒ String
Returns An explanation of the result.
37 38 39 |
# File 'lib/boxcars/result.rb', line 37 def to_s explanation end |