class Xenon::MediaType

A media type.

@see ContentType @see MediaRange

Constants

JSON
XML

Attributes

params[R]
subtype[R]
type[R]

Public Class Methods

new(type, subtype, params = {}) click to toggle source

Initializes a new instance of MediaType.

@param type [String] The main type, e.g. 'application'. @param subtype [String] The subtype, e.g. 'json'. @param params [Hash] Any params for the media type; don't use 'q' or 'charset'.

# File lib/xenon/media_type.rb, line 18
def initialize(type, subtype, params = {})
  @type = type
  @subtype = subtype
  @params = params
end
parse(s) click to toggle source

Parses a media type.

@param s [String] The media type string. @return [MediaType] The media type.

# 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

Public Instance Methods

==(other) click to toggle source
# File lib/xenon/media_type.rb, line 59
def ==(other)
  @type == other.type && @subtype == other.subtype && @params == other.params
end
experimental?() click to toggle source
# 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?() click to toggle source
# File lib/xenon/media_type.rb, line 45
def personal?
  @subtype.start_with?('prs.')
end
to_s() click to toggle source
# File lib/xenon/media_type.rb, line 79
def to_s
  "#{@type}/#{@subtype}" << @params.map { |n, v| v ? "; #{n}=#{v}" : "; #{n}" }.join
end
vendor?() click to toggle source
# File lib/xenon/media_type.rb, line 49
def vendor?
  @subtype.start_with?('vnd.')
end
with_charset(charset) click to toggle source

Creates a {ContentType} using this media type with a charset.

@param charset [String] The desired charset, e.g. 'utf-8'. @return [ContentType] The content type.

# File lib/xenon/media_type.rb, line 75
def with_charset(charset)
  ContentType.new(self, charset)
end
with_q(q) click to toggle source

Creates a {MediaRange} using this media type with a quality factor.

@param q [Numeric] A value between 1.0 (most desirable) and 0.0 (not acceptable). @return [MediaRange] The media range.

# File lib/xenon/media_type.rb, line 67
def with_q(q)
  MediaRange.new(self, q)
end