Class: MailRoom::Mailbox

Inherits:
Struct
  • Object
show all
Defined in:
lib/mail_room/mailbox.rb

Overview

Holds configuration for each of the email accounts we wish to monitor

and deliver email to when new emails arrive over imap

Constant Summary collapse

IMAP_IDLE_TIMEOUT =

Keep it to 29 minutes or less The IMAP serve will close the connection after 30 minutes of inactivity (which sending IDLE and then nothing technically is), so we re-idle every 29 minutes, as suggested by the spec: tools.ietf.org/html/rfc2177

29 * 60
DEFAULTS =

Default attributes for the mailbox configuration

{
  :search_command => 'UNSEEN',
  :delivery_method => 'postback',
  :host => 'imap.gmail.com',
  :port => 993,
  :ssl => true,
  :start_tls => false,
  :idle_timeout => IMAP_IDLE_TIMEOUT,
  :delete_after_delivery => false,
  :delivery_options => {},
  :arbitration_method => 'noop',
  :arbitration_options => {}
}

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Mailbox

Store the configuration and require the appropriate delivery method

Parameters:

  • attributes (Hash) (defaults to: {})

    configuration options



55
56
57
58
59
# File 'lib/mail_room/mailbox.rb', line 55

def initialize(attributes={})
  super(*DEFAULTS.merge(attributes).values_at(*members))

  validate!
end

Instance Method Details

#arbitration_klassObject



65
66
67
# File 'lib/mail_room/mailbox.rb', line 65

def arbitration_klass
  Arbitration[arbitration_method]
end

#arbitratorObject



73
74
75
# File 'lib/mail_room/mailbox.rb', line 73

def arbitrator
  @arbitrator ||= arbitration_klass.new(parsed_arbitration_options)
end

#deliver(message) ⇒ Object

deliver the imap email message

Parameters:

  • message (Net::IMAP::FetchData)


83
84
85
86
87
88
# File 'lib/mail_room/mailbox.rb', line 83

def deliver(message)
  body = message.attr['RFC822']
  return true unless body

  delivery.deliver(body)
end

#deliver?(uid) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/mail_room/mailbox.rb', line 77

def deliver?(uid)
  arbitrator.deliver?(uid)
end

#deliveryObject



69
70
71
# File 'lib/mail_room/mailbox.rb', line 69

def delivery
  @delivery ||= delivery_klass.new(parsed_delivery_options)
end

#delivery_klassObject



61
62
63
# File 'lib/mail_room/mailbox.rb', line 61

def delivery_klass
  Delivery[delivery_method]
end

#ssl_optionsObject

true, false, or ssl options hash



91
92
93
# File 'lib/mail_room/mailbox.rb', line 91

def ssl_options
  replace_verify_mode(ssl)
end

#validate!Object



95
96
97
98
99
# File 'lib/mail_room/mailbox.rb', line 95

def validate!
  if self[:idle_timeout] > IMAP_IDLE_TIMEOUT
    raise IdleTimeoutTooLarge.new("Please use an idle timeout smaller than #{29*60} to prevent IMAP server disconnects")
  end
end