Class: Foobara::RemoteCommand

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

Direct Known Subclasses

AuthenticatedRemoteCommand

Defined Under Namespace

Classes: UnexpectedError

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

install!

Methods included from CommandPatternImplementation

install_state_machine_callback_methods

Methods included from TruncatedInspect

#inspect, truncating

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Class Attribute Details

.url_base ⇒ Object

Returns the value of attribute url_base.



11
12
13
# File '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 '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 '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 '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 '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 '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 '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 '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.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

.url ⇒ Object



50
51
52
# File '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 '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 '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 '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
              # simplecov:disable
              {}
              # simplecov:enable
            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 '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 '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 '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
        # simplecov:disable
        raise "Bad error category: #{error["category"]}"
        # simplecov:enable
      end
    end

    halt!
  else
    # simplecov:disable
    raise UnexpectedError, "#{response.code} from #{url}: #{response.body}"
    # simplecov:enable
  end
end

#url ⇒ Object



83
84
85
# File 'src/foobara/remote_command.rb', line 83

def url
  self.class.url
end