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.



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

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.



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

def config
  @config
end

#notify_smtpObject (readonly)

Returns the value of attribute notify_smtp.



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

def notify_smtp
  @notify_smtp
end

#source_imapObject (readonly)

Returns the value of attribute source_imap.



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

def source_imap
  @source_imap
end

Instance Method Details

#configure(&config_block) ⇒ Object



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

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

#logged_in?Boolean

Returns:

  • (Boolean)


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

def logged_in?
  !!@logged_in
end

#loggerObject



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

def logger
  @config.logger
end

#nameObject



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

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

#notify(notice) ⇒ Object



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

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



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

def run(runner=nil)
  return if runner && runner.shutdown?
  each_source_mail_item(runner) do |mail_item|
    begin
      logger.debug "** #{mail_item.inspect}"
      response = send_to_dest(mail_item)
      dest_uid = parse_append_response_uid(response)
      apply_dest_filters(dest_uid)
    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



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

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



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

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

#uidObject



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

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