Class: Datastar::CompressionConfig
- Inherits:
-
Object
- Object
- Datastar::CompressionConfig
- Defined in:
- lib/datastar/compression_config.rb
Overview
Immutable value object that holds an ordered list of pre-built compressors and negotiates the best one for a given request.
Use CompressionConfig.build to create instances from user-facing configuration values. The first compressor in the list is preferred when the client supports multiple.
Constant Summary collapse
- ACCEPT_ENCODING =
'HTTP_ACCEPT_ENCODING'- BLANK_HASH =
{}.freeze
Class Method Summary collapse
-
.build(input) ⇒ CompressionConfig
Build a CompressionConfig from various user-facing input forms.
Instance Method Summary collapse
-
#enabled? ⇒ Boolean
Whether any compressors are configured.
-
#initialize(compressors) ⇒ CompressionConfig
constructor
A new instance of CompressionConfig.
-
#negotiate(request) ⇒ Compressor::Gzip, ...
Negotiate compression with the client based on the
Accept-Encodingheader.
Constructor Details
#initialize(compressors) ⇒ CompressionConfig
Returns a new instance of CompressionConfig.
101 102 103 104 |
# File 'lib/datastar/compression_config.rb', line 101 def initialize(compressors) @compressors = compressors.freeze freeze end |
Class Method Details
.build(input) ⇒ CompressionConfig
Build a Datastar::CompressionConfig from various user-facing input forms.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/datastar/compression_config.rb', line 59 def self.build(input) case input when CompressionConfig input when false, nil new([]) when true new([build_compressor(:br), build_compressor(:gzip)]) when Array compressors = input.map do |entry| case entry when Symbol build_compressor(entry) when Array name, = entry build_compressor(name, || BLANK_HASH) else raise ArgumentError, "Invalid compression entry: #{entry.inspect}. Expected Symbol or [Symbol, Hash]." end end new(compressors) else raise ArgumentError, "Invalid compression value: #{input.inspect}. Expected true, false, or Array." end end |
Instance Method Details
#enabled? ⇒ Boolean
Whether any compressors are configured.
113 114 115 |
# File 'lib/datastar/compression_config.rb', line 113 def enabled? @compressors.any? end |
#negotiate(request) ⇒ Compressor::Gzip, ...
Negotiate compression with the client based on the Accept-Encoding header.
Iterates the configured compressors in order (first = preferred) and returns the first one whose encoding the client accepts. Returns Datastar::Compressor::NONE when compression is disabled, the header is absent, or no match is found.
No objects are created per-request — compressors are pre-built and reused.
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/datastar/compression_config.rb', line 133 def negotiate(request) return Compressor::NONE unless enabled? accepted = parse_accept_encoding(request.get_header(ACCEPT_ENCODING).to_s) return Compressor::NONE if accepted.empty? @compressors.each do |compressor| return compressor if accepted.include?(compressor.encoding) end Compressor::NONE end |