Class: Protocol::HTTP::Header::Accept

Inherits:
Array
  • Object
show all
Defined in:
lib/protocol/http/header/accept.rb

Overview

The ‘accept-content-type` header represents a list of content-types that the client can accept.

Defined Under Namespace

Classes: MediaRange

Constant Summary collapse

SEPARATOR =

Regular expression used to split values on commas, with optional surrounding whitespace, taking into account quoted strings.

/
	(?:            # 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
ParseError =
Class.new(Error)
MEDIA_RANGE =
/\A(?<type>#{TOKEN})\/(?<subtype>#{TOKEN})(?<parameters>.*)\z/
PARAMETER =
/\s*;\s*(?<key>#{TOKEN})=((?<value>#{TOKEN})|(?<quoted_value>#{QUOTED_STRING}))/

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Accept

Parse the ‘accept` header value into a list of content types.



73
74
75
76
77
# File 'lib/protocol/http/header/accept.rb', line 73

def initialize(value = nil)
	if value
		super(value.scan(SEPARATOR).map(&:strip))
	end
end

Instance Method Details

#<<(value) ⇒ Object

Adds one or more comma-separated values to the header.

The input string is split into distinct entries and appended to the array.



84
85
86
# File 'lib/protocol/http/header/accept.rb', line 84

def << (value)
	self.concat(value.scan(SEPARATOR).map(&:strip))
end

#media_rangesObject

Parse the ‘accept` header.



98
99
100
101
102
# File 'lib/protocol/http/header/accept.rb', line 98

def media_ranges
	self.map do |value|
		self.parse_media_range(value)
	end
end

#to_sObject

Serializes the stored values into a comma-separated string.



91
92
93
# File 'lib/protocol/http/header/accept.rb', line 91

def to_s
	join(",")
end