Class: HttpObjects::Parameters::MediaType

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

Overview

3.7. Media Types

media-type = type “/” subtype *( “;” parameter ) type = token subtype = token

Examples

text/html; charset=ISO-8859-4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, value, parameters) ⇒ MediaType

Returns a new instance of MediaType.



30
31
32
33
34
# File 'lib/http_objects/parameters/media_type.rb', line 30

def initialize(raw, value, parameters)
  @raw = raw
  @value = value
  @parameters = parameters
end

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters.



13
14
15
# File 'lib/http_objects/parameters/media_type.rb', line 13

def parameters
  @parameters
end

#rawObject (readonly)

Returns the value of attribute raw.



13
14
15
# File 'lib/http_objects/parameters/media_type.rb', line 13

def raw
  @raw
end

#valueObject (readonly)

Returns the value of attribute value.



13
14
15
# File 'lib/http_objects/parameters/media_type.rb', line 13

def value
  @value
end

Class Method Details

.parse(value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/http_objects/parameters/media_type.rb', line 15

def self.parse(value)
  raw = value
  value, params = value.to_s.split(";", 2)
  value = value.to_s
  params = params.to_s

  parameters = {}
  params.split(/\s*;\s*/).each do |param|
    name, param_value = param.split(/=/)
    name = name.gsub(/\s*/, "")
    parameters[name.downcase] = param_value
  end
  self.new(raw, value, parameters)
end

Instance Method Details

#charset!Object



40
41
42
# File 'lib/http_objects/parameters/media_type.rb', line 40

def charset!
  @parameters.fetch("charset", nil)
end

#charset?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/http_objects/parameters/media_type.rb', line 36

def charset?
  !!self.charset!
end