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

Inherits:
Split
  • 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}))/

Constants inherited from Split

Split::COMMA

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Split

coerce, #initialize

Constructor Details

This class inherits a constructor from Protocol::HTTP::Header::Split

Class Method Details

.parse(value) ⇒ Object

Parses a raw header value.



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

def self.parse(value)
  self.new(value.scan(SEPARATOR).map(&:strip))
end

.trailer?Boolean

Whether this header is acceptable in HTTP trailers.

Returns:

  • (Boolean)


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

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.



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.



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

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

#to_sObject

Converts the parsed header value into a raw header value.



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

def to_s
  join(",")
end