class AcceptLanguage < Split

Inherits from
Split: #<<, #to_s, #sort_by_quality_factor, .parse, .coerce, .trailer?

The accept-language header represents a list of languages that the client can accept.

Nested Classes and Modules

class Language

A parsed language entry with an optional quality factor.

Definitions

NAME = /\*|[A-Z]{1,8}(-[A-Z0-9]{1,8})*/i

https://tools.ietf.org/html/rfc3066#section-2.1

QVALUE = /0(\.[0-9]{0,6})?|1(\.[0]{0,6})?/

https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.9

LANGUAGE = /\A(?<name>#{NAME})(\s*;\s*q=(?<q>#{QVALUE}))?\z/

https://greenbytes.de/tech/webdav/rfc7231.html#quality.values

def languages

Parse the accept-language header value into a list of languages.

Signature

returns Array(Charset)

the list of character sets and their associated quality factors.

Implementation

def languages
	self.map do |value|
		if match = value.match(LANGUAGE)
			Language.new(match[:name], match[:q])
		else
			raise ParseError.new("Could not parse language: #{value.inspect}")
		end
	end
end

def preferred_languages

Parse the accept-language header and order languages by preference.

Languages with equal quality factors retain their original relative order.

Signature

returns Array(Language)

the preferred languages.

Implementation

def preferred_languages
	sort_by_quality_factor(languages)
end