Class: HTTP::Accept::MediaTypes::MediaRange

Inherits:
Struct
  • Object
show all
Defined in:
lib/http/accept/media_types.rb

Overview

A single entry in the Accept: header, which includes a mime type and associated parameters.

Direct Known Subclasses

ContentType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parametersObject

Returns the value of attribute parameters

Returns:

  • (Object)

    the current value of parameters



40
41
42
# File 'lib/http/accept/media_types.rb', line 40

def parameters
  @parameters
end

#subtypeObject

Returns the value of attribute subtype

Returns:

  • (Object)

    the current value of subtype



40
41
42
# File 'lib/http/accept/media_types.rb', line 40

def subtype
  @subtype
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



40
41
42
# File 'lib/http/accept/media_types.rb', line 40

def type
  @type
end

Class Method Details

.parse(scanner, normalize_whitespace = true) ⇒ Object

Raises:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/http/accept/media_types.rb', line 92

def self.parse(scanner, normalize_whitespace = true)
  return to_enum(:parse, scanner, normalize_whitespace) unless block_given?
  
  while scanner.scan(MIME_TYPE)
    type = scanner[:type]
    subtype = scanner[:subtype]
    
    parameters = parse_parameters(scanner, normalize_whitespace)
    
    yield self.new(type, subtype, parameters)
    
    # Are there more?
    break unless scanner.scan(/\s*,\s*/)
  end
  
  raise ParseError.new("Could not parse entire string!") unless scanner.eos?
end

.parse_parameters(scanner, normalize_whitespace) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/http/accept/media_types.rb', line 75

def self.parse_parameters(scanner, normalize_whitespace)
  parameters = {}
  
  while scanner.scan(PARAMETER)
    key = scanner[:key]
    
    # If the regular expression PARAMETER matched, it must be one of these two:
    if value = scanner[:value]
      parameters[key] = value
    elsif quoted_value = scanner[:quoted_value]
      parameters[key] = QuotedString.unquote(quoted_value, normalize_whitespace)
    end
  end
  
  return parameters
end

Instance Method Details

#===(other) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/http/accept/media_types.rb', line 49

def === other
  if other.is_a? self.class
    super
  else
    return self.mime_type === other
  end
end

#mime_typeObject



57
58
59
# File 'lib/http/accept/media_types.rb', line 57

def mime_type
  "#{type}/#{subtype}"
end

#parameters_stringObject



41
42
43
44
45
46
47
# File 'lib/http/accept/media_types.rb', line 41

def parameters_string
  return '' if parameters == nil or parameters.empty?
  
  parameters.collect do |key, value|
    "; #{key.to_s}=#{QuotedString.quote(value.to_s)}"
  end.join
end

#quality_factorObject



67
68
69
# File 'lib/http/accept/media_types.rb', line 67

def quality_factor
  parameters.fetch('q', 1.0).to_f
end

#split(*args) ⇒ Object



71
72
73
# File 'lib/http/accept/media_types.rb', line 71

def split(*args)
  return [type, subtype]
end

#to_sObject Also known as: to_str



61
62
63
# File 'lib/http/accept/media_types.rb', line 61

def to_s
  "#{type}/#{subtype}#{parameters_string}"
end