Class: AcceptLanguage::Matcher Private

Inherits:
Object
  • Object
show all
Defined in:
lib/accept_language/matcher.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Note:

This class is intended for internal use by Parser and should not be instantiated directly.

Matches Accept-Language header values against application-supported languages to determine the optimal language choice. Handles quality values, wildcards, and language tag matching according to RFC 2616 specifications.

Constant Summary collapse

WILDCARD =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"*"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**languages_range) ⇒ Matcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Matcher.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/accept_language/matcher.rb', line 18

def initialize(**languages_range)
  @excluded_langtags = ::Set[]
  langtags = []

  languages_range.select do |langtag, quality|
    if quality.zero?
      @excluded_langtags << langtag unless wildcard?(langtag)
    else
      level = (quality * 1_000).to_i
      langtags[level] = langtag
    end
  end

  @preferred_langtags = langtags.compact.reverse
end

Instance Attribute Details

#excluded_langtagsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/accept_language/matcher.rb', line 15

def excluded_langtags
  @excluded_langtags
end

#preferred_langtagsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/accept_language/matcher.rb', line 15

def preferred_langtags
  @preferred_langtags
end

Instance Method Details

#call(*available_langtags) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (::ArgumentError)


35
36
37
38
39
40
41
42
# File 'lib/accept_language/matcher.rb', line 35

def call(*available_langtags)
  raise ::ArgumentError, "Language tags cannot be nil" if available_langtags.any?(&:nil?)

  filtered_tags = drop_unacceptable(*available_langtags)
  return nil if filtered_tags.empty?

  find_best_match(filtered_tags)
end