lib/boxcars/boxcar.rb in boxcars-0.2.0 vs lib/boxcars/boxcar.rb in boxcars-0.2.1

- old
+ new

@@ -60,42 +60,52 @@ # @param args [Array] The positional arguments to pass to the boxcar. # @param kwargs [Hash] The keyword arguments to pass to the boxcar. # you can pass one or the other, but not both. # @return [String] The answer to the question. def run(*args, **kwargs) + rv = conduct(*args, **kwargs) + rv.is_a?(Result) ? rv.to_answer : rv + end + + # Get an extended answer from the boxcar. + # @param args [Array] The positional arguments to pass to the boxcar. + # @param kwargs [Hash] The keyword arguments to pass to the boxcar. + # you can pass one or the other, but not both. + # @return [Boxcars::Result] The answer to the question. + def conduct(*args, **kwargs) Boxcars.info "> Entering #{name}#run", :gray, style: :bold - rv = do_run(*args, **kwargs) + rv = depart(*args, **kwargs) Boxcars.info "< Exiting #{name}#run", :gray, style: :bold rv end private # Get an answer from the boxcar. - def do_call(inputs:, return_only_outputs: false) + def run_boxcar(inputs:, return_only_outputs: false) inputs = our_inputs(inputs) output = nil begin output = call(inputs: inputs) rescue StandardError => e Boxcars.error "Error in #{name} boxcar#call: #{e}", :red raise e end validate_outputs(outputs: output.keys) - # memory&.save_convext(inputs: inputs, outputs: outputs) return output if return_only_outputs inputs.merge(output) end - def do_run(*args, **kwargs) + # line up parameters and run boxcar + def depart(*args, **kwargs) if kwargs.empty? raise Boxcars::ArgumentError, "run supports only one positional argument." if args.length != 1 - return do_call(inputs: args[0])[output_keys.first] + return run_boxcar(inputs: args[0])[output_keys.first] end - return do_call(**kwargs)[output_keys].first if args.empty? + return run_boxcar(**kwargs)[output_keys].first if args.empty? raise Boxcars::ArgumentError, "run supported with either positional or keyword arguments but not both. Got args" \ ": #{args} and kwargs: #{kwargs}." end @@ -112,14 +122,15 @@ validate_inputs(inputs: inputs) end # the default answer is the text passed in def get_answer(text) - text + Result.from_text(text) end end end +require "boxcars/result" require "boxcars/boxcar/engine_boxcar" require "boxcars/boxcar/calculator" require "boxcars/boxcar/google_search" require "boxcars/boxcar/sql" require "boxcars/boxcar/active_record"