Skip to content
Kward Search API index

Class: Kward::Hooks::HttpHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/hooks/http_handler.rb

Overview

Executes an HTTP lifecycle hook using JSON request/response bodies.

Constant Summary collapse

DEFAULT_TIMEOUT_SECONDS =
10

Instance Method Summary collapse

Constructor Details

#initialize(url:, timeout_seconds: DEFAULT_TIMEOUT_SECONDS, headers: nil, failure_policy: Catalog::DEFAULT_FAILURE_POLICY, http_client: Net::HTTP) ⇒ HttpHandler

Returns a new instance of HttpHandler.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
# File 'lib/kward/hooks/http_handler.rb', line 14

def initialize(url:, timeout_seconds: DEFAULT_TIMEOUT_SECONDS, headers: nil, failure_policy: Catalog::DEFAULT_FAILURE_POLICY, http_client: Net::HTTP)
  @uri = URI.parse(url.to_s)
  raise ArgumentError, "HTTP hook requires http or https URL" unless %w[http https].include?(@uri.scheme)

  @timeout_seconds = positive_timeout(timeout_seconds)
  @headers = stringify_hash(headers || {})
  @failure_policy = Catalog.normalize_failure_policy(failure_policy)
  @http_client = http_client
end

Instance Method Details

#call(event, _context = nil) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/kward/hooks/http_handler.rb', line 24

def call(event, _context = nil)
  response = post(JSON.dump(event.to_h))
  return failure_decision("HTTP hook failed: #{response.code} #{response.message}") unless response.is_a?(Net::HTTPSuccess)

  parse_decision(response.body)
rescue StandardError => e
  failure_decision("HTTP hook failed: #{e.message}")
end