Class: HTTP::Accept::MediaTypes::MediaRange
- Inherits:
-
Struct
- Object
- Struct
- HTTP::Accept::MediaTypes::MediaRange
- 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
Instance Attribute Summary collapse
-
#mime_type ⇒ Object
Returns the value of attribute mime_type.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
Class Method Summary collapse
- .parse(scanner, normalize_whitespace = true) ⇒ Object
- .parse_parameters(scanner, normalize_whitespace) ⇒ Object
Instance Method Summary collapse
- #===(other) ⇒ Object
- #parameters_string ⇒ Object
- #quality_factor ⇒ Object
- #split(on = '/', count = 2) ⇒ Object
- #to_s ⇒ Object (also: #to_str)
Instance Attribute Details
#mime_type ⇒ Object
Returns the value of attribute mime_type
38 39 40 |
# File 'lib/http/accept/media_types.rb', line 38 def mime_type @mime_type end |
#parameters ⇒ Object
Returns the value of attribute parameters
38 39 40 |
# File 'lib/http/accept/media_types.rb', line 38 def parameters @parameters end |
Class Method Details
.parse(scanner, normalize_whitespace = true) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/http/accept/media_types.rb', line 86 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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/http/accept/media_types.rb', line 69 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
47 48 49 50 51 52 53 |
# File 'lib/http/accept/media_types.rb', line 47 def === other if other.is_a? self.class super else return self.mime_type === other end end |
#parameters_string ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/http/accept/media_types.rb', line 39 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_factor ⇒ Object
61 62 63 |
# File 'lib/http/accept/media_types.rb', line 61 def quality_factor parameters.fetch('q', 1.0).to_f end |
#split(on = '/', count = 2) ⇒ Object
65 66 67 |
# File 'lib/http/accept/media_types.rb', line 65 def split(on = '/', count = 2) mime_type.split(on, count) end |
#to_s ⇒ Object Also known as: to_str
55 56 57 |
# File 'lib/http/accept/media_types.rb', line 55 def to_s "#{mime_type}#{parameters_string}" end |