Module: HTTP::Accept::MediaTypes

Defined in:
lib/http/accept/media_types.rb,
lib/http/accept/media_types/map.rb

Overview

Parse and process the HTTP Accept: header.

Defined Under Namespace

Classes: Map, MediaRange

Constant Summary collapse

MIME_TYPE =
/(?<type>#{TOKEN})\/(?<subtype>#{TOKEN})/
PARAMETER =
/\s*;\s*(?<key>#{TOKEN})=((?<value>#{TOKEN})|(?<quoted_value>#{QUOTED_STRING}))/
HTTP_ACCEPT =
'HTTP_ACCEPT'.freeze
WILDCARD_MEDIA_RANGE =
MediaRange.new("*", "*", {}).freeze

Class Method Summary collapse

Class Method Details

.browser_preferred_media_types(env) ⇒ Object

Parse the list of browser preferred content types and return ordered by priority. If no ‘Accept:` header is specified, the behaviour is the same as if `Accept: /` was provided (according to RFC).



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/http/accept/media_types.rb', line 110

def self.browser_preferred_media_types(env)
	if accept_content_types = env[HTTP_ACCEPT]&.strip
		unless accept_content_types.empty?
			return HTTP::Accept::MediaTypes.parse(accept_content_types)
		end
	end
	
	# According to http://tools.ietf.org/html/rfc7231#section-5.3.2:
	# A request without any Accept header field implies that the user agent will accept any media type in response.
	# You should treat a non-existent Accept header as */*.
	return [WILDCARD_MEDIA_RANGE]
end

.parse(text, normalize_whitespace = true) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/http/accept/media_types.rb', line 98

def self.parse(text, normalize_whitespace = true)
	scanner = StringScanner.new(text)
	
	media_types = MediaRange.parse(scanner, normalize_whitespace)
	
	return Sort.by_quality_factor(media_types)
end