Module: MultiMail::Receiver::Base::ClassMethods
- Defined in:
- lib/multi_mail/receiver/base.rb
Instance Method Summary collapse
-
#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.
-
#condense(message) ⇒ Mail::Message
Condenses a message's HTML parts to a single HTML part.
-
#flatten(message, parts) ⇒ Mail::Message
Flattens a hierarchy of message parts.
-
#multimap(object) ⇒ Multimap
Converts a hash or array to a multimap.
-
#parse(raw) ⇒ Object
Parses raw POST data into a params hash.
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.
69 70 71 72 73 74 75 |
# File 'lib/multi_mail/receiver/base.rb', line 69 def add_file_arguments() if Hash === {:filename => [:filename], :content => [:tempfile].read} else {:filename => .original_filename, :content => .read} end end |
#condense(message) ⇒ Mail::Message
Condenses a message's HTML parts to a single HTML part.
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() if .multipart? && .parts.any?(&:multipart?) # Get the message parts as a flat array. result = flatten(Mail.new, .parts.dup) # Rebuild the message's parts. .parts.clear # Merge non-attachments with the same content type. (result.parts - result.).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 .parts << Mail::Part.new({ :content_type => content_type, :body => body, }) end # Add attachments last. result..each do |part| .parts << part end end end |
#flatten(message, parts) ⇒ Mail::Message
Flattens a hierarchy of message parts.
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/multi_mail/receiver/base.rb', line 198 def flatten(, parts) parts.each do |part| if part.multipart? flatten(, part.parts) else .parts << part end end end |
#multimap(object) ⇒ Multimap
Converts a hash or array to a multimap.
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.
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 |