Class: Steppe::ContentType

Inherits:
Object
  • Object
show all
Defined in:
lib/steppe/content_type.rb

Constant Summary collapse

TOKEN =
/[!#$%&'*+\-.^_`|~0-9A-Z]+/i
MIME_TYPE =
/(?<type>#{TOKEN})\/(?<subtype>#{TOKEN})/
QUOTED_STRING =
/"(?:\\.|[^"\\])*"/
PARAMETER =
/\s*;\s*(?<key>#{TOKEN})=(?<value>#{TOKEN}|#{QUOTED_STRING})/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, subtype, params) ⇒ ContentType

Returns a new instance of ContentType.



53
54
55
56
57
58
59
# File 'lib/steppe/content_type.rb', line 53

def initialize(type, subtype, params)
  @type = type.downcase
  @subtype = subtype.downcase
  @params = params
  @media_type = "#{type}/#{subtype}"
  freeze
end

Instance Attribute Details

#media_typeObject (readonly)

Returns the value of attribute media_type.



51
52
53
# File 'lib/steppe/content_type.rb', line 51

def media_type
  @media_type
end

#paramsObject (readonly)

Returns the value of attribute params.



51
52
53
# File 'lib/steppe/content_type.rb', line 51

def params
  @params
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



51
52
53
# File 'lib/steppe/content_type.rb', line 51

def subtype
  @subtype
end

#typeObject (readonly)

Returns the value of attribute type.



51
52
53
# File 'lib/steppe/content_type.rb', line 51

def type
  @type
end

Class Method Details

.parse(str) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/steppe/content_type.rb', line 12

def self.parse(str)
  return str if str.is_a?(ContentType)

  if str.is_a?(Symbol)
    str = Rack::Mime.mime_type(".#{str}")
  end

  m = MIME_TYPE.match(str) or
  raise ArgumentError, "invalid content type: #{str.inspect}"

  params = {}
  str.scan(PARAMETER) do
    key, value = Regexp.last_match.values_at(:key, :value)
    value = value[1..-2] if value&.start_with?('"') # unquote
    params[key.downcase] = value
  end

  new(m[:type], m[:subtype], params)
end

.parse_accept(header) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/steppe/content_type.rb', line 32

def self.parse_accept(header)
  header.split(/\s*,\s*/).map do |entry|
    # Match the MIME type part
    m = MIME_TYPE.match(entry)
    next unless m

    params = {}
    # Iterate over all parameters
    entry.scan(PARAMETER) do |match|
      key, value = Regexp.last_match.values_at(:key, :value)
      # Remove quotes if quoted
      value = value[1..-2] if value&.start_with?('"')
      params[key.downcase] = value
    end

    new(m[:type], m[:subtype], params)
  end.compact.sort_by { |ct| -ct.quality }
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



68
69
70
71
72
73
# File 'lib/steppe/content_type.rb', line 68

def ==(other)
  other.is_a?(ContentType) &&
    type == other.type &&
    subtype == other.subtype &&
    params == other.params
end

#hashObject



76
# File 'lib/steppe/content_type.rb', line 76

def hash = [type, subtype, params].hash

#qualified?Boolean

Returns:

  • (Boolean)


61
# File 'lib/steppe/content_type.rb', line 61

def qualified? = !(type == '*' && subtype == '*')

#qualityObject



78
# File 'lib/steppe/content_type.rb', line 78

def quality = params.fetch('q', 1.0).to_f

#to_sObject



63
64
65
66
# File 'lib/steppe/content_type.rb', line 63

def to_s
  param_str = params.map { |k, v| "#{k}=#{v}" }.join('; ')
  [ media_type, param_str ].reject(&:empty?).join('; ')
end