Module: MultiMail::Receiver::Base::ClassMethods

Defined in:
lib/multi_mail/receiver/base.rb

Instance Method Summary collapse

Instance Method Details

#add_file_arguments(attachment) ⇒ Hash

ActionDispatch::Http::Request subclasses Rack::Request and turns attachment hashes into instances of ActionDispatch::Http::UploadedFile in Rails 3 and 4 and instances of ActionController::UploadedFile in Rails 2.3, both of which have the same interface.

Parameters:

  • attachment (ActionDispatch::Http::UploadedFile, ActionController::UploadedFile, Hash)

    an attachment

Returns:

  • (Hash)

    arguments for Mail::Message#add_file



69
70
71
72
73
74
75
# File 'lib/multi_mail/receiver/base.rb', line 69

def add_file_arguments(attachment)
  if Hash === attachment
    {:filename => attachment[:filename], :content => attachment[:tempfile].read}
  else
    {:filename => attachment.original_filename, :content => attachment.read}
  end
end

#condense(message) ⇒ Mail::Message

Condenses a message's HTML parts to a single HTML part.

Examples:

flat = self.class.condense(message.dup)

Parameters:

  • message (Mail::Message)

    a message with zero or more HTML parts

Returns:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/multi_mail/receiver/base.rb', line 154

def condense(message)
  if message.multipart? && message.parts.any?(&:multipart?)
    # Get the message parts as a flat array.
    result = flatten(Mail.new, message.parts.dup)

    # Rebuild the message's parts.
    message.parts.clear

    # Merge non-attachments with the same content type.
    (result.parts - result.attachments).group_by(&:content_type).each do |content_type,group|
      body = group.map{|part| part.body.decoded}.join

      # Make content types match across all APIs.
      if content_type == 'text/plain; charset=us-ascii'
        # `text/plain; charset=us-ascii` is the default content type.
        content_type = 'text/plain'
      elsif content_type == 'text/html; charset=us-ascii'
        content_type = 'text/html; charset=UTF-8'
        body = body.encode('UTF-8') if body.respond_to?(:encode)
      end

      message.parts << Mail::Part.new({
        :content_type => content_type,
        :body => body,
      })
    end

    # Add attachments last.
    result.attachments.each do |part|
      message.parts << part
    end
  end

  message
end

#flatten(message, parts) ⇒ Mail::Message

Flattens a hierarchy of message parts.

Examples:

flat = self.class.flatten(Mail.new, parts.dup)

Parameters:

  • message (Mail::Message)

    a message

  • parts (Mail::PartsList)

    parts to add to the message

Returns:



198
199
200
201
202
203
204
205
206
207
# File 'lib/multi_mail/receiver/base.rb', line 198

def flatten(message, parts)
  parts.each do |part|
    if part.multipart?
      flatten(message, part.parts)
    else
      message.parts << part
    end
  end
  message
end

#multimap(object) ⇒ Multimap

Converts a hash or array to a multimap.

Parameters:

  • object (Hash, Array)

    a hash or array

Returns:



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/multi_mail/receiver/base.rb', line 81

def multimap(object)
  multimap = Multimap.new
  object.each do |key,value|
    if Array === value
      value.each do |v|
        multimap[key] = v
      end
    else
      multimap[key] = value
    end
  end
  multimap
end

#parse(raw) ⇒ Object

Parses raw POST data into a params hash.

Parameters:

  • raw (String, Hash)

    raw POST data or a params hash

Raises:

  • (ArgumentError)

    if the argument is not a string or a hash



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/multi_mail/receiver/base.rb', line 99

def parse(raw)
  case raw
  when String
    begin
      JSON.load(raw)
    rescue JSON::ParserError
      params = CGI.parse(raw)

      # Flatten the parameters.
      params.each do |key,value|
        if Array === value && value.size == 1
          params[key] = value.first
        end
      end

      params
    end
  when Array
    params = {}

    # Collect the values for each key.
    map = Multimap.new
    raw.each do |key,value|
      map[key] = value
    end

    # Flatten the parameters.
    map.each_pair do |key,value|
      if Array === value && value.size == 1
        params[key] = value.first
      else
        params[key] = value
      end
    end

    params
  when Rack::Request
    env = raw.env.dup
    env.delete('rack.input')
    env.delete('rack.errors')
    {'env' => env}.merge(raw.params)
  when Hash
    raw
  else
    raise ArgumentError, "Can't handle #{raw.class.name} input"
  end
end