Class: Mbox::Mail::Headers::ContentType

Inherits:
Object
  • Object
show all
Defined in:
lib/mbox/mail/headers/content_type.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ ContentType

Returns a new instance of ContentType.



45
46
47
48
49
# File 'lib/mbox/mail/headers/content_type.rb', line 45

def initialize (data = {})
	@mime     = data[:mime] || 'text/plain'
	@charset  = data[:charset]
	@boundary = data[:boundary]
end

Instance Attribute Details

#boundaryObject

Returns the value of attribute boundary.



43
44
45
# File 'lib/mbox/mail/headers/content_type.rb', line 43

def boundary
  @boundary
end

#charsetObject

Returns the value of attribute charset.



43
44
45
# File 'lib/mbox/mail/headers/content_type.rb', line 43

def charset
  @charset
end

#mimeObject

Returns the value of attribute mime.



43
44
45
# File 'lib/mbox/mail/headers/content_type.rb', line 43

def mime
  @mime
end

Class Method Details

.parse(text) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mbox/mail/headers/content_type.rb', line 23

def self.parse (text)
	return text if text.is_a?(ContentType)

	return ContentType.new unless text && text.is_a?(String)

	stuff = text.gsub(/\n\r/, '').split(/\s*;\s*/)
	type  = stuff.shift

	ContentType.new(Hash[stuff.map {|stuff|
		stuff    = stuff.strip.split(/=/)
		stuff[0] = stuff[0].to_sym

		if stuff[1][0] == '"' && stuff[1][stuff[1].length-1] == '"'
			stuff[1] = stuff[1][1, stuff[1].length-2]
		end

		stuff
	}].merge(mime: type))
end

Instance Method Details

#to_sObject



51
52
53
# File 'lib/mbox/mail/headers/content_type.rb', line 51

def to_s
	"#{mime}#{"; charset=#{charset}" if charset}#{"; boundary=#{boundary}" if boundary}"
end