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
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#subtype ⇒ Object
Returns the value of attribute subtype.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
- .parse(scanner, normalize_whitespace = true) ⇒ Object
- .parse_parameters(scanner, normalize_whitespace) ⇒ Object
Instance Method Summary collapse
- #===(other) ⇒ Object
-
#initialize(type, subtype = '*', parameters = {}) ⇒ MediaRange
constructor
A new instance of MediaRange.
- #mime_type ⇒ Object
- #parameters_string ⇒ Object
- #quality_factor ⇒ Object
- #split(*args) ⇒ Object
- #to_s ⇒ Object (also: #to_str)
Constructor Details
#initialize(type, subtype = '*', parameters = {}) ⇒ MediaRange
Returns a new instance of MediaRange.
24 25 26 |
# File 'lib/http/accept/media_types.rb', line 24 def initialize(type, subtype = '*', parameters = {}) super(type, subtype, parameters) end |
Instance Attribute Details
#parameters ⇒ Object
Returns the value of attribute parameters
23 24 25 |
# File 'lib/http/accept/media_types.rb', line 23 def parameters @parameters end |
#subtype ⇒ Object
Returns the value of attribute subtype
23 24 25 |
# File 'lib/http/accept/media_types.rb', line 23 def subtype @subtype end |
#type ⇒ Object
Returns the value of attribute type
23 24 25 |
# File 'lib/http/accept/media_types.rb', line 23 def type @type end |
Class Method Details
.parse(scanner, normalize_whitespace = true) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/http/accept/media_types.rb', line 79 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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/http/accept/media_types.rb', line 62 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
36 37 38 39 40 41 42 |
# File 'lib/http/accept/media_types.rb', line 36 def === other if other.is_a? self.class super else return self.mime_type === other end end |
#mime_type ⇒ Object
44 45 46 |
# File 'lib/http/accept/media_types.rb', line 44 def mime_type "#{type}/#{subtype}" end |
#parameters_string ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/http/accept/media_types.rb', line 28 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
54 55 56 |
# File 'lib/http/accept/media_types.rb', line 54 def quality_factor parameters.fetch('q', 1.0).to_f end |
#split(*args) ⇒ Object
58 59 60 |
# File 'lib/http/accept/media_types.rb', line 58 def split(*args) return [type, subtype] end |
#to_s ⇒ Object Also known as: to_str
48 49 50 |
# File 'lib/http/accept/media_types.rb', line 48 def to_s "#{type}/#{subtype}#{parameters_string}" end |