Class: Xenon::MediaRange
- Inherits:
-
Object
- Object
- Xenon::MediaRange
- Includes:
- Comparable
- Defined in:
- lib/xenon/media_type.rb
Constant Summary collapse
- DEFAULT_Q =
1.0
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#q ⇒ Object
readonly
Returns the value of attribute q.
-
#subtype ⇒ Object
readonly
Returns the value of attribute subtype.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #=~(media_type) ⇒ Object (also: #===)
-
#initialize(type, subtype, params = {}) ⇒ MediaRange
constructor
A new instance of MediaRange.
- #to_s ⇒ Object
Constructor Details
#initialize(type, subtype, params = {}) ⇒ MediaRange
Returns a new instance of MediaRange.
116 117 118 119 120 121 |
# File 'lib/xenon/media_type.rb', line 116 def initialize(type, subtype, params = {}) @type = type @subtype = subtype @q = Float(params.delete('q')) rescue DEFAULT_Q @params = params end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
114 115 116 |
# File 'lib/xenon/media_type.rb', line 114 def params @params end |
#q ⇒ Object (readonly)
Returns the value of attribute q.
114 115 116 |
# File 'lib/xenon/media_type.rb', line 114 def q @q end |
#subtype ⇒ Object (readonly)
Returns the value of attribute subtype.
114 115 116 |
# File 'lib/xenon/media_type.rb', line 114 def subtype @subtype end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
114 115 116 |
# File 'lib/xenon/media_type.rb', line 114 def type @type end |
Class Method Details
.parse(s) ⇒ Object
123 124 125 126 127 128 |
# File 'lib/xenon/media_type.rb', line 123 def self.parse(s) tree = Parsers::MediaRange.new.parse(s) Parsers::MediaTypeTransform.new.apply(tree) rescue Parslet::ParseFailed raise Xenon::ParseError.new("Invalid media range (#{s})") end |
Instance Method Details
#<=>(other) ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/xenon/media_type.rb', line 130 def <=>(other) dt = compare_types(@type, other.type) return dt if dt != 0 ds = compare_types(@subtype, other.subtype) return ds if ds != 0 dp = params.size <=> other.params.size return dp if dp != 0 @q <=> other.q end |
#=~(media_type) ⇒ Object Also known as: ===
140 141 142 143 144 |
# File 'lib/xenon/media_type.rb', line 140 def =~(media_type) (type == '*' || type == media_type.type) && (subtype == '*' || subtype == media_type.subtype) && params.all? { |n, v| media_type.params[n] == v } end |
#to_s ⇒ Object
148 149 150 151 152 153 |
# File 'lib/xenon/media_type.rb', line 148 def to_s s = "#{@type}/#{@subtype}" s << @params.map { |n, v| v ? "; #{n}=#{v}" : "; #{n}" }.join s << "; q=#{@q}" if @q != DEFAULT_Q s end |