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

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mime_typeObject

Returns the value of attribute mime_type

Returns:

  • (Object)

    the current value of mime_type



34
35
36
# File 'lib/http/accept/media_types.rb', line 34

def mime_type
  @mime_type
end

#parametersObject

Returns the value of attribute parameters

Returns:

  • (Object)

    the current value of parameters



34
35
36
# File 'lib/http/accept/media_types.rb', line 34

def parameters
  @parameters
end

Class Method Details

.parse(scanner) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/http/accept/media_types.rb', line 39

def self.parse(scanner)
  return to_enum(:parse, scanner) unless block_given?
  
  while mime_type = scanner.scan(MIME_TYPE)
    parameters = {}
    
    while scanner.scan(PARAMETER)
      key = scanner[:key]
      
      if value = scanner[:value]
        parameters[key] = value
      elsif quoted_value = scanner[:quoted_value]
        parameters[key] = QuotedString.new(quoted_value)
      else
        raise ParseError.new("Could not parse parameter!")
      end
    end
    
    yield self.new(mime_type, parameters)
    
    # Are there more?
    return unless scanner.scan(/\s*,\s*/)
  end
  
  raise ParseError.new("Could not parse entire string!") unless scanner.eos?
end

Instance Method Details

#quality_factorObject



35
36
37
# File 'lib/http/accept/media_types.rb', line 35

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