Class: Mail::ContentTypeField

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

Constant Summary collapse

FIELD_NAME =
'content-type'
CAPITALIZED_FIELD =
'Content-Type'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

included

Methods included from CommonField

included

Constructor Details

#initialize(*args) ⇒ ContentTypeField

Returns a new instance of ContentTypeField.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mail/fields/content_type_field.rb', line 11

def initialize(*args)
  if args.last.class == Array
    @main_type = args.last[0]
    @sub_type = args.last[1]
    @parameters = ParameterHash.new.merge!(args.last.last)
    super(CAPITALIZED_FIELD, args.last)
  else
    @main_type = nil
    @sub_type = nil
    @parameters = nil
    super(CAPITALIZED_FIELD, strip_field(FIELD_NAME, args.last))
  end
  self.parse
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



120
121
122
123
124
125
126
127
# File 'lib/mail/fields/content_type_field.rb', line 120

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



81
82
83
# File 'lib/mail/fields/content_type_field.rb', line 81

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

.with_boundary(type) ⇒ Object



77
78
79
# File 'lib/mail/fields/content_type_field.rb', line 77

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

Instance Method Details

#attempt_to_cleanObject



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

def attempt_to_clean
  # Sanitize the value, handle special cases
  @element ||= Mail::ContentTypeElement.new(sanatize(value))
rescue
  # All else fails, just get the mime type
  @element ||= Mail::ContentTypeElement.new(get_mime_type(value))
end

#decodedObject



114
115
116
# File 'lib/mail/fields/content_type_field.rb', line 114

def decoded
  value
end

#defaultObject



63
64
65
# File 'lib/mail/fields/content_type_field.rb', line 63

def default
  decoded
end

#elementObject



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

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

#encodedObject

TODO: Fix this up



110
111
112
# File 'lib/mail/fields/content_type_field.rb', line 110

def encoded
  "#{CAPITALIZED_FIELD}: #{content_type};\r\n\t#{parameters.encoded};\r\n"
end

#filenameObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mail/fields/content_type_field.rb', line 97

def filename
  case
  when parameters['filename']
    @filename = parameters['filename']
  when parameters['name']
    @filename = parameters['name']
  else 
    @filename = nil
  end
  @filename
end

#main_typeObject



51
52
53
# File 'lib/mail/fields/content_type_field.rb', line 51

def main_type
  @main_type ||= element.main_type
end

#parametersObject



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

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

#parse(val = value) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/mail/fields/content_type_field.rb', line 27

def parse(val = value)
  unless val.blank?
    self.value = val 
    @element = nil
    element
  end
end

#stringObject Also known as: content_type



59
60
61
# File 'lib/mail/fields/content_type_field.rb', line 59

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

#stringify(params) ⇒ Object



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

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

#sub_typeObject



55
56
57
# File 'lib/mail/fields/content_type_field.rb', line 55

def sub_type
  @sub_type ||= element.sub_type
end

#valueObject



85
86
87
88
89
90
91
# File 'lib/mail/fields/content_type_field.rb', line 85

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