class Minbox::Inbox
Public Class Methods
instance(root_dir:)
click to toggle source
# File lib/minbox/inbox.rb, line 7 def self.instance(root_dir:) @instances ||= {} @instances[root_dir] ||= new(root_dir: root_dir) end
new(root_dir:)
click to toggle source
# File lib/minbox/inbox.rb, line 12 def initialize(root_dir:) @semaphore = Mutex.new start_listening(root_dir) empty! end
Public Instance Methods
each() { |email| ... }
click to toggle source
# File lib/minbox/inbox.rb, line 54 def each @emails.each do |id, email| yield email end end
emails(count: 0)
click to toggle source
# File lib/minbox/inbox.rb, line 18 def emails(count: 0) wait_until { |x| x.count >= count } if count > 0 with_lock do |emails| emails.values end end
empty!()
click to toggle source
# File lib/minbox/inbox.rb, line 48 def empty! with_lock do @emails = {} end end
open(subject:)
click to toggle source
# File lib/minbox/inbox.rb, line 40 def open(subject:) wait_until do emails.find do |email| subject.is_a?(String) ? email.subject == subject : email.subject.match?(subject) end end end
wait_until(seconds: 10, wait: 0.1) { |self| ... }
click to toggle source
# File lib/minbox/inbox.rb, line 25 def wait_until(seconds: 10, wait: 0.1) iterations = (seconds / wait).to_i iterations.times do result = yield(self) return result if result sleep wait end nil end
wait_until!(*args, &block)
click to toggle source
# File lib/minbox/inbox.rb, line 36 def wait_until!(*args, &block) raise "timeout: expired. #{args}" unless wait_until(*args, &block) end
Private Instance Methods
changed(modified, added, removed)
click to toggle source
# File lib/minbox/inbox.rb, line 62 def changed(modified, added, removed) with_lock do |emails| added.each do |file| mail = Mail.read(file) Minbox.logger.debug("Received: #{mail.subject}") emails[File.basename(file)] = mail end removed.each do |file| emails.delete(File.basename(file)) end end end
listener_for(dir)
click to toggle source
# File lib/minbox/inbox.rb, line 75 def listener_for(dir) ::Listen.to(File.expand_path(dir), only: /\.eml$/, &method(:changed)) end
start_listening(root_dir)
click to toggle source
# File lib/minbox/inbox.rb, line 79 def start_listening(root_dir) listener_for(root_dir).start end
with_lock() { |emails| ... }
click to toggle source
# File lib/minbox/inbox.rb, line 83 def with_lock @semaphore.synchronize do yield @emails if block_given? end end