Class: Xenon::MediaType
- Inherits:
-
Object
- Object
- Xenon::MediaType
- Defined in:
- lib/xenon/media_type.rb
Overview
A media type.
Constant Summary collapse
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#subtype ⇒ Object
readonly
Returns the value of attribute subtype.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.parse(s) ⇒ MediaType
Parses a media type.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #experimental? ⇒ Boolean
-
#initialize(type, subtype, params = {}) ⇒ MediaType
constructor
Initializes a new instance of MediaType.
- #personal? ⇒ Boolean
- #to_s ⇒ Object
- #vendor? ⇒ Boolean
-
#with_charset(charset) ⇒ ContentType
Creates a ContentType using this media type with a charset.
-
#with_q(q) ⇒ MediaRange
Creates a MediaRange using this media type with a quality factor.
Constructor Details
#initialize(type, subtype, params = {}) ⇒ MediaType
Initializes a new instance of MediaType.
18 19 20 21 22 |
# File 'lib/xenon/media_type.rb', line 18 def initialize(type, subtype, params = {}) @type = type @subtype = subtype @params = params end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
11 12 13 |
# File 'lib/xenon/media_type.rb', line 11 def params @params end |
#subtype ⇒ Object (readonly)
Returns the value of attribute subtype.
11 12 13 |
# File 'lib/xenon/media_type.rb', line 11 def subtype @subtype end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
11 12 13 |
# File 'lib/xenon/media_type.rb', line 11 def type @type end |
Class Method Details
.parse(s) ⇒ MediaType
Parses a media type.
28 29 30 31 32 33 |
# File 'lib/xenon/media_type.rb', line 28 def self.parse(s) tree = Parsers::MediaType.new.parse(s) Parsers::MediaTypeTransform.new.apply(tree) rescue Parslet::ParseFailed raise Xenon::ParseError.new("Invalid media type (#{s}).") end |
Instance Method Details
#==(other) ⇒ Object
59 60 61 |
# File 'lib/xenon/media_type.rb', line 59 def ==(other) @type == other.type && @subtype == other.subtype && @params == other.params end |
#experimental? ⇒ Boolean
41 42 43 |
# File 'lib/xenon/media_type.rb', line 41 def experimental? @subtype.start_with?('x.') # not x- see http://tools.ietf.org/html/rfc6838#section-3.4 end |
#personal? ⇒ Boolean
45 46 47 |
# File 'lib/xenon/media_type.rb', line 45 def personal? @subtype.start_with?('prs.') end |
#to_s ⇒ Object
79 80 81 |
# File 'lib/xenon/media_type.rb', line 79 def to_s "#{@type}/#{@subtype}" << @params.map { |n, v| v ? "; #{n}=#{v}" : "; #{n}" }.join end |
#vendor? ⇒ Boolean
49 50 51 |
# File 'lib/xenon/media_type.rb', line 49 def vendor? @subtype.start_with?('vnd.') end |
#with_charset(charset) ⇒ ContentType
Creates a ContentType using this media type with a charset.
75 76 77 |
# File 'lib/xenon/media_type.rb', line 75 def with_charset(charset) ContentType.new(self, charset) end |
#with_q(q) ⇒ MediaRange
Creates a Xenon::MediaRange using this media type with a quality factor.
67 68 69 |
# File 'lib/xenon/media_type.rb', line 67 def with_q(q) MediaRange.new(self, q) end |