class AcceptHeaders::MediaType

Attributes

extensions[R]
subtype[R]
type[R]

Public Class Methods

new(type = '*', subtype = '*', q: 1.0, extensions: {}) click to toggle source
# File lib/accept_headers/media_type.rb, line 10
def initialize(type = '*', subtype = '*', q: 1.0, extensions: {})
  self.type = type
  self.subtype = subtype
  self.q = q
  self.extensions = extensions
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/accept_headers/media_type.rb, line 17
def <=>(other)
  if q < other.q
    -1
  elsif q > other.q
    1
  elsif (type == '*' && other.type != '*') || (subtype == '*' && other.subtype != '*')
    -1
  elsif (type != '*' && other.type == '*') || (subtype != '*' && other.subtype == '*')
    1
  elsif extensions.size < other.extensions.size
    -1
  elsif extensions.size > other.extensions.size
    1
  else
    0
  end
end
extensions=(hash) click to toggle source
# File lib/accept_headers/media_type.rb, line 47
def extensions=(hash)
  @extensions = {}
  hash.each do |k,v|
    @extensions[k.strip] = v
  end
  @extensions
end
match(media_range_string) click to toggle source
# File lib/accept_headers/media_type.rb, line 77
def match(media_range_string)
  match_data = Negotiator::MEDIA_TYPE_PATTERN.match(media_range_string)
  if !match_data
    false
  elsif type == match_data[:type] && subtype == match_data[:subtype]
    true
  elsif type == match_data[:type] && subtype == '*'
    true
  elsif type == '*' && subtype == '*'
    true
  else
    false
  end
end
media_range() click to toggle source
# File lib/accept_headers/media_type.rb, line 73
def media_range
  "#{type}/#{subtype}"
end
subtype=(value) click to toggle source
# File lib/accept_headers/media_type.rb, line 39
def subtype=(value)
  @subtype = if value.nil? && type == '*'
    '*'
  else
    value.strip.downcase
  end
end
to_h() click to toggle source
# File lib/accept_headers/media_type.rb, line 55
def to_h
  {
    type: type,
    subtype: subtype,
    q: q,
    extensions: extensions
  }
end
to_s() click to toggle source
# File lib/accept_headers/media_type.rb, line 64
def to_s
  qvalue = (q == 0 || q == 1) ? q.to_i : q
  string = "#{media_range};q=#{qvalue}"
  if extensions.size > 0
    extensions.each { |k, v| string.concat(";#{k}=#{v}") }
  end
  string
end
type=(value) click to toggle source
# File lib/accept_headers/media_type.rb, line 35
def type=(value)
  @type = value.strip.downcase
end