Class: Linzer::Message::Adapter::Abstract Abstract
- Inherits:
-
Object
- Object
- Linzer::Message::Adapter::Abstract
- Defined in:
- lib/linzer/message/adapter/abstract.rb
Overview
Subclass and implement #header, #derived, and #field to create a new adapter.
Abstract base class for HTTP message adapters.
Adapters provide a unified interface for accessing HTTP message components regardless of the underlying HTTP library. Each adapter implements field retrieval, header access, and signature attachment for a specific HTTP message type.
Direct Known Subclasses
Generic::Request, Generic::Response, Rack::Request, Rack::Response
Instance Method Summary collapse
-
#[](field) ⇒ String, ...
Retrieves a component value from the message.
-
#attach!(signature) ⇒ Object
Attaches a signature to the underlying HTTP message.
-
#attached_request? ⇒ Boolean
Checks if this response has an attached request.
-
#field?(f) ⇒ Boolean
Checks if a component exists in the message.
-
#has_signature? ⇒ Boolean
Checks whether the request contains HTTP Message Signature headers.
-
#header(name) ⇒ String?
abstract
Retrieves a raw header value by name.
-
#initialize(operation, **options) ⇒ Abstract
constructor
A new instance of Abstract.
-
#request? ⇒ Boolean
Checks if this adapter wraps an HTTP request.
-
#response? ⇒ Boolean
Checks if this adapter wraps an HTTP response.
Constructor Details
Instance Method Details
#[](field) ⇒ String, ...
Retrieves a component value from the message.
Handles both regular header fields and derived components, including parameter processing (‘;sf`, `;bs`, `;req`, `;key`).
72 73 74 75 76 |
# File 'lib/linzer/message/adapter/abstract.rb', line 72 def [](field) field_id = field.is_a?(FieldId) ? field : parse_field_name(field) return nil if field_id.nil? || field_id.item.nil? retrieve(field_id.item, field_id.derived? ? :derived : :field) end |
#attach!(signature) ⇒ Object
Attaches a signature to the underlying HTTP message.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/linzer/message/adapter/abstract.rb', line 101 def attach!(signature) signature_headers = signature.to_h unless has_signature? signature_headers.each { |h, v| set_header!(h, v) } return @operation end signature_headers.each do |hdr, value| merged = Starry.parse_dictionary(String(header(hdr))) merged.merge!(Starry.parse_dictionary(value)) set_header!(hdr, Starry.serialize_dictionary(merged)) end @operation rescue Starry::ParseError => e raise Error, "Cannot attach signature, invalid signature header(s)!", cause: e end |
#attached_request? ⇒ Boolean
Checks if this response has an attached request.
Attached requests enable the ‘;req` parameter for accessing request fields from a response signature.
44 45 46 |
# File 'lib/linzer/message/adapter/abstract.rb', line 44 def attached_request? response? && !!@attached_request end |
#field?(f) ⇒ Boolean
Checks if a component exists in the message.
52 53 54 |
# File 'lib/linzer/message/adapter/abstract.rb', line 52 def field?(f) !!self[f] end |
#has_signature? ⇒ Boolean
Checks whether the request contains HTTP Message Signature headers.
Returns true if either the “signature-input” or “signature” header is present.
93 94 95 |
# File 'lib/linzer/message/adapter/abstract.rb', line 93 def has_signature? !!header("signature-input") || !!header("signature") end |
#header(name) ⇒ String?
Subclasses must implement this method.
Retrieves a raw header value by name.
83 84 85 |
# File 'lib/linzer/message/adapter/abstract.rb', line 83 def header(name) raise Linzer::Error, "Sub-classes are required to implement this method!" end |
#request? ⇒ Boolean
Checks if this adapter wraps an HTTP request.
28 29 30 |
# File 'lib/linzer/message/adapter/abstract.rb', line 28 def request? self.class.to_s.include?("Request") end |
#response? ⇒ Boolean
Checks if this adapter wraps an HTTP response.
34 35 36 |
# File 'lib/linzer/message/adapter/abstract.rb', line 34 def response? self.class.to_s.include?("Response") end |