Class: BSV::Overlay::HTTPSLookupFacilitator

Inherits:
LookupFacilitator show all
Defined in:
lib/bsv/overlay/lookup_facilitator.rb

Overview

Default HTTPS-based lookup facilitator using Net::HTTP.

POSTs to {url}/lookup with a JSON body and the X-Aggregation: yes header, as required by the BSV Overlay Services protocol.

HTTPS is enforced by default; pass allow_http: true to permit plain HTTP (useful for local development and testing).

An injectable http_client may be supplied for testing. It must respond to #request(uri, net_http_request) and return an object with #code and #body.

Instance Method Summary collapse

Constructor Details

#initialize(allow_http: false, http_client: nil) ⇒ HTTPSLookupFacilitator

Returns a new instance of HTTPSLookupFacilitator.

Parameters:

  • allow_http (Boolean) (defaults to: false)

    permit non-HTTPS URLs (default: false)

  • http_client (#request, nil) (defaults to: nil)

    injectable HTTP client for testing



43
44
45
46
47
# File 'lib/bsv/overlay/lookup_facilitator.rb', line 43

def initialize(allow_http: false, http_client: nil)
  super()
  @allow_http  = allow_http
  @http_client = http_client
end

Instance Method Details

#lookup(url, question, timeout: 5) ⇒ LookupAnswer

Perform an HTTPS POST to {url}/lookup and return the parsed answer.

Parameters:

  • url (String)

    base URL of the Overlay Services host

  • question (LookupQuestion)

    the question to send

  • timeout (Integer) (defaults to: 5)

    request timeout in seconds (default: 5)

Returns:

Raises:

  • (ArgumentError)

    if the URL is not HTTPS and allow_http is false

  • (RuntimeError)

    if the server returns a non-2xx status



57
58
59
60
61
62
63
64
65
# File 'lib/bsv/overlay/lookup_facilitator.rb', line 57

def lookup(url, question, timeout: 5)
  validate_url!(url)

  uri     = URI("#{url.chomp('/')}/lookup")
  request = build_request(uri, question)

  response = execute(uri, request, timeout)
  handle_response(response)
end