class AcceptHeaders::Language

Attributes

primary_tag[R]
subtag[R]

Public Class Methods

new(primary_tag = '*', subtag = nil, q: 1.0) click to toggle source
# File lib/accept_headers/language.rb, line 10
def initialize(primary_tag = '*', subtag = nil, q: 1.0)
  self.primary_tag = primary_tag
  self.subtag = subtag
  self.q = q
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/accept_headers/language.rb, line 16
def <=>(other)
  if q < other.q
    -1
  elsif q > other.q
    1
  elsif (primary_tag == '*' && other.primary_tag != '*') || (subtag == '*' && other.subtag != '*')
    -1
  elsif (primary_tag != '*' && other.primary_tag == '*') || (subtag != '*' && other.subtag == '*')
    1
  else
    0
  end
end
language_tag() click to toggle source
# File lib/accept_headers/language.rb, line 55
def language_tag
  if primary_tag == '*' && (subtag.nil? || subtag == '*')
    '*'
  else
    "#{primary_tag}-#{subtag}"
  end
end
match(language_tag_string) click to toggle source
# File lib/accept_headers/language.rb, line 63
def match(language_tag_string)
  match_data = Negotiator::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 == match_data[:primary_tag] && subtag == '*'
    true
  elsif primary_tag == '*'
    true
  else
    false
  end
end
primary_tag=(value) click to toggle source
# File lib/accept_headers/language.rb, line 30
def primary_tag=(value)
  @primary_tag = value.strip.downcase
end
subtag=(value) click to toggle source
# File lib/accept_headers/language.rb, line 34
def subtag=(value)
  @subtag = if value.nil?
    '*'
  else
    value.strip.downcase
  end
end
to_h() click to toggle source
# File lib/accept_headers/language.rb, line 42
def to_h
  {
    primary_tag: primary_tag,
    subtag: subtag,
    q: q
  }
end
to_s() click to toggle source
# File lib/accept_headers/language.rb, line 50
def to_s
  qvalue = (q == 0 || q == 1) ? q.to_i : q
  "#{language_tag};q=#{qvalue}"
end