Class: PatientHttp::HttpHeaders
- Inherits:
-
Object
- Object
- PatientHttp::HttpHeaders
- 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
-
#[](key) ⇒ String?
Retrieves the value for a header (case insensitive).
-
#[]=(key, value) ⇒ Object
Sets the value for a header (case insensitive).
-
#each {|key, value| ... } ⇒ Enumerator
Iterates over each header.
- #eql?(other) ⇒ Boolean
-
#except(*keys) ⇒ HttpHeaders
Returns a new HttpHeaders without the specified keys (case-insensitive).
-
#fetch(key, default = nil) ⇒ String, Object
Fetches the value for a header with an optional default.
- #hash ⇒ Object
-
#include?(name) ⇒ Boolean
Checks if a header exists (case insensitive).
-
#initialize(headers = {}) ⇒ HttpHeaders
constructor
Initializes a new HttpHeaders instance.
-
#merge(other_headers) ⇒ HttpHeaders
Merges another set of headers into a new HttpHeaders instance.
-
#to_h ⇒ Hash
Converts to a regular hash with lowercase keys.
Constructor Details
#initialize(headers = {}) ⇒ HttpHeaders
Initializes a new HttpHeaders instance.
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).
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).
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.
79 80 81 |
# File 'lib/patient_http/http_headers.rb', line 79 def each(&block) @headers.each(&block) end |
#eql?(other) ⇒ 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).
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.
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 |
#hash ⇒ Object
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).
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.
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_h ⇒ Hash
Converts to a regular hash with lowercase keys.
71 72 73 |
# File 'lib/patient_http/http_headers.rb', line 71 def to_h @headers.dup end |