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.


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

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)


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

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


19
20
21
# File 'lib/mail/fields/content_type_field.rb', line 19

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

.singular?Boolean

Returns:

  • (Boolean)

11
12
13
# File 'lib/mail/fields/content_type_field.rb', line 11

def singular?
  true
end

.with_boundary(type) ⇒ Object


15
16
17
# File 'lib/mail/fields/content_type_field.rb', line 15

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

Instance Method Details

#attempt_to_cleanObject


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

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


101
102
103
104
# File 'lib/mail/fields/content_type_field.rb', line 101

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

#defaultObject


68
69
70
# File 'lib/mail/fields/content_type_field.rb', line 68

def default
  decoded
end

#elementObject


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

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

#encodedObject


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

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

#filenameObject


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

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

#main_typeObject


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

def main_type
  @main_type ||= element.main_type
end

#parametersObject


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

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

#stringObject Also known as: content_type


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

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

#stringify(params) ⇒ Object


88
89
90
# File 'lib/mail/fields/content_type_field.rb', line 88

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

#sub_typeObject


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

def sub_type
  @sub_type ||= element.sub_type
end

#valueObject


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

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