class Blobsterix::AcceptType

Public Class Methods

get(env, format) click to toggle source
# File lib/blobsterix/helper/accept_type.rb, line 10
def self.get(env, format)
  parse(env["HTTP_ACCEPT"], format)
end
new(*data) click to toggle source
# File lib/blobsterix/helper/accept_type.rb, line 14
def initialize(*data)
  data = ["*/*"] if data.empty?
  @mimetype = data.flatten[0]
  set_q_factor_string(data.flatten[1] || "q=0.0")
  mediatype
  subtype
  score
end
parse(header, format) click to toggle source
# File lib/blobsterix/helper/accept_type.rb, line 4
def self.parse(header, format)
  fields = (header||"").split(",")
  fields << (MimeMagic.by_extension(format) || MimeMagic.new("*/*")).type if format
  fields.map{|entry| AcceptType.new(entry.split(";"))}.sort {|a,b| b.score <=> a.score}
end

Public Instance Methods

equal?(other_type) click to toggle source
# File lib/blobsterix/helper/accept_type.rb, line 52
def equal? other_type
  return false unless other_type
  mediatype === other_type.mediatype and subtype === other_type.subtype# and factor == other_type.factor
end
factor() click to toggle source
# File lib/blobsterix/helper/accept_type.rb, line 43
def factor
  @q_factor
end
is?(other_type) click to toggle source
# File lib/blobsterix/helper/accept_type.rb, line 47
def is? other_type
  return false unless other_type
  mediatype === other_type.mediatype || mediatype === "*" || other_type.mediatype === "*"
end
mediatype() click to toggle source
# File lib/blobsterix/helper/accept_type.rb, line 31
def mediatype()
  @mediatype ||= @mimetype.split("/")[0]
end
score() click to toggle source
# File lib/blobsterix/helper/accept_type.rb, line 39
def score
  @score ||= factor+(mediatype != "*" ? 1.0: 0.0)+(subtype != "*" ? 1.0: 0.0)
end
subtype() click to toggle source
# File lib/blobsterix/helper/accept_type.rb, line 35
def subtype()
  @subtype ||= @mimetype.split("/")[1]
end
to_s() click to toggle source
# File lib/blobsterix/helper/accept_type.rb, line 23
def to_s()
  @mimetype.to_s
end
type() click to toggle source
# File lib/blobsterix/helper/accept_type.rb, line 27
def type()
  @mimetype
end

Private Instance Methods

set_q_factor_string(str) click to toggle source
# File lib/blobsterix/helper/accept_type.rb, line 58
def set_q_factor_string(str)
  str.scanf("%c=%f"){|char, num|
    @q_factor = num if char === 'q'
  }
end