Module: Mpp::Http::Headers

Extended by:
T::Sig
Defined in:
lib/mpp/http/headers.rb

Overview

Shared {url:, headers:} helpers used by the x402 facilitator and Tempo fee-payer and relay HTTP clients.

headers may be:

* a Hash of static header names/values
* a zero-arity proc returning that Hash
* a proc receiving the request path (or JSON-RPC method) and returning
that Hash
* a nested Hash keyed by operation (`"verify"`, `"broadcast"`, …)

Class Method Summary collapse

Class Method Details

.normalize_base_url(url) ⇒ Object



21
22
23
24
25
# File 'lib/mpp/http/headers.rb', line 21

def normalize_base_url(url)
  base = url.to_s
  base = base.chomp("/") while base.end_with?("/")
  base
end

.resolve(source, path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mpp/http/headers.rb', line 44

def resolve(source, path)
  return {} if source.nil?

  if source.respond_to?(:call)
    arity = source.respond_to?(:arity) ? source.arity : 1
    source = (arity == 0) ? source.call : source.call(path)
  end
  return {} unless source.is_a?(Hash)

  operation = path.to_s.delete_prefix("/")
  last = T.let(operation.split("/").last, T.nilable(String))
  keyed = source[operation] || source[operation.to_sym]
  keyed ||= source[last] || source[last.to_sym] if last && last != operation
  stringify(keyed.is_a?(Hash) ? keyed : source)
end

.stringify(headers) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/mpp/http/headers.rb', line 35

def stringify(headers)
  return {} if headers.nil?

  headers.each_with_object({}) do |(key, value), acc|
    acc[key.to_s] = value.to_s
  end
end

.symbolize(config) ⇒ Object



28
29
30
31
32
# File 'lib/mpp/http/headers.rb', line 28

def symbolize(config)
  config.each_with_object({}) do |(key, value), acc|
    acc[key.to_sym] = value
  end
end