Class: Xenon::MediaType

Inherits:
Object
  • Object
show all
Defined in:
lib/xenon/media_type.rb

Overview

A media type.

Constant Summary collapse

JSON =
MediaType.new('application', 'json')
XML =
MediaType.new('application', 'xml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, subtype, params = {}) ⇒ MediaType

Initializes a new instance of MediaType.

Parameters:

  • type (String)

    The main type, e.g. ‘application’.

  • subtype (String)

    The subtype, e.g. ‘json’.

  • params (Hash) (defaults to: {})

    Any params for the media type; don’t use ‘q’ or ‘charset’.



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

#paramsObject (readonly)

Returns the value of attribute params.



11
12
13
# File 'lib/xenon/media_type.rb', line 11

def params
  @params
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



11
12
13
# File 'lib/xenon/media_type.rb', line 11

def subtype
  @subtype
end

#typeObject (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.

Parameters:

  • s (String)

    The media type string.

Returns:



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

Returns:

  • (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

Returns:

  • (Boolean)


45
46
47
# File 'lib/xenon/media_type.rb', line 45

def personal?
  @subtype.start_with?('prs.')
end

#to_sObject



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

Returns:

  • (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.

Parameters:

  • charset (String)

    The desired charset, e.g. ‘utf-8’.

Returns:



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.

Parameters:

  • q (Numeric)

    A value between 1.0 (most desirable) and 0.0 (not acceptable).

Returns:



67
68
69
# File 'lib/xenon/media_type.rb', line 67

def with_q(q)
  MediaRange.new(self, q)
end