Sha256: 7805c83156efab2a6dc3bfae19409d058e0264de9adb2218d15b8ffcb7224255

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

module MailRoom
  # Mailbox Configuration fields
  MAILBOX_FIELDS = [
    :email,
    :password,
    :host,
    :port,
    :ssl,
    :search_command,
    :name,
    :delete_after_delivery,
    :delivery_method, # :noop, :logger, :postback, :letter_opener
    :log_path, # for logger
    :delivery_url, # for postback
    :delivery_token, # for postback
    :location, # for letter_opener
    :delivery_options
  ]

  # Holds configuration for each of the email accounts we wish to monitor
  #   and deliver email to when new emails arrive over imap
  Mailbox = Struct.new(*MAILBOX_FIELDS) do
    # Default attributes for the mailbox configuration
    DEFAULTS = {
      :search_command => 'UNSEEN',
      :delivery_method => 'postback',
      :host => 'imap.gmail.com',
      :port => 993,
      :ssl => true,
      :delete_after_delivery => false,
      :delivery_options => {}
    }

    # Store the configuration and require the appropriate delivery method
    # @param attributes [Hash] configuration options
    def initialize(attributes={})
      super(*DEFAULTS.merge(attributes).values_at(*members))

      require_relative("./delivery/#{(delivery_method)}")
    end

    # move to a mailbox deliverer class?
    def delivery_klass
      case delivery_method
      when "noop"
        Delivery::Noop
      when "logger"
        Delivery::Logger
      when "letter_opener"
        Delivery::LetterOpener
      when "sidekiq"
        Delivery::Sidekiq
      else
        Delivery::Postback
      end
    end

    # deliver the imap email message
    # @param message [Net::IMAP::FetchData]
    def deliver(message)
      message = message.attr['RFC822']
      return true unless message
      
      delivery_klass.new(parsed_delivery_options).deliver(message)
    end

    private
    def parsed_delivery_options
      delivery_klass::Options.new(self)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mail_room-0.4.2 lib/mail_room/mailbox.rb
mail_room-0.4.1 lib/mail_room/mailbox.rb