Module: Philiprehberger::HeaderKit::Range
- Defined in:
- lib/philiprehberger/header_kit/range.rb
Overview
Parses and builds the HTTP Range request header (RFC 7233 §3.1).
Format: ‘<unit>=<start>-<end>[, <start>-<end>]*` Supports byte ranges, suffix ranges (`-N`), and open-ended ranges (`N-`).
Class Method Summary collapse
-
.build(unit, ranges) ⇒ String
Build a Range header value.
-
.parse(header) ⇒ Hash{Symbol => Object}?
Parse a Range header value.
Class Method Details
.build(unit, ranges) ⇒ String
Build a Range header value.
50 51 52 53 54 |
# File 'lib/philiprehberger/header_kit/range.rb', line 50 def build(unit, ranges) list = ranges.is_a?(Array) ? ranges : [ranges] formatted = list.map { |r| format_range(r) } "#{unit}=#{formatted.join(', ')}" end |
.parse(header) ⇒ Hash{Symbol => Object}?
Parse a Range header value.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/philiprehberger/header_kit/range.rb', line 23 def parse(header) return nil if header.nil? header = header.to_s.strip return nil if header.empty? unit, _, spec = header.partition('=') unit = unit.strip spec = spec.strip return nil if unit.empty? || spec.empty? ranges = spec.split(',').map { |part| parse_range(part.strip) } return nil if ranges.any?(&:nil?) { unit: unit, ranges: ranges } end |