Class: Mail::ContentTypeField

Inherits:
NamedStructuredField show all
Defined in:
lib/mail/fields/content_type_field.rb

Overview

:nodoc:

Constant Summary collapse

NAME =
'Content-Type'

Instance Attribute Summary

Attributes inherited from CommonField

#charset, #errors, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommonField

parse, #parse, #responsible_for?, #singular?, #to_s

Constructor Details

#initialize(value = nil, charset = nil) ⇒ ContentTypeField

Returns a new instance of ContentTypeField.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mail/fields/content_type_field.rb', line 22

def initialize(value = nil, charset = nil)
  if value.is_a? Array
    @main_type = value[0]
    @sub_type = value[1]
    @parameters = ParameterHash.new.merge!(value.last)
  else
    @main_type = nil
    @sub_type = nil
    value = value.to_s
  end

  super ensure_filename_quoted(value), charset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



106
107
108
109
110
111
112
113
# File 'lib/mail/fields/content_type_field.rb', line 106

def method_missing(name, *args, &block)
  if name.to_s =~ /(\w+)=/
    self.parameters[$1] = args.first
    @value = "#{content_type}; #{stringify(parameters)}"
  else
    super
  end
end

Class Method Details

.generate_boundaryObject



17
18
19
# File 'lib/mail/fields/content_type_field.rb', line 17

def generate_boundary
  "--==_mimepart_#{Mail.random_tag}"
end

.singular?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/mail/fields/content_type_field.rb', line 9

def singular?
  true
end

.with_boundary(type) ⇒ Object



13
14
15
# File 'lib/mail/fields/content_type_field.rb', line 13

def with_boundary(type)
  new "#{type}; boundary=#{generate_boundary}"
end

Instance Method Details

#attempt_to_cleanObject



45
46
47
48
49
50
51
# File 'lib/mail/fields/content_type_field.rb', line 45

def attempt_to_clean
  # Sanitize the value, handle special cases
  Mail::ContentTypeElement.new(sanitize(value))
rescue Mail::Field::ParseError
  # All else fails, just get the MIME media type
  Mail::ContentTypeElement.new(get_mime_type(value))
end

#decodedObject



99
100
101
102
# File 'lib/mail/fields/content_type_field.rb', line 99

def decoded
  p = "; #{parameters.decoded}" if parameters && parameters.length > 0
  "#{content_type}#{p}"
end

#defaultObject



66
67
68
# File 'lib/mail/fields/content_type_field.rb', line 66

def default
  decoded
end

#elementObject



36
37
38
39
40
41
42
43
# File 'lib/mail/fields/content_type_field.rb', line 36

def element
  @element ||=
    begin
      Mail::ContentTypeElement.new(value)
    rescue Mail::Field::ParseError
      attempt_to_clean
    end
end

#encodedObject



94
95
96
97
# File 'lib/mail/fields/content_type_field.rb', line 94

def encoded
  p = ";\r\n\s#{parameters.encoded}" if parameters && parameters.length > 0
  "#{name}: #{content_type}#{p}\r\n"
end

#filenameObject



90
91
92
# File 'lib/mail/fields/content_type_field.rb', line 90

def filename
  @filename ||= parameters['filename'] || parameters['name']
end

#main_typeObject



53
54
55
# File 'lib/mail/fields/content_type_field.rb', line 53

def main_type
  @main_type ||= element.main_type
end

#parametersObject



70
71
72
73
74
75
76
# File 'lib/mail/fields/content_type_field.rb', line 70

def parameters
  unless defined? @parameters
    @parameters = ParameterHash.new
    element.parameters.each { |p| @parameters.merge!(p) }
  end
  @parameters
end

#stringObject Also known as: content_type



61
62
63
# File 'lib/mail/fields/content_type_field.rb', line 61

def string
  "#{main_type}/#{sub_type}"
end

#stringify(params) ⇒ Object



86
87
88
# File 'lib/mail/fields/content_type_field.rb', line 86

def stringify(params)
  params.map { |k,v| "#{k}=#{Encodings.param_encode(v)}" }.join("; ")
end

#sub_typeObject



57
58
59
# File 'lib/mail/fields/content_type_field.rb', line 57

def sub_type
  @sub_type ||= element.sub_type
end

#valueObject



78
79
80
81
82
83
84
# File 'lib/mail/fields/content_type_field.rb', line 78

def value
  if @value.is_a? Array
    "#{@main_type}/#{@sub_type}; #{stringify(parameters)}"
  else
    @value
  end
end