Module: Rack::Acceptable::Charsets

Defined in:
lib/rack/acceptable/mixin/charsets.rb

Instance Method Summary collapse

Instance Method Details

#accept_charset?(chs) ⇒ Boolean

Checks if the Charset (as a String) passed acceptable. Works case-insensitively.

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rack/acceptable/mixin/charsets.rb', line 29

def accept_charset?(chs)
  chs = chs.downcase
  return true if (accepts = acceptable_charsets).empty?
  if ch = accepts.assoc(chs) || accepts.assoc(Const::WILDCARD)
    ch.last > 0
  else
    chs == Const::ISO_8859_1
  end
rescue
  false
end

#acceptable_charsetsObject

Returns

An Array with wildcards / downcased Charsets and associated quality factors (qvalues). Default qvalue is 1.0.

Raises

ArgumentError

Syntax of the Accept-Charset request-header is bad. For example, one of Charsets is not a ‘token’, one of quality factors is malformed etc.



17
18
19
20
21
22
23
24
# File 'lib/rack/acceptable/mixin/charsets.rb', line 17

def acceptable_charsets
  Utils.parse_header(
    env[Const::ENV_HTTP_ACCEPT_CHARSET].to_s.downcase,
    Utils::HTTP_ACCEPT_TOKEN_REGEX)
rescue
  raise ArgumentError,
  "Malformed Accept-Charset header: #{env[Const::ENV_HTTP_ACCEPT_CHARSET].inspect}"
end

#negotiate_charset(*things) ⇒ Object Also known as: preferred_charset_from

Detects the best Charset. Works case-insensitively.



44
45
46
47
# File 'lib/rack/acceptable/mixin/charsets.rb', line 44

def negotiate_charset(*things)
  things.map!{|t| t.downcase}
  Utils.detect_best_charset(things, acceptable_charsets)
end