Class: Restfulness::Headers::MediaType

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

Overview

Generic media type handling according to the RFC2616 HTTP/1.1 header fields specification.

If instantiated with a string, the MediaType object will attempt to parse and set the objects attributes.

If an empty or no string is provided, the media-type can be prepared by setting the type, subtype and optional parameters values. Calling the #to_s method will provide the compiled version.

Accessor names and parsing is based on details from en.wikipedia.org/wiki/Media_type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str = "") ⇒ MediaType

Returns a new instance of MediaType.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/restfulness/headers/media_type.rb', line 44

def initialize(str = "")
  # Defaults
  self.type = "*"
  self.subtype = "*"
  self.vendor = ""
  self.suffix = ""
  self.parameters = {}

  # Attempt to parse string if provided
  parse(str) unless str.empty?
end

Instance Attribute Details

#parametersObject

Hash of parameters using symbols as keys



42
43
44
# File 'lib/restfulness/headers/media_type.rb', line 42

def parameters
  @parameters
end

#subtypeObject

Always last part of definition. For example:

* "json" from "application/json"
* "user" from "application/vnd.example.user+json;version=1"


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

def subtype
  @subtype
end

#suffixObject

When using vendor content types, a suffix may be provided:

* "json" from "application/vnd.example.user+json"


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

def suffix
  @suffix
end

#typeObject

First part of the mime-type, typically “application”, “text”, or similar. Vendor types are not supported.



20
21
22
# File 'lib/restfulness/headers/media_type.rb', line 20

def type
  @type
end

#vendorObject

Refers to the vendor part of type string, for example:

* "example" from "application/vnd.example.user+json"


33
34
35
# File 'lib/restfulness/headers/media_type.rb', line 33

def vendor
  @vendor
end

Instance Method Details

#==(value) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/restfulness/headers/media_type.rb', line 88

def ==(value)
  if value.is_a?(String)
    value = self.class.new(value)
  end
  raise "Invalid type comparison!" unless value.is_a?(MediaType)
  type == value.type &&
    subtype == value.subtype &&
    vendor == value.vendor &&
    suffix == value.suffix &&
    parameters == value.parameters
end

#charsetObject



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

def charset
  parameters[:charset]
end

#form?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/restfulness/headers/media_type.rb', line 120

def form?
  type == "application" && subtype == "x-www-form-urlencoded"
end

#json?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/restfulness/headers/media_type.rb', line 108

def json?
  type == "application" && (subtype == "json" || suffix == "json")
end

#parse(str) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/restfulness/headers/media_type.rb', line 56

def parse(str)
  # Split between base and parameters
  parts = str.split(';').map{|p| p.strip}
  t = parts.shift.split('/', 2)
  self.type = t[0] if t[0]

  # Handle subtype, and more complex vendor + suffix
  if t[1]
    (v, s) = t[1].split('+',2)
    self.suffix = s if s
    s = v.split('.')
    s.shift if s[0] == 'vnd'
    self.subtype = s.pop
    self.vendor = s.join('.') unless s.empty?
  end

  # Finally, with remaining parts, handle parameters
  self.parameters = Hash[parts.map{|p| (k,v) = p.split('=', 2); [k.to_sym, v]}]
end

#text?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/restfulness/headers/media_type.rb', line 116

def text?
  type == "text" && subtype == "plain"
end

#to_sObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/restfulness/headers/media_type.rb', line 76

def to_s
  base = "#{type}/"
  if !vendor.empty?
    base << ["vnd", vendor, subtype].join('.')
  else
    base << subtype 
  end
  base << "+#{suffix}" unless suffix.empty?
  base << ";" + parameters.map{|k,v| "#{k}=#{v}"}.join(';') unless parameters.empty?
  base
end

#versionObject



104
105
106
# File 'lib/restfulness/headers/media_type.rb', line 104

def version
  parameters[:version]
end

#xml?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/restfulness/headers/media_type.rb', line 112

def xml?
  type == "application" && (subtype == "xml" || suffix == "xml")
end