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}))/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Accept

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



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

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

Class Method Details

.trailer?Boolean

Whether this header is acceptable in HTTP trailers.

Returns:

  • (Boolean)


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

def self.trailer?
  false
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.



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

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

#media_rangesObject

Parse the ‘accept` header.



105
106
107
108
109
# File 'lib/protocol/http/header/accept.rb', line 105

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.



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

def to_s
  join(",")
end