Class: Mail::ContentTypeField

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

Constant Summary

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

Constants included from Utilities

Utilities::CRLF, Utilities::CRLF_REGEX, Utilities::LF

Constants included from Constants

Mail::Constants::ASTERISK, Mail::Constants::ATOM_UNSAFE, Mail::Constants::B_VALUES, Mail::Constants::CAPITAL_M, Mail::Constants::COLON, Mail::Constants::CONTROL_CHAR, Mail::Constants::CR, Mail::Constants::CRLF, Mail::Constants::CR_ENCODED, Mail::Constants::EMPTY, Mail::Constants::ENCODED_VALUE, Mail::Constants::EQUAL_LF, Mail::Constants::FIELD_BODY, Mail::Constants::FIELD_LINE, Mail::Constants::FIELD_PREFIX, Mail::Constants::FIELD_SPLIT, Mail::Constants::FULL_ENCODED_VALUE, Mail::Constants::FWS, Mail::Constants::HEADER_LINE, Mail::Constants::HEADER_SPLIT, Mail::Constants::HYPHEN, Mail::Constants::LF, Mail::Constants::LF_ENCODED, Mail::Constants::NULL_SENDER, Mail::Constants::PHRASE_UNSAFE, Mail::Constants::QP_SAFE, Mail::Constants::QP_UNSAFE, Mail::Constants::Q_VALUES, Mail::Constants::SPACE, Mail::Constants::TEXT, Mail::Constants::TOKEN_UNSAFE, Mail::Constants::UNDERSCORE, Mail::Constants::WSP

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StructuredField

#charset, #charset=, #errors

Methods included from Utilities

#atom_safe?, blank?, #bracket, #capitalize_field, #constantize, #dasherize, #dquote, #escape_paren, #map_lines, #map_with_index, #match_to_s, #paren, #quote_atom, #quote_phrase, #quote_token, to_crlf, to_lf, #token_safe?, #unbracket, #underscoreize, #unparen, unquote, #uri_escape, #uri_parser, #uri_unescape

Methods included from CommonField

#field_length, #name, #name=, #responsible_for?, #to_s, #value=

Constructor Details

#initialize(value = nil, charset = 'utf-8') ⇒ ContentTypeField

Returns a new instance of ContentTypeField



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

def initialize(value = nil, charset = 'utf-8')
  self.charset = charset
  if value.class == Array
    @main_type = value[0]
    @sub_type = value[1]
    @parameters = ParameterHash.new.merge!(value.last)
  else
    @main_type = nil
    @sub_type = nil
    @parameters = nil
    value = strip_field(FIELD_NAME, value)
  end
  value = ensure_filename_quoted(value)
  super(CAPITALIZED_FIELD, value, charset)
  self.parse
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



132
133
134
135
136
137
138
139
# File 'lib/mail/fields/content_type_field.rb', line 132

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



83
84
85
# File 'lib/mail/fields/content_type_field.rb', line 83

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

.with_boundary(type) ⇒ Object



79
80
81
# File 'lib/mail/fields/content_type_field.rb', line 79

def ContentTypeField.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
  @element ||= Mail::ContentTypeElement.new(sanatize(value))
rescue
  # All else fails, just get the MIME media type
  @element ||= Mail::ContentTypeElement.new(get_mime_type(value))
end

#decodedObject



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

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

#defaultObject



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

def default
  decoded
end

#elementObject



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

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

#encodedObject

TODO: Fix this up



112
113
114
115
116
117
118
119
# File 'lib/mail/fields/content_type_field.rb', line 112

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

#filenameObject



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

def filename
  case
  when parameters['filename']
    @filename = parameters['filename']
  when parameters['name']
    @filename = parameters['name']
  else
    @filename = nil
  end
  @filename
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



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

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

#parse(val = value) ⇒ Object



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

def parse(val = value)
  unless Utilities.blank?(val)
    self.value = val
    @element = nil
    element
  end
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



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

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



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

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