class Accept < Split
- Inherits from
Split: #sort_by_quality_factor, .coerce
The accept-content-type header represents a list of content-types that the client can accept.
Nested Classes and Modules
class MediaRangeA single entry in the Accept: header, which includes a mime type and associated parameters. A media range can include wild cards, but a media type is a specific type and subtype.
Definitions
SEPARATOR
Regular expression used to split values on commas, with optional surrounding whitespace, taking into account quoted strings.
Implementation
SEPARATOR = /
(?: # Start non-capturing group
"(?:[^"\\]|\\.)*" # Match quoted strings, including quoted pairs
| # OR
[^,"]+ # Match non-quoted strings until a comma or quote
)+
(?=,|\z) # Match until a comma or end of string
/x
def self.parse(value)
Parses a raw header value.
Signature
-
parameter
valueString a raw header value containing comma-separated media types.
-
returns
Accept a new instance containing the parsed media types.
Implementation
def self.parse(value)
self.new(value.scan(SEPARATOR).map(&:strip))
end
def <<(value)
Adds one or more comma-separated values to the header.
The input string is split into distinct entries and appended to the array.
Signature
-
parameter
valueString a raw header value containing one or more media types separated by commas.
Implementation
def << value
self.concat(value.scan(SEPARATOR).map(&:strip))
end
def to_s
Converts the parsed header value into a raw header value.
Signature
-
returns
String a raw header value (comma-separated string).
Implementation
def to_s
join(",")
end
def self.trailer?
Whether this header is acceptable in HTTP trailers.
Signature
-
returns
Boolean false, as Accept headers are used for response content negotiation.
Implementation
def self.trailer?
false
end
def media_ranges
Parse the accept header.
Signature
-
returns
Array(Charset) the list of content types and their associated parameters.
Implementation
def media_ranges
self.map do |value|
self.parse_media_range(value)
end
end
def preferred_media_ranges
Parse the accept header and order media ranges by preference.
Media ranges with equal quality factors retain their original relative order.
Signature
-
returns
Array(MediaRange) the preferred media ranges.
Implementation
def preferred_media_ranges
sort_by_quality_factor(media_ranges)
end