Module: Philiprehberger::HeaderKit

Defined in:
lib/philiprehberger/header_kit.rb,
lib/philiprehberger/header_kit/cors.rb,
lib/philiprehberger/header_kit/etag.rb,
lib/philiprehberger/header_kit/link.rb,
lib/philiprehberger/header_kit/accept.rb,
lib/philiprehberger/header_kit/cookie.rb,
lib/philiprehberger/header_kit/version.rb,
lib/philiprehberger/header_kit/security.rb,
lib/philiprehberger/header_kit/forwarded.rb,
lib/philiprehberger/header_kit/negotiation.rb,
lib/philiprehberger/header_kit/retry_after.rb,
lib/philiprehberger/header_kit/content_type.rb,
lib/philiprehberger/header_kit/authorization.rb,
lib/philiprehberger/header_kit/cache_control.rb,
lib/philiprehberger/header_kit/accept_builder.rb,
lib/philiprehberger/header_kit/accept_encoding.rb,
lib/philiprehberger/header_kit/accept_language.rb

Defined Under Namespace

Modules: Accept, AcceptBuilder, AcceptEncoding, AcceptLanguage, Authorization, CacheControl, ContentType, Cookie, Cors, Etag, Forwarded, Link, Negotiation, RetryAfter, Security Classes: Error

Constant Summary collapse

VERSION =
'0.5.0'

Class Method Summary collapse

Class Method Details

.build_accept(types) ⇒ String

Build an Accept header string from an array of type hashes.

Parameters:

  • types (Array<Hash>)

    each with :type and optional :quality

Returns:

  • (String)

    formatted Accept header value



36
37
38
# File 'lib/philiprehberger/header_kit.rb', line 36

def self.build_accept(types)
  AcceptBuilder.build(types)
end

.build_cache_control(directives) ⇒ String

Build a Cache-Control header string from a directive hash.

Parameters:

  • directives (Hash{Symbol => true, Integer, String})

    directive hash

Returns:

  • (String)

    formatted Cache-Control header value



85
86
87
# File 'lib/philiprehberger/header_kit.rb', line 85

def self.build_cache_control(directives)
  CacheControl.build(directives)
end

.build_cors(**options) ⇒ Hash{String => String}

Build CORS response headers.

Parameters:

  • options (Hash)

    CORS options (origin:, methods:, headers:, max_age:, credentials:, expose:)

Returns:

  • (Hash{String => String})

    response headers



152
153
154
# File 'lib/philiprehberger/header_kit.rb', line 152

def self.build_cors(**options)
  Cors.build(**options)
end

Build a Link header string from an array of link hashes.

Parameters:

  • links (Array<Hash>)

    each with :uri and optional :rel, :type, :title

Returns:

  • (String)

    formatted Link header value



127
128
129
# File 'lib/philiprehberger/header_kit.rb', line 127

def self.build_link(links)
  Link.build(links)
end

Build a Set-Cookie header string.

Parameters:

  • name (String)

    cookie name

  • value (String)

    cookie value

  • options (Hash)

    optional attributes (expires:, max_age:, secure:, httponly:, samesite:, path:, domain:)

Returns:

  • (String)

    formatted Set-Cookie header value



111
112
113
# File 'lib/philiprehberger/header_kit.rb', line 111

def self.build_set_cookie(name, value, **options)
  Cookie.build_set_cookie(name, value, **options)
end

.etag_match?(header_value, resource_etag) ⇒ Boolean

Check whether an If-None-Match / If-Match header matches a resource ETag.

Accepts a single ETag, comma-separated list, weak-prefixed values (e.g. ‘W/“abc”`), or the `*` wildcard. Weak and strong ETags collapse to equality on the inner token.

Parameters:

  • header_value (String, nil)

    the raw header value

  • resource_etag (String, nil)

    the resource ETag (unquoted, no ‘W/` prefix)

Returns:

  • (Boolean)

    true if any value in the header matches the resource



189
190
191
# File 'lib/philiprehberger/header_kit.rb', line 189

def self.etag_match?(header_value, resource_etag)
  Etag.match?(header_value, resource_etag)
end

.negotiate(accept_header, available) ⇒ String?

Find the best matching media type via content negotiation.

Parameters:

  • accept_header (String)

    the Accept header value

  • available (Array<String>)

    list of available media types

Returns:

  • (String, nil)

    the best matching type, or nil if no match



136
137
138
# File 'lib/philiprehberger/header_kit.rb', line 136

def self.negotiate(accept_header, available)
  Negotiation.negotiate(accept_header, available)
end

.negotiate_language(header, available) ⇒ String?

Negotiate the best language from an Accept-Language header.

Parameters:

  • header (String)

    the Accept-Language header value

  • available (Array<String>)

    list of available language tags

Returns:

  • (String, nil)

    the best matching language, or nil if no match



61
62
63
# File 'lib/philiprehberger/header_kit.rb', line 61

def self.negotiate_language(header, available)
  AcceptLanguage.negotiate(header, available)
end

.parse_accept(header) ⇒ Array<Hash>

