Module: Linzer::HTTP

Extended by:
HTTP
Included in:
HTTP
Defined in:
lib/linzer/http.rb,
lib/linzer/http/bootstrap.rb,
lib/linzer/http/signature_feature.rb

Overview

Simple HTTP client with automatic request signing.

This module provides convenience methods for making signed HTTP requests using Net::HTTP. It automatically signs outgoing requests with the provided key.

For each standard HTTP method (GET, POST, PUT, DELETE, etc.), a corresponding method is dynamically defined.

Examples:

Making a signed GET request

key = Linzer.generate_ed25519_key("my-key")
response = Linzer::HTTP.get("https://example.com/api", key: key)

Making a signed POST request with body

response = Linzer::HTTP.post("https://example.com/api",
  key: key,
  data: { "name" => "value" }.to_json,
  headers: { "Content-Type" => "application/json" }
)

Customizing covered components

response = Linzer::HTTP.get("https://example.com/api",
  key: key,
  covered_components: %w[@method @authority @path date content-type]
)

See Also:

Defined Under Namespace

Modules: Bootstrap Classes: SignatureFeature

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.known_http_methodsArray<String>

Returns the list of known HTTP methods from Net::HTTP.

Returns:

  • (Array<String>)

    HTTP method names (e.g., “GET”, “POST”)



38
39
40
41
42
43
44
45
# File 'lib/linzer/http.rb', line 38

def self.known_http_methods
  Net::HTTP
    .constants
    .map    { |const| Net::HTTP.const_get(const) }
    .select { |klass| klass.is_a?(Class) && klass.const_defined?(:METHOD) }
    .map    { |klass| klass::METHOD }
    .freeze
end

.register_adapterObject

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.

Registers the HTTP gem request adapter.



10
11
12
13
14
# File 'lib/linzer/http/signature_feature.rb', line 10

def register_adapter
  request_class = ::HTTP::Request
  adapter_class = Linzer::Message::Adapter::HTTPGem::Request
  Linzer::Message.register_adapter(request_class, adapter_class)
end

Instance Method Details

#get(uri, options) ⇒ Net::HTTPResponse

Makes a signed GET request.

Parameters:

  • uri (String)

    The request URI

  • options (Hash)

    Request options

Options Hash (options):

  • :key (Key)

    The signing key (required)

  • :headers (Hash)

    Additional headers

  • :covered_components (Array<String>)

    Components to sign

  • :params (Hash)

    Additional signature parameters

  • :debug (Boolean)

    Enable debug output

Returns:

  • (Net::HTTPResponse)

    The response



70
71
72
73
74
75
76
# File 'lib/linzer/http.rb', line 70

known_http_methods.each do |http_method|  # e.g.:
  method = http_method.downcase.to_sym    #
  define_method(method) do |uri, options| # def post(uri, **options)
    options ||= {}                        #   request :post, uri, options
    request method, uri, options          # end
  end
end

#post(uri, options) ⇒ Net::HTTPResponse

Makes a signed POST request.

Parameters:

  • uri (String)

    The request URI

  • options (Hash)

    Request options

Options Hash (options):

  • :key (Key)

    The signing key (required)

  • :data (String)

    Request body (required for POST)

  • :headers (Hash)

    Additional headers

  • :covered_components (Array<String>)

    Components to sign

  • :params (Hash)

    Additional signature parameters

Returns:

  • (Net::HTTPResponse)

    The response



70
71
72
73
74
75
76
# File 'lib/linzer/http.rb', line 70

known_http_methods.each do |http_method|  # e.g.:
  method = http_method.downcase.to_sym    #
  define_method(method) do |uri, options| # def post(uri, **options)
    options ||= {}                        #   request :post, uri, options
    request method, uri, options          # end
  end
end