Class: Webbed::MediaType

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

Overview

Representation of an HTTP Media Type.

Constant Summary collapse

MIME_TYPE_REGEX =
/^([-\w.+]+)\/([-\w.+]*)$/
PARAMETERS_REGEX =
/=|\s*;\s*/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(media_type) ⇒ MediaType

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



54
55
56
57
# File 'lib/webbed/media_type.rb', line 54

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

Instance Attribute Details

#parametersHash{String => String}

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})


39
40
41
# File 'lib/webbed/media_type.rb', line 39

def parameters
  @parameters
end

#subtypeString

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)


27
28
29
# File 'lib/webbed/media_type.rb', line 27

def subtype
  @subtype
end

#typeString

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)


16
17
18
# File 'lib/webbed/media_type.rb', line 16

def type
  @type
end

Instance Method Details

#==(other_media_type) ⇒ Boolean

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)


133
134
135
# File 'lib/webbed/media_type.rb', line 133

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

#interpretable_asArray<String>

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>)


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

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

#mime_typeString

The MIME type of the Media Type.

Returns:

  • (String)


62
63
64
# File 'lib/webbed/media_type.rb', line 62

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

#mime_type=(mime_type)

Sets the MIME type of the Media Type.

Parameters:

  • mime_type (String)


69
70
71
72
73
# File 'lib/webbed/media_type.rb', line 69

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

#suffixString?

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)


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

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

#to_sString

Converts the Media Type to a string.

Returns:

  • (String)


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

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

#vendor_specific?Boolean

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)


100
101
102
# File 'lib/webbed/media_type.rb', line 100

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