Class: Webbed::MediaType

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

Constant Summary

MIME_TYPE_REGEX =

Regular expression for parsing the type and subtype of a MIME type

/^([-\w.+]+)\/([-\w.+]*)$/
PARAMETERS_REGEX =

Regular expression for parsing the parameters of a Media Type

/=|\s*;\s*/

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (MediaType) initialize(media_type)

Creates a new Media Type

Examples:

Create a MediaType without parameters

media_type = Webbed::MediaType.new('text/html')
media_type.mime_type # => 'text/html'
media_type.parameters # => {}

Create a MediaType with parameters

media_type = Webbed::MediaType.new('text/html; q=1.0')
media_type.mime_type # => 'text/html'
media_type.parameters # => { 'q' => '1.0' }

Parameters:

  • media_type (String)

    the Media Type to create as defined in RFC 2616



56
57
58
59
# File 'lib/webbed/media_type.rb', line 56

def initialize(media_type)
  self.mime_type, *parameters = media_type.split(PARAMETERS_REGEX)
  self.parameters = Hash[*parameters] || {}
end

Instance Attribute Details

- (Hash<String => String>) parameters

The parameters of the Media Type

According to RFC 2616, parameters are separated from the MIME type and each other using a semicolon.

Examples:

media_type = Webbed::MediaType.new('text/html; q=1.0')
media_type.parameters # => { 'q' => '1.0' }

Returns:

  • (Hash<String => String>)


35
36
37
# File 'lib/webbed/media_type.rb', line 35

def parameters
  @parameters
end

- (String) subtype

The subtype of the MIME type

According to RFC 2616, this is the after before the slash.

Examples:

media_type = Webbed::MediaType.new('text/html')
media_type.type # => 'html'

Returns:

  • (String)


23
24
25
# File 'lib/webbed/media_type.rb', line 23

def subtype
  @subtype
end

- (String) type

The type of the MIME type

According to RFC 2616, this is the part before the slash.

Examples:

media_type = Webbed::MediaType.new('text/html')
media_type.type # => 'text'

Returns:

  • (String)


12
13
14
# File 'lib/webbed/media_type.rb', line 12

def type
  @type
end

Instance Method Details

- (Boolean) ==(other_media_type)

Compares the Media Type to another Media Type

Two Media Types are equal if their #mime_type's are equal.

Parameters:

  • other_media_type (#mime_type)

    the other Media Type

Returns:

  • (Boolean)


135
136
137
# File 'lib/webbed/media_type.rb', line 135

def ==(other_media_type)
  mime_type == other_media_type.mime_type
end

- (Array<String>) interpretable_as

The MIME types that the Media Type can be interpreted as

This uses the suffix to generate a list of MIME types that can be used to interpret the Media Type. It's useful if you need to be able to parse XML or JSON Media Types that may or may not have suffixes using general XML or JSON parsers.

Examples:

media_type = Webbed::MediaType.new('application/xml')
media_type.interpretable_as # ['application/xml']

media_type = Webbed::MediaType.new('application/atom+xml')
media_type.interpretable_as # => ['application/atom+xml', 'application/xml']

Returns:

  • (Array<String>)


154
155
156
157
158
159
160
# File 'lib/webbed/media_type.rb', line 154

def interpretable_as
  if suffix
    [mime_type, "#{type}/#{suffix}"]
  else
    [mime_type]
  end
end

- (String) mime_type

The MIME type of the Media Type

Returns:

  • (String)


64
65
66
# File 'lib/webbed/media_type.rb', line 64

def mime_type
  "#{type}/#{subtype}"
end

- mime_type=(new_mime_type)

Sets the MIME type of the Media Type

Parameters:

  • new_mime_type (String)


71
72
73
74
75
# File 'lib/webbed/media_type.rb', line 71

def mime_type=(new_mime_type)
  MIME_TYPE_REGEX =~ new_mime_type
  self.type = $1
  self.subtype = $2
end

- (String?) suffix

The suffix of the MIME type

Suffixes follow the convention set forth by the Atom specification: separated from the rest of the MIME Type by a +.

Examples:

media_type = Webbed::MediaType.new('application/xml')
media_type.suffix # => nil

media_type = Webbed::MediaType.new('application/atom+xml')
media_type.suffix # => 'xml'

Returns:

  • (String, nil)


119
120
121
122
123
124
125
126
127
# File 'lib/webbed/media_type.rb', line 119

def suffix
  suffix = subtype.split('+')[-1]
  
  if suffix != subtype
    suffix
  else
    nil
  end
end

- (String) to_s

Converts the Media Type to a string

Returns:

  • (String)


80
81
82
83
84
85
86
87
# File 'lib/webbed/media_type.rb', line 80

def to_s
  if parameters.empty?
    mime_type
  else
    parameters = self.parameters.map { |k, v| "#{k}=#{v}" }.join('; ')
    "#{mime_type}; #{parameters}"
  end
end

- (Boolean) vendor_specific?

Whether or not the Media Type is vendor-specific

The method uses the vnd. prefix convention to determine whether or not it was created for a specific vendor.

Examples:

media_type = Webbed::MediaType.new('application/json')
media_type.vendor_specific? # => false

media_type = Webbed::MediaType.new('application/vnd.my-special-type')
media_type.vendor_specific? # => true

Returns:

  • (Boolean)


102
103
104
# File 'lib/webbed/media_type.rb', line 102

def vendor_specific?
  subtype[0..3] == 'vnd.'
end