lib/accept_headers/language.rb in accept_headers-0.0.1 vs lib/accept_headers/language.rb in accept_headers-0.0.2
- old
+ new
@@ -3,12 +3,10 @@
module AcceptHeaders
class Language
include Comparable
include Acceptable
- class InvalidLanguageTagError < Error; end
-
attr_reader :primary_tag, :subtag, :params
def initialize(primary_tag = '*', subtag = nil, q: 1.0)
self.primary_tag = primary_tag
self.subtag = subtag
@@ -52,32 +50,18 @@
def to_s
qvalue = (q == 0 || q == 1) ? q.to_i : q
"#{primary_tag}-#{subtag};q=#{qvalue}"
end
- LANGUAGE_PATTERN = /^\s*(?<primary_tag>[\w]{1,8}|\*)(?:\s*\-\s*(?<subtag>[\w]{1,8}|\*))?\s*$/
-
- def self.parse(original_header)
- header = original_header.dup
- header.sub!(/\AAccept-Language:\s*/, '')
- header.strip!
- return [Language.new] if header.empty?
- languages = []
- header.split(',').each do |entry|
- language_arr = entry.split(';', 2)
- next if language_arr[0].nil?
- language_range = LANGUAGE_PATTERN.match(language_arr[0])
- next if language_range.nil?
- begin
- languages << Language.new(
- language_range[:primary_tag],
- language_range[:subtag],
- q: parse_q(language_arr[1])
- )
- rescue Error
- next
- end
+ def match(other)
+ if primary_tag == other.primary_tag && subtag == other.subtag
+ true
+ elsif primary_tag == other.primary_tag && subtag == '*'
+ true
+ elsif other.primary_tag == '*'
+ true
+ else
+ false
end
- languages.sort! { |x,y| y <=> x }
end
end
end