Class: Rack::Accept::Language

Inherits:
Object
  • Object
show all
Includes:
Header
Defined in:
lib/rack/accept/language.rb

Overview

Represents an HTTP Accept-Language header according to the HTTP 1.1 specification, and provides several convenience methods for determining acceptable content languages.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4

Instance Attribute Summary collapse

Attributes included from Header::PublicInstanceMethods

#qvalues

Instance Method Summary collapse

Methods included from Header

join, normalize_qvalue, parse, parse_media_type, parse_range_params

Methods included from Header::PublicInstanceMethods

#accept?, #best_of, #initialize, #sort, #sort_with_qvalues, #to_s, #value, #values

Instance Attribute Details

#first_level_match=(value) ⇒ Object (writeonly)

Sets the attribute first_level_match

Parameters:

  • value —

    the value to set the attribute first_level_match to.



9
10
11
# File 'lib/rack/accept/language.rb', line 9

def first_level_match=(value)
  @first_level_match = value
end

Instance Method Details

#matches(language) ⇒ Object

Returns an array of languages from this header that match the given language, ordered by precedence.



26
27
28
29
30
31
32
33
34
# File 'lib/rack/accept/language.rb', line 26

def matches(language)
  values.select {|v|
    v = v.match(/^(.+?)-/) ? $1 : v if @first_level_match
    v == language || v == '*' || (language.match(/^(.+?)-/) && v == $1)
  }.sort {|a, b|
    # "*" gets least precedence, any others are compared based on length.
    a == '*' ? -1 : (b == '*' ? 1 : a.length <=> b.length)
  }.reverse
end

#name ⇒ Object

The name of this header.



12
13
14
# File 'lib/rack/accept/language.rb', line 12

def name
  'Accept-Language'
end

#qvalue(language) ⇒ Object

Determines the quality factor (qvalue) of the given language.



17
18
19
20
21
22
# File 'lib/rack/accept/language.rb', line 17

def qvalue(language)
  return 1 if @qvalues.empty?
  m = matches(language)
  return 0 if m.empty?
  normalize_qvalue(@qvalues[m.first])
end