Class: Ryespy::Listener::IMAP

Inherits:
Base
  • Object
show all
Defined in:
lib/ryespy/listener/imap.rb

Constant Summary collapse

REDIS_KEY_PREFIX =
'imap'.freeze
SIDEKIQ_JOB_CLASS =
'RyespyIMAPJob'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ IMAP

Returns a new instance of IMAP.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ryespy/listener/imap.rb', line 11

def initialize(opts = {})
  @imap_config = {
    :host      => opts[:host],
    :port      => opts[:port],
    :ssl       => opts[:ssl],
    :username  => opts[:username],
    :password  => opts[:password],
  }
  
  super(opts)
end

Instance Method Details

#check(mailbox) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ryespy/listener/imap.rb', line 27

def check(mailbox)
  @logger.debug { "mailbox: #{mailbox}" }
  
  @logger.debug { "redis_key: #{redis_key(mailbox)}" }
  
  last_seen_uid = @redis.get(redis_key(mailbox)).to_i
  
  unseen_uids = get_unseen_uids(mailbox, last_seen_uid)
  
  @logger.debug { "unseen_uids: #{unseen_uids}" }
  
  unseen_uids.each do |uid|
    @redis.set(redis_key(mailbox), uid)
    
    @notifiers.each { |n| n.notify(SIDEKIQ_JOB_CLASS, [mailbox, uid]) }
  end
  
  @logger.info { "#{mailbox} has #{unseen_uids.count} new emails" }
end

#closeObject



23
24
25
# File 'lib/ryespy/listener/imap.rb', line 23

def close
  @imap.disconnect
end