class Accept
The accept-content-type
header represents a list of content-types that the client can accept.
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 (no escaping of quotes within)
| # OR
[^,"]+ # Match non-quoted strings until a comma or quote
)+
(?=,|\z) # Match until a comma or end of string
/x
MediaRange
A 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.
Implementation
MediaRange = Struct.new(:type, :subtype, :parameters) do
def initialize(type, subtype = "*", parameters = {})
super(type, subtype, parameters)
end
def <=> other
other.quality_factor <=> self.quality_factor
end
def parameters_string
return "" if parameters == nil or parameters.empty?
parameters.collect do |key, value|
";#{key.to_s}=#{QuotedString.quote(value.to_s)}"
end.join
end
def === other
if other.is_a? self.class
super
else
return self.range_string === other
end
end
def range_string
"#{type}/#{subtype}"
end
def to_s
"#{type}/#{subtype}#{parameters_string}"
end
alias to_str to_s
def quality_factor
parameters.fetch("q", 1.0).to_f
end
def split(*args)
return [type, subtype]
end
end
def initialize(value = nil)
Parse the accept
header value into a list of content types.
Signature
-
parameter
value
String
the value of the header.
Implementation
def initialize(value = nil)
if value
super(value.scan(SEPARATOR).map(&:strip))
end
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
value
String
the value or values to add, separated by commas.
Implementation
def << (value)
self.concat(value.scan(SEPARATOR).map(&:strip))
end
def to_s
Serializes the stored values into a comma-separated string.
Signature
-
returns
String
the serialized representation of the header values.
Implementation
def to_s
join(",")
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