Sha256: 714413d81a5777c8f649208a3c3c96432fe60a31deb913a69a6d99b650fd3441

Contents?: true

Size: 1.82 KB

Versions: 8

Compression:

Stored size: 1.82 KB

Contents

require 'set'

# This class decomposes a Mail::Message into a PostageApp API call that
# produces the same result when sent.

class PostageApp::Mail::Arguments
  # Certain headers need to be ignored since they are generated internally.
  HEADERS_IGNORED = Set.new(%w[
    Content-Type
    To
  ]).freeze

  # Creates a new instance with the given Mail::Message binding.
  def initialize(mail)
    @mail = mail
  end

  # Returns the extracted arguments. If a pre-existing arguments has is
  # supplied, arguments are injected into that.
  def extract(arguments = nil)
    arguments ||= { }
    arguments['content'] ||= { }
    arguments['headers'] ||= { }

    arguments['recipients'] = @mail.to

    if (@mail.multipart?)
      @mail.parts.each do |part|
        if (part.content_disposition)
          add_attachment(arguments, part)
        else
          add_part(arguments, part)
        end
      end
    else
      add_part(arguments, @mail)
    end

    _headers = arguments['headers']
    @mail.header.fields.each do |field|
      next if (HEADERS_IGNORED.include?(field.name))

      _headers[field.name] = field.value
    end

    if (@mail.has_attachments?)
      @mail.attachments.each do |attachment|
        add_attachment(arguments, attachment)
      end
    end

    [ :send_message, arguments ]
  end

protected
  def add_part(arguments, part)
    case (part.content_type.to_s.split(/\s*;/).first)
    when 'text/html'
      arguments['content']['text/html'] = part.body.to_s
    when 'text/plain', nil
      arguments['content']['text/plain'] = part.body.to_s
    else
      # Unknown type.
    end
  end

  def add_attachment(arguments, part)
    arguments['attachments'] ||= { }

    arguments['attachments'][part.filename] = {
      'content' => Base64.encode64(part.body.to_s),
      'content_type' => part.content_type
    }
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
postageapp-1.4.2 lib/postageapp/mail/arguments.rb
postageapp-1.4.1 lib/postageapp/mail/arguments.rb
postageapp-1.4.0 lib/postageapp/mail/arguments.rb
postageapp-1.3.1 lib/postageapp/mail/arguments.rb
postageapp-1.3.0 lib/postageapp/mail/arguments.rb
postageapp-1.2.6 lib/postageapp/mail/arguments.rb
postageapp-1.2.5 lib/postageapp/mail/arguments.rb
postageapp-1.2.0 lib/postageapp/mail/arguments.rb