Class: HTTP::Accept::Languages::LanguageRange

Inherits:
Struct
  • Object
show all
Defined in:
lib/http/accept/languages.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#localeObject

Returns the value of attribute locale

Returns:

  • (Object)

    the current value of locale



36
37
38
# File 'lib/http/accept/languages.rb', line 36

def locale
  @locale
end

#qObject

Returns the value of attribute q

Returns:

  • (Object)

    the current value of q



36
37
38
# File 'lib/http/accept/languages.rb', line 36

def q
  @q
end

Class Method Details

.parse(scanner) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/http/accept/languages.rb', line 41

def self.parse(scanner)
  return to_enum(:parse, scanner) unless block_given?
  
  while scanner.scan(LANGUAGE_RANGE)
    yield self.new(scanner[:locale], scanner[:q])
    
    # Are there more?
    return unless scanner.scan(/\s*,\s*/)
  end
  
  raise ParseError.new("Could not parse entire string!") unless scanner.eos?
end

Instance Method Details

#prefix_of?(other) ⇒ Boolean

If the user ask for ‘en’, this satisfies any language that begins with ‘en-’

Returns:

  • (Boolean)


55
56
57
# File 'lib/http/accept/languages.rb', line 55

def prefix_of? other
  other.start_with(locale)
end

#quality_factorObject



37
38
39
# File 'lib/http/accept/languages.rb', line 37

def quality_factor
  (q || 1.0).to_f
end