Module: HTTP::Accept::QuotedString

Defined in:
lib/http/accept/quoted_string.rb

Class Method Summary collapse

Class Method Details

.quote(value, force = false) ⇒ Object

Quote a string if required. Doesn’t handle newlines correctly currently.



46
47
48
49
50
51
52
# File 'lib/http/accept/quoted_string.rb', line 46

def self.quote(value, force = false)
	if value =~ /"/ or force
		"\"#{value.gsub(/["\\]/, "\\\\\\0")}\""
	else
		return value
	end
end

.unquote(value, normalize_whitespace = true) ⇒ Object

Unquote a “quoted-string” value according to tools.ietf.org/html/rfc7230#section-3.2.6 It should already match the QUOTED_STRING pattern above by the parser.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/http/accept/quoted_string.rb', line 32

def self.unquote(value, normalize_whitespace = true)
	value = value[1...-1]
	
	value.gsub!(/\\(.)/, '\1') 
	
	if normalize_whitespace
		# LWS = [CRLF] 1*( SP | HT )
		value.gsub!(/[\r\n]+\s+/, ' ')
	end
	
	return value
end