Parse an Accept header into structured entries sorted by quality.

Parameters:

  • header (String)

    the Accept header value

Returns:

  • (Array<Hash>)

    entries with :type, :quality, :params keys



28
29
30
# File 'lib/philiprehberger/header_kit.rb', line 28

def self.parse_accept(header)
  Accept.parse(header)
end

.parse_accept_encoding(header) ⇒ Array<Hash>

Parse an Accept-Encoding header into structured entries sorted by quality.

Parameters:

  • header (String)

    the Accept-Encoding header value

Returns:

  • (Array<Hash>)

    entries with :encoding, :quality keys



44
45
46
# File 'lib/philiprehberger/header_kit.rb', line 44

def self.parse_accept_encoding(header)
  AcceptEncoding.parse(header)
end

.parse_accept_language(header) ⇒ Array<Hash>

Parse an Accept-Language header into structured entries sorted by quality.

Parameters:

  • header (String)

    the Accept-Language header value

Returns:

  • (Array<Hash>)

    entries with :language, :quality keys



52
53
54
# File 'lib/philiprehberger/header_kit.rb', line 52

def self.parse_accept_language(header)
  AcceptLanguage.parse(header)
end

.parse_authorization(header) ⇒ Hash

Parse an Authorization header into its components.

Parameters:

  • header (String)

    the Authorization header value

Returns:

  • (Hash)

    with :scheme and :credentials or :params



69
70
71
# File 'lib/philiprehberger/header_kit.rb', line 69

def self.parse_authorization(header)
  Authorization.parse(header)
end

.parse_cache_control(header) ⇒ Hash{Symbol => true, Integer, String}

Parse a Cache-Control header into a directive hash.

Parameters:

  • header (String)

    the Cache-Control header value

Returns:

  • (Hash{Symbol => true, Integer, String})

    parsed directives



77
78
79
# File 'lib/philiprehberger/header_kit.rb', line 77

def self.parse_cache_control(header)
  CacheControl.parse(header)
end

.parse_content_type(header) ⇒ Hash

Parse a Content-Type header into its components.

Parameters:

  • header (String)

    the Content-Type header value

Returns:

  • (Hash)

    with :media_type, :charset, :boundary keys



93
94
95
# File 'lib/philiprehberger/header_kit.rb', line 93

def self.parse_content_type(header)
  ContentType.parse(header)
end

Parse a Cookie header into a name-value hash.

Parameters:

  • header (String)

    the Cookie header value

Returns:

  • (Hash{String => String})

    cookie name-value pairs



101
102
103
# File 'lib/philiprehberger/header_kit.rb', line 101

def self.parse_cookie(header)
  Cookie.parse(header)
end

.parse_cors(headers) ⇒ Hash

Parse CORS-related headers from a request.

Parameters:

  • headers (Hash)

    request headers hash

Returns:

  • (Hash)

    with :origin, :method, :headers keys



144
145
146
# File 'lib/philiprehberger/header_kit.rb', line 144

def self.parse_cors(headers)
  Cors.parse(headers)
end

.parse_forwarded(header) ⇒ Array<Hash>

Parse an RFC 7239 Forwarded header.

Parameters:

  • header (String)

    the Forwarded header value

Returns:

  • (Array<Hash>)

    parsed entries with symbol keys



168
169
170
# File 'lib/philiprehberger/header_kit.rb', line 168

def self.parse_forwarded(header)
  Forwarded.parse(header)
end

Parse a Link header into an array of link entries.

Parameters:

  • header (String)

    the Link header value

Returns:

  • (Array<Hash>)

    entries with :uri, :rel, :type, :title keys



119
120
121
# File 'lib/philiprehberger/header_kit.rb', line 119

def self.parse_link(header)
  Link.parse(header)
end

.parse_retry_after(header) ⇒ Hash?

Parse a Retry-After header.

Returns a hash with :seconds (Integer) for numeric values, or :date (Time) for HTTP date values. Returns nil for nil/empty input.

Parameters:

  • header (String)

    the Retry-After header value

Returns:

  • (Hash, nil)

    hash with :seconds or :date key, or nil



200
201
202
# File 'lib/philiprehberger/header_kit.rb', line 200

def self.parse_retry_after(header)
  RetryAfter.parse(header)
end

.parse_via(header) ⇒ Array<Hash>

Parse a Via header into structured entries.

Parameters:

  • header (String)

    the Via header value

Returns:

  • (Array<Hash>)

    entries with :protocol, :host, :comment keys



176
177
178
# File 'lib/philiprehberger/header_kit.rb', line 176

def self.parse_via(header)
  Forwarded.parse_via(header)
end

.security_headers(**options) ⇒ Hash{String => String}

Generate recommended security response headers.

Parameters:

  • options (Hash)

    overrides (hsts:, csp:, frame:, content_type_options:, referrer_policy:)

Returns:

  • (Hash{String => String})

    security headers



160
161
162
# File 'lib/philiprehberger/header_kit.rb', line 160

def self.security_headers(**options)
  Security.headers(**options)
end