Class: Foobara::RemoteCommand
- Defined in:
- foobara-remote-imports-0.0.14/src/foobara/remote_command.rb
Direct Known Subclasses
Defined Under Namespace
Classes: UnexpectedError
Constant Summary
Constants included from TruncatedInspect
Class Attribute Summary collapse
-
.url_base ⇒ Object
Returns the value of attribute url_base.
Instance Attribute Summary collapse
-
#parsed_result ⇒ Object
Returns the value of attribute parsed_result.
-
#request_body ⇒ Object
Returns the value of attribute request_body.
-
#request_headers ⇒ Object
Returns the value of attribute request_headers.
-
#response ⇒ Object
Returns the value of attribute response.
-
#response_body ⇒ Object
Returns the value of attribute response_body.
-
#response_code ⇒ Object
Returns the value of attribute response_code.
Attributes included from CommandPatternImplementation::Concerns::Subcommands
Attributes included from CommandPatternImplementation::Concerns::Runtime
#exception, #outcome, #raw_result
Attributes included from CommandPatternImplementation::Concerns::Errors
Attributes included from CommandPatternImplementation::Concerns::Inputs
Class Method Summary collapse
- .subclass(url_base:, description:, inputs:, result:, possible_errors:, name:, base: RemoteCommand) ⇒ Object
- .url ⇒ Object
Instance Method Summary collapse
- #build_request_body ⇒ Object
- #build_request_headers ⇒ Object
-
#cast_and_validate_inputs ⇒ Object
We need to override this method and let the receiving end perform most of the casting.
- #execute ⇒ Object
- #issue_http_request ⇒ Object
- #parse_response ⇒ Object
- #url ⇒ Object
Methods inherited from Command
Methods included from Concern
foobara_class_methods_module_for, foobara_concern?, included
Methods included from CommandPatternImplementation::Concerns::Reflection
Methods included from CommandPatternImplementation::Concerns::DomainMappers
#domain_map, #domain_map!, #run_mapped_subcommand!
Methods included from CommandPatternImplementation::Concerns::Subcommands
#run_subcommand!, #subcommand?
Methods included from CommandPatternImplementation::Concerns::Entities
Methods included from CommandPatternImplementation::Concerns::Transactions
Methods included from NestedTransactionable
#auto_detect_current_transactions, #commit_transaction, #commit_transaction_if_open, #open_transaction, #opened_transactions, #relevant_entity_classes, relevant_entity_classes_for_type, #relevant_entity_classes_for_type, #rollback_transaction, #transactions, #use_transaction, with_needed_transactions_for_type
Methods included from CommandPatternImplementation::Concerns::StateMachine
Methods included from CommandPatternImplementation::Concerns::Runtime
#halt!, #run, #run!, #run_execute, #succeed, #success?, #validate, #validate_records
Methods included from CommandPatternImplementation::Concerns::Errors
Methods included from CommandPatternImplementation::Concerns::Inputs
#initialize, #method_missing, #respond_to_missing?, #respond_to_missing_for_inputs?
Methods included from TruncatedInspect
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Foobara::CommandPatternImplementation::Concerns::Inputs
Class Attribute Details
.url_base ⇒ Object
Returns the value of attribute url_base.
11 12 13 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 11 def url_base @url_base end |
Instance Attribute Details
#parsed_result ⇒ Object
Returns the value of attribute parsed_result.
98 99 100 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 98 def parsed_result @parsed_result end |
#request_body ⇒ Object
Returns the value of attribute request_body.
98 99 100 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 98 def request_body @request_body end |
#request_headers ⇒ Object
Returns the value of attribute request_headers.
98 99 100 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 98 def request_headers @request_headers end |
#response ⇒ Object
Returns the value of attribute response.
98 99 100 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 98 def response @response end |
#response_body ⇒ Object
Returns the value of attribute response_body.
98 99 100 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 98 def response_body @response_body end |
#response_code ⇒ Object
Returns the value of attribute response_code.
98 99 100 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 98 def response_code @response_code end |
Class Method Details
.subclass(url_base:, description:, inputs:, result:, possible_errors:, name:, base: RemoteCommand) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 13 def subclass( url_base:, description:, inputs:, result:, possible_errors:, name:, base: RemoteCommand ) klass = Util.make_class_p(name, base) klass.url_base = url_base klass.description description klass.inputs inputs klass.result result possible_errors.each_value do |possible_error_manifest| error_class_name = possible_error_manifest.error.reference error_class = Foobara..(error_class_name) key = possible_error_manifest.key symbol = possible_error_manifest.symbol data = possible_error_manifest.processor_manifest_data category = possible_error_manifest.category possible_error = PossibleError.new(error_class, symbol:, data:, key:, category:) if possible_error_manifest.manually_added possible_error.manually_added = true end klass.register_possible_error_class(possible_error) end klass end |
.url ⇒ Object
50 51 52 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 50 def url @url ||= "#{url_base}/run/#{name}" end |
Instance Method Details
#build_request_body ⇒ Object
100 101 102 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 100 def build_request_body self.request_body = inputs end |
#build_request_headers ⇒ Object
104 105 106 107 108 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 104 def build_request_headers self.request_headers = { "Content-Type" => "application/json" } end |
#cast_and_validate_inputs ⇒ Object
We need to override this method and let the receiving end perform most of the casting. One exception is we will find and convert all records to their primary key values instead of sending serialized records over the wire.
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 58 def cast_and_validate_inputs @inputs = if inputs_type.nil? && (raw_inputs.nil? || raw_inputs.empty?) # TODO: test this path # :nocov: {} # :nocov: else serializer_class = CommandConnectors::Serializers::EntitiesToPrimaryKeysSerializer serializer = serializer_class.new(detached_to_primary_key: true) serializer.serialize(raw_inputs) end end |
#execute ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 87 def execute build_request_body build_request_headers # TODO: implement this for queries? # build_query_string issue_http_request parse_response parsed_result end |
#issue_http_request ⇒ Object
110 111 112 113 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 110 def issue_http_request url = URI.parse(self.url) self.response = Net::HTTP.post(url, JSON.generate(request_body), request_headers) end |
#parse_response ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 115 def parse_response self.response_body = response.body self.response_code = response.code if response.is_a?(Net::HTTPSuccess) self.parsed_result = JSON.parse(response_body) elsif response.code.start_with?("4") errors = JSON.parse(response_body) errors.each do |error| case error["category"] when "runtime" e = add_runtime_error( symbol: error["symbol"], message: error["message"], context: error["context"], halt: false ) e.runtime_path = error["runtime_path"] when "data" add_input_error( symbol: error["symbol"], message: error["message"], context: error["context"], path: error["path"] ) else # :nocov: raise "Bad error category: #{error["category"]}" # :nocov: end end halt! else # :nocov: raise UnexpectedError, "#{response.code} from #{url}: #{response.body}" # :nocov: end end |
#url ⇒ Object
83 84 85 |
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_command.rb', line 83 def url self.class.url end |