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

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

Direct Known Subclasses

ContentType

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



79
80
81
# File 'lib/http/accept/media_types.rb', line 79

def mime_type
  @mime_type
end

#parametersObject

Returns the value of attribute parameters

Returns:

  • (Object)

    the current value of parameters



79
80
81
# File 'lib/http/accept/media_types.rb', line 79

def parameters
  @parameters
end

Class Method Details

.parse(scanner, normalize_whitespace = true) ⇒ Object

Raises:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/http/accept/media_types.rb', line 127

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 = parse_parameters(scanner, normalize_whitespace)
		
		yield self.new(mime_type, 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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/http/accept/media_types.rb', line 110

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



88
89
90
91
92
93
94
# File 'lib/http/accept/media_types.rb', line 88

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

#parameters_stringObject



80
81
82
83
84
85
86
# File 'lib/http/accept/media_types.rb', line 80

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



102
103
104
# File 'lib/http/accept/media_types.rb', line 102

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

#split(on = '/', count = 2) ⇒ Object



106
107
108
# File 'lib/http/accept/media_types.rb', line 106

def split(on = '/', count = 2)
	mime_type.split(on, count)
end

#to_sObject Also known as: to_str



96
97
98
# File 'lib/http/accept/media_types.rb', line 96

def to_s
	@to_s || "#{mime_type}#{parameters_string}"
end