Class: ContentType

Inherits:
Object
  • Object
show all
Defined in:
lib/content_type.rb,
lib/content_type/parser.rb,
lib/content_type/version.rb

Overview

ContentType structure

Defined Under Namespace

Classes: Parser

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsed) ⇒ ContentType

Returns a new instance of ContentType.



12
13
14
15
16
17
18
# File 'lib/content_type.rb', line 12

def initialize(parsed)
  parsed = [parsed] unless parsed.is_a? Array

  @type       = parsed.first[:type].to_s.downcase
  @subtype    = parsed.first[:subtype].to_s.downcase
  @parameters = Hash[parse_parameters parsed[1..-1]]
end

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters.



10
11
12
# File 'lib/content_type.rb', line 10

def parameters
  @parameters
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



10
11
12
# File 'lib/content_type.rb', line 10

def subtype
  @subtype
end

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/content_type.rb', line 10

def type
  @type
end

Class Method Details

.parse(str) ⇒ Object



38
39
40
# File 'lib/content_type.rb', line 38

def self.parse(str)
  new Parser.new.parse str
end

Instance Method Details

#charsetObject



24
25
26
# File 'lib/content_type.rb', line 24

def charset
  parameters["charset"]
end

#inspectObject



28
29
30
# File 'lib/content_type.rb', line 28

def inspect
  "#<ContentType #{mime_type} #{parameters.inspect}>"
end

#mime_typeObject



20
21
22
# File 'lib/content_type.rb', line 20

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

#to_sObject Also known as: to_str



32
33
34
35
# File 'lib/content_type.rb', line 32

def to_s
  (["#{mime_type}"] + parameters.map { |k, v| "#{k}=#{v.to_s.inspect}" })
    .compact.join("; ")
end