Class: EmailIntegrator::POP3

Inherits:
Object
  • Object
show all
Defined in:
lib/email_integrator/email_integrator.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ POP3

Returns a new instance of POP3.



11
12
13
14
15
16
17
# File 'lib/email_integrator/email_integrator.rb', line 11

def initialize(connection)
  @connection = connection
  [:server, :username, :password].each do |param|
    raise ArgumentError, "missing argument #{param.to_s}" unless @connection.has_key? param
  end

end

Instance Method Details

#clean_backtrace(exception) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/email_integrator/email_integrator.rb', line 65

def clean_backtrace(exception)
  if backtrace = exception.backtrace
    if defined?(RAILS_ROOT)
      backtrace.map { |line| line.sub(RAILS_ROOT, '')+"\n" }
    else
      backtrace
    end
  end
end

#error_messages(errors) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/email_integrator/email_integrator.rb', line 57

def error_messages(errors)
  message = ""
  errors.each do |e|
    message<< "#{e.class} #{e.message}\n#{clean_backtrace(e)}\n"
  end
  message
end

#is_production?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/email_integrator/email_integrator.rb', line 25

def is_production?
  ENV["RAILS_ENV"] == "production" ? true : false
end

#process(pattern) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/email_integrator/email_integrator.rb', line 29

def process(pattern)
  #

  # Connect to email server and look for emails

  #

  errors=[]
  conn = Net::POP3.new(server)
  conn.start(username,
             password) do |pop|
    pop.mails.each do |msg|
      begin
        xml = msg.pop[pattern]
        if xml
          processed = yield(xml)
          msg.delete if processed && is_production?
        end
      rescue Exception => e
        errors << e
      end
    end
  end
  if errors.size>0
    raise(RuntimeError,
        "Processing email integration generated exception(#{errors.size}).\n"+
        "Until it is resolved it the message will be stuck in the email inbox: (#{server}, #{username})."+
        "Error Messages: " + error_messages(errors))
  end        
end