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



85
86
87
# File 'lib/http/accept/media_types.rb', line 85

def mime_type
  @mime_type
end

#parametersObject

Returns the value of attribute parameters

Returns:

  • (Object)

    the current value of parameters



85
86
87
# File 'lib/http/accept/media_types.rb', line 85

def parameters
  @parameters
end

Class Method Details

.parse(scanner, normalize_whitespace = true) ⇒ Object

Raises:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/http/accept/media_types.rb', line 94

def self.parse(scanner, normalize_whitespace = true)
	return to_enum(:parse, scanner, normalize_whitespace) 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.unquote(quoted_value, normalize_whitespace)
			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



86
87
88
# File 'lib/http/accept/media_types.rb', line 86

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

#splitObject



90
91
92
# File 'lib/http/accept/media_types.rb', line 90

def split
	@type, @subtype = mime_type.split('/')
end