Class: Foobara::RemoteCommand

Inherits:
Command
  • Object
show all
Defined in:
foobara-remote-imports-0.0.11/src/foobara/remote_command.rb

Defined Under Namespace

Classes: UnexpectedError

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from CommandPatternImplementation::Concerns::Subcommands

#is_subcommand

Attributes included from CommandPatternImplementation::Concerns::Runtime

#exception, #outcome

Attributes included from CommandPatternImplementation::Concerns::Errors

#error_collection

Attributes included from CommandPatternImplementation::Concerns::Inputs

#inputs, #raw_inputs

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

install!, reset_all

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods included from CommandPatternImplementation::Concerns::Reflection

#initialize

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

#load_entities, #load_records

Methods included from CommandPatternImplementation::Concerns::Transactions

#auto_detect_current_transactions, #commit_transaction, #open_transaction, #opened_transactions, #relevant_entity_classes, #rollback_transaction, #transactions

Methods included from CommandPatternImplementation::Concerns::StateMachine

#state_machine

Methods included from CommandPatternImplementation::Concerns::Runtime

#halt!, #run, #run!, #run_execute, #succeed, #success?, #validate, #validate_records

Methods included from CommandPatternImplementation::Concerns::Errors

#initialize

Methods included from CommandPatternImplementation::Concerns::Inputs

#initialize, #method_missing, #respond_to_missing?, #respond_to_missing_for_inputs?

Methods included from TruncatedInspect

#inspect, truncating

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::CommandPatternImplementation::Concerns::Inputs

Class Attribute Details

.url_baseObject

Returns the value of attribute url_base.



12
13
14
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 12

def url_base
  @url_base
end

Instance Attribute Details

#parsed_resultObject

Returns the value of attribute parsed_result.



100
101
102
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 100

def parsed_result
  @parsed_result
end

#request_bodyObject

Returns the value of attribute request_body.



100
101
102
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 100

def request_body
  @request_body
end

#request_headersObject

Returns the value of attribute request_headers.



100
101
102
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 100

def request_headers
  @request_headers
end

#responseObject

Returns the value of attribute response.



100
101
102
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 100

def response
  @response
end

#response_bodyObject

Returns the value of attribute response_body.



100
101
102
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 100

def response_body
  @response_body
end

#response_codeObject

Returns the value of attribute response_code.



100
101
102
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 100

def response_code
  @response_code
end

Class Method Details

.subclass(url_base:, description:, inputs:, result:, possible_errors:, name:, base: self) ⇒ Object



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
49
50
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 14

def subclass(
  url_base:,
  description:,
  inputs:,
  result:,
  possible_errors:,
  name:,
  base: self
)
  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.foobara_root_namespace.foobara_lookup_error!(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

.urlObject



52
53
54
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 52

def url
  @url ||= "#{url_base}/run/#{name}"
end

Instance Method Details

#build_request_bodyObject



102
103
104
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 102

def build_request_body
  self.request_body = inputs
end

#build_request_headersObject



106
107
108
109
110
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 106

def build_request_headers
  self.request_headers = {
    "Content-Type" => "application/json"
  }
end

#cast_and_validate_inputsObject

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.



60
61
62
63
64
65
66
67
68
69
70
71
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 60

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

#executeObject



89
90
91
92
93
94
95
96
97
98
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 89

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_requestObject



112
113
114
115
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 112

def issue_http_request
  url = URI.parse(self.url)
  self.response = Net::HTTP.post(url, JSON.generate(request_body), request_headers)
end

#parse_responseObject



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
154
155
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 117

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

#urlObject



85
86
87
# File 'foobara-remote-imports-0.0.11/src/foobara/remote_command.rb', line 85

def url
  self.class.url
end