lib/accept_headers/language.rb in accept_headers-0.0.4 vs lib/accept_headers/language.rb in accept_headers-0.0.5

- old
+ new

@@ -5,10 +5,12 @@ include Comparable include Acceptable attr_reader :primary_tag, :subtag, :params + LANGUAGE_TAG_PATTERN = /^\s*(?<primary_tag>[\w]{1,8}|\*)(?:\s*\-\s*(?<subtag>[\w]{1,8}|\*))?\s*$/ + def initialize(primary_tag = '*', subtag = nil, q: 1.0) self.primary_tag = primary_tag self.subtag = subtag self.q = q end @@ -51,18 +53,26 @@ qvalue = (q == 0 || q == 1) ? q.to_i : q "#{language_tag};q=#{qvalue}" end def language_tag - "#{primary_tag}-#{subtag}" + if primary_tag == '*' && (subtag.nil? || subtag == '*') + '*' + else + "#{primary_tag}-#{subtag}" + end end - def match(other) - if primary_tag == other.primary_tag && subtag == other.subtag + private + def match(language_tag_string) + match_data = LANGUAGE_TAG_PATTERN.match(language_tag_string) + if !match_data + false + elsif primary_tag == match_data[:primary_tag] && subtag == match_data[:subtag] true - elsif primary_tag == other.primary_tag && subtag == '*' + elsif primary_tag == match_data[:primary_tag] && subtag == '*' true - elsif other.primary_tag == '*' + elsif primary_tag == '*' true else false end end