Class: InboxSync::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/inbox-sync/sync.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configs = {}) ⇒ Sync

Returns a new instance of Sync.



13
14
15
16
17
18
# File 'lib/inbox-sync/sync.rb', line 13

def initialize(configs={})
  @config = InboxSync::Config.new(configs)
  @source_imap = nil
  @notify_smtp = nil
  @logged_in   = false
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/inbox-sync/sync.rb', line 11

def config
  @config
end

#notify_smtpObject (readonly)

Returns the value of attribute notify_smtp.



11
12
13
# File 'lib/inbox-sync/sync.rb', line 11

def notify_smtp
  @notify_smtp
end

#source_imapObject (readonly)

Returns the value of attribute source_imap.



11
12
13
# File 'lib/inbox-sync/sync.rb', line 11

def source_imap
  @source_imap
end

Instance Method Details

#configure(&config_block) ⇒ Object



36
37
38
39
# File 'lib/inbox-sync/sync.rb', line 36

def configure(&config_block)
  @config.instance_eval(&config_block) if config_block
  self
end

#logged_in?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/inbox-sync/sync.rb', line 32

def logged_in?
  !!@logged_in
end

#loggerObject



20
21
22
# File 'lib/inbox-sync/sync.rb', line 20

def logger
  @config.logger
end

#nameObject



28
29
30
# File 'lib/inbox-sync/sync.rb', line 28

def name
  "#{@config.source..user} (#{@config.source.host})"
end

#notify(notice) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/inbox-sync/sync.rb', line 76

def notify(notice)
  logger.info "** sending '#{notice.subject}' to #{notice.to.inspect}"
  begin
    notice.send
  rescue Exception => err
    log_error(err)
  end
end

#run(runner = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/inbox-sync/sync.rb', line 55

def run(runner=nil)
  return if runner && runner.shutdown?
  each_source_mail_item(runner) do |mail_item|
    begin
      response = send_to_dest(mail_item)
      dest_uid = parse_append_response_uid(response)
      logger.debug "** dest uid: #{dest_uid.inspect}"
    rescue Exception => err
      log_error(err)
      notify(Notice::SyncMailItemError.new(@notify_smtp, @config.notify, {
        :error => err,
        :mail_item => mail_item,
        :sync => self
      }))
    ensure
      archive_on_source(mail_item)
      mail_item = nil
    end
  end
end

#setupObject



41
42
43
44
45
46
47
# File 'lib/inbox-sync/sync.rb', line 41

def setup
  logger.info "=== #{config_log_detail(@config.source)} sync started. ==="

  @notify_smtp ||= setup_smtp(:notify, @config.notify)
  @config.validate!
   if !logged_in?
end

#teardownObject



49
50
51
52
53
# File 'lib/inbox-sync/sync.rb', line 49

def teardown
  logout if logged_in?
  @source_imap = @notify_smtp = nil
  logger.info "=== #{config_log_detail(@config.source)} sync finished. ==="
end

#uidObject



24
25
26
# File 'lib/inbox-sync/sync.rb', line 24

def uid
  "#{@config.source..user}:#{@config.source.host}"
end