Class: Zenrows::Hooks::Context Private

Inherits:
Object
  • Object
show all
Defined in:
lib/zenrows/hooks/context.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Builds context hashes for hook callbacks

Provides methods to create and enrich context objects passed to hooks. Handles parsing of ZenRows-specific response headers.

Author:

  • Ernest Bursa

Since:

  • 0.3.0

Constant Summary collapse

ZENROWS_HEADERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

ZenRows response headers to parse

Since:

  • 0.3.0

{
  "Concurrency-Limit" => :concurrency_limit,
  "Concurrency-Remaining" => :concurrency_remaining,
  "X-Request-Cost" => :request_cost,
  "X-Request-Id" => :request_id,
  "Zr-Final-Url" => :final_url
}.freeze

Class Method Summary collapse

Class Method Details

.enrich_with_response(context, response) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Enrich context with response data

Adds timing information and parses ZenRows headers from response.

Parameters:

  • context (Hash)

    Existing request context

  • response (Object)

    HTTP response object

Returns:

  • (Hash)

    Enriched context

Since:

  • 0.3.0



52
53
54
55
56
57
58
# File 'lib/zenrows/hooks/context.rb', line 52

def enrich_with_response(context, response)
  headers = extract_headers(response)
  context[:zenrows_headers] = parse_zenrows_headers(headers)
  context[:completed_at] = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  context[:duration] = context[:completed_at] - context[:started_at]
  context
end

.for_request(method:, url:, options:, backend:) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build context for a request

Parameters:

  • method (Symbol)

    HTTP method (:get, :post, etc.)

  • url (String)

    Target URL

  • options (Hash)

    ZenRows options used for the request

  • backend (Symbol)

    Backend name (:http_rb, :net_http)

Returns:

  • (Hash)

    Request context

Since:

  • 0.3.0



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zenrows/hooks/context.rb', line 31

def for_request(method:, url:, options:, backend:)
  uri = parse_uri(url)

  {
    method: method,
    url: url,
    host: uri&.host,
    options: options.dup.freeze,
    started_at: Process.clock_gettime(Process::CLOCK_MONOTONIC),
    backend: backend,
    zenrows_headers: {}
  }
end