Class: InboxSync::Sync

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

Defined Under Namespace

Classes: MailItemGroup

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configs = {}) ⇒ Sync

Returns a new instance of Sync.



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

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.



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

def config
  @config
end

#notify_smtpObject (readonly)

Returns the value of attribute notify_smtp.



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

def notify_smtp
  @notify_smtp
end

#source_imapObject (readonly)

Returns the value of attribute source_imap.



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

def source_imap
  @source_imap
end

Instance Method Details

#configure(&config_block) ⇒ Object



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

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

#logged_in?Boolean

Returns:

  • (Boolean)


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

def logged_in?
  !!@logged_in
end

#loggerObject



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

def logger
  @config.logger
end

#mail_item_groupsObject

this splits the mail_items list into ‘@config.num_workers` lists this spreads mail evenly across the groups with earlier items appearing earliest in each list



61
62
63
64
65
66
67
68
69
# File 'lib/inbox-sync/sync.rb', line 61

def mail_item_groups
  num_groups = @config.num_workers
  groups = []
  num_groups.times { groups << MailItemGroup.new(self) }
  get_mail_items.each_with_index do |item, i|
    groups[i % num_groups].add(item)
  end
  groups
end

#nameObject



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

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

#notify(notice) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/inbox-sync/sync.rb', line 90

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

#run(mail_item) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/inbox-sync/sync.rb', line 71

def run(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

#setupObject



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

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



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

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

#uidObject



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

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