Class: RubyCrawl::ServiceClient
- Inherits:
-
Object
- Object
- RubyCrawl::ServiceClient
- Defined in:
- lib/rubycrawl/service_client.rb
Overview
Handles node service lifecycle and HTTP requests.
Instance Method Summary collapse
-
#create_session ⇒ String
Create a session for reusing browser context across multiple crawls.
-
#destroy_session(session_id) ⇒ Object
Destroy a session and close its browser context.
- #ensure_running ⇒ Object
-
#initialize(host:, port:, node_dir:, node_bin:, node_log:) ⇒ ServiceClient
constructor
A new instance of ServiceClient.
- #post_json(path, body) ⇒ Object
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
#create_session ⇒ String
Create a session for reusing browser context across multiple crawls.
41 42 43 44 45 46 |
# File 'lib/rubycrawl/service_client.rb', line 41 def create_session response = post_json('/session/create', {}) raise ServiceError, "Failed to create session: #{response['error']}" if response['error'] response['session_id'] end |
#destroy_session(session_id) ⇒ Object
Destroy a session and close its browser context.
50 51 52 53 54 55 |
# File 'lib/rubycrawl/service_client.rb', line 50 def destroy_session(session_id) post_json('/session/destroy', { session_id: session_id }) rescue StandardError # Ignore errors on destroy - context may already be closed nil end |
#ensure_running ⇒ Object
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.}" rescue Errno::ECONNREFUSED, Errno::ECONNRESET => e raise ServiceError, "Cannot connect to node service at #{uri}: #{e.}" rescue Net::OpenTimeout, Net::ReadTimeout => e raise TimeoutError, "Request to node service timed out: #{e.}" end |