Class: RubyCrawl::ServiceClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycrawl/service_client.rb

Overview

Handles node service lifecycle and HTTP requests.

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, node_dir:, node_bin:, node_log:) ⇒ ServiceClient

Returns a new instance of ServiceClient.



10
11
12
13
14
15
16
17
# File 'lib/rubycrawl/service_client.rb', line 10

def initialize(host:, port:, node_dir:, node_bin:, node_log:)
  @host = host
  @port = Integer(port)
  @node_dir = node_dir
  @node_bin = node_bin
  @node_log = node_log
  @node_pid = nil
end

Instance Method Details

#ensure_runningObject



19
20
21
22
23
24
# File 'lib/rubycrawl/service_client.rb', line 19

def ensure_running
  return if healthy?

  start_service
  wait_until_healthy
end

#post_json(path, body) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubycrawl/service_client.rb', line 26

def post_json(path, body)
  uri = URI("http://#{@host}:#{@port}#{path}")
  request = build_request(uri, body)
  response = perform_request(uri, request)
  JSON.parse(response.body)
rescue JSON::ParserError => e
  raise ServiceError, "Node service returned invalid JSON: #{e.message}"
rescue Errno::ECONNREFUSED, Errno::ECONNRESET => e
  raise ServiceError, "Cannot connect to node service at #{uri}: #{e.message}"
rescue Net::OpenTimeout, Net::ReadTimeout => e
  raise TimeoutError, "Request to node service timed out: #{e.message}"
end