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.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsed) ⇒ ContentType

Returns a new instance of ContentType.



10
11
12
13
14
15
16
# File 'lib/content_type.rb', line 10

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 = (parse_parameters parsed[1..-1]).to_h
end

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters.



8
9
10
# File 'lib/content_type.rb', line 8

def parameters
  @parameters
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



8
9
10
# File 'lib/content_type.rb', line 8

def subtype
  @subtype
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/content_type.rb', line 8

def type
  @type
end

Class Method Details

.parse(str) ⇒ Object



36
37
38
# File 'lib/content_type.rb', line 36

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

Instance Method Details

#charsetObject



22
23
24
# File 'lib/content_type.rb', line 22

def charset
  parameters["charset"]
end

#inspectObject



26
27
28
# File 'lib/content_type.rb', line 26

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

#mime_typeObject



18
19
20
# File 'lib/content_type.rb', line 18

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

#to_sObject Also known as: to_str



30
31
32
33
# File 'lib/content_type.rb', line 30

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