Class: PatientHttp::HttpHeaders

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/patient_http/http_headers.rb

Overview

Case insensitive HTTP headers.

This class provides a hash-like interface for HTTP headers with case-insensitive key access. Header names are normalized to lowercase for storage and lookup.

Instance Method Summary collapse

Constructor Details

#initialize(headers = {}) ⇒ HttpHeaders

Initializes a new HttpHeaders instance.

Parameters:

  • headers (Hash) (defaults to: {})

    initial headers to set



14
15
16
17
18
19
# File 'lib/patient_http/http_headers.rb', line 14

def initialize(headers = {})
  @headers = {}
  headers&.each do |key, value|
    @headers[key.to_s.downcase] = value
  end
end

Instance Method Details

#[](key) ⇒ String?

Retrieves the value for a header (case insensitive).

Parameters:

  • key (String, Symbol)

    header name

Returns:

  • (String, nil)

    header value or nil if not found



25
26
27
# File 'lib/patient_http/http_headers.rb', line 25

def [](key)
  @headers[key.to_s.downcase]
end

#[]=(key, value) ⇒ Object

Sets the value for a header (case insensitive).

Parameters:

  • key (String, Symbol)

    header name

  • value (String)

    header value



33
34
35
# File 'lib/patient_http/http_headers.rb', line 33

def []=(key, value)
  @headers[key.to_s.downcase] = value
end

#each {|key, value| ... } ⇒ Enumerator

Iterates over each header.

Yields:

  • (key, value)

    yields each header key-value pair

Returns:

  • (Enumerator)

    if no block given



79
80
81
# File 'lib/patient_http/http_headers.rb', line 79

def each(&block)
  @headers.each(&block)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/patient_http/http_headers.rb', line 91

def eql?(other)
  other.is_a?(HttpHeaders) && @headers.eql?(other.to_h)
end

#except(*keys) ⇒ HttpHeaders

Returns a new HttpHeaders without the specified keys (case-insensitive).

Parameters:

  • keys (Array<String, Symbol>)

    header names to exclude

Returns:

  • (HttpHeaders)

    new instance without the specified headers



62
63
64
65
66
# File 'lib/patient_http/http_headers.rb', line 62

def except(*keys)
  normalized = keys.map { |k| k.to_s.downcase }
  filtered_headers = @headers.reject { |key, _value| normalized.include?(key) } # rubocop:disable Style/HashExcept
  self.class.new(filtered_headers)
end

#fetch(key, default = nil) ⇒ String, Object

Fetches the value for a header with an optional default.

Parameters:

  • key (String, Symbol)

    header name

  • default (Object) (defaults to: nil)

    default value if header not found

Returns:

  • (String, Object)

    header value or default



42
43
44
# File 'lib/patient_http/http_headers.rb', line 42

def fetch(key, default = nil)
  @headers.fetch(key.to_s.downcase, default)
end

#hashObject



95
96
97
# File 'lib/patient_http/http_headers.rb', line 95

def hash
  @headers.hash
end

#include?(name) ⇒ Boolean

Checks if a header exists (case insensitive).

Parameters:

  • name (String, Symbol)

    header name

Returns:

  • (Boolean)

    true if header exists



87
88
89
# File 'lib/patient_http/http_headers.rb', line 87

def include?(name)
  @headers.include?(name.to_s.downcase)
end

#merge(other_headers) ⇒ HttpHeaders

Merges another set of headers into a new HttpHeaders instance.

Parameters:

  • other_headers (Hash, HttpHeaders)

    headers to merge

Returns:



50
51
52
53
54
55
56
# File 'lib/patient_http/http_headers.rb', line 50

def merge(other_headers)
  new_headers = dup
  other_headers.each do |key, value|
    new_headers[key] = value
  end
  new_headers
end

#to_hHash

Converts to a regular hash with lowercase keys.

Returns:

  • (Hash)

    hash representation



71
72
73
# File 'lib/patient_http/http_headers.rb', line 71

def to_h
  @headers.dup
end