Class: Artichoke::Poller

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

Instance Method Summary collapse

Constructor Details

#initializePoller

Returns a new instance of Poller.



4
5
6
# File 'lib/artichoke/poller.rb', line 4

def initialize
  @gmail_start_time = DateTime.now
end

Instance Method Details

#clientObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/artichoke/poller.rb', line 9

def client
  username = Artichoke::Connection.client_username
  password = Artichoke::Connection.client_password
  begin
    @gmail = Gmail.new(username, password)
    @gmail.peek = true
    yield
  #Retry for intermittent Gmail issues
  rescue Net::IMAP::ByeResponseError, Net::IMAP::NoResponseError
    retry
  ensure      
    @gmail.disconnect
  end
end

#find(options = {}) ⇒ Object

poller.find(“Sample Email”, timeout:75, content:[“specific positioning”, “footer”], attachments:[“picture.jpg”, “spreadsheet.csv”], skip_error: false)

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/artichoke/poller.rb', line 26

def find(options={})
  raise ArgumentError.new("Email Subject required") unless options[:message_subject]
  begin
    Timeout::timeout(options[:timeout]|| 75) do
      while true do
        client do
          gm_string = "newer:#{@gmail_start_time.to_i} subject:"+options[:message_subject]+" "
          if options[:attachments]
            options[:attachments].each{|attachment| gm_string += attachment+" "} 
            gm_string+= " has:attachment"
          end
          @gmail.inbox.emails(:gm => gm_string).each do |email|
            message = email.message
            if (message.date.to_i >= @gmail_start_time.to_i) && (message.subject == options[:message_subject])
              body = (message.text_part.try(:decoded) || message.html_part.try(:decoded) || message.body.to_s.force_encoding('utf-8'))
              return Message.new(message) if (options[:content]|| []).all?{|c| body =~ /#{Regexp.escape(c)}/}
            end
          end
        end
      end
    end
  rescue Timeout::Error => e
    raise "No email was found with subject: #{options[:message_subject]} >= #{@gmail_start_time}, content(s): #{options[:content]||'N/A'}, attachment(s) #{options[:attachments]||'N/A'}"  unless options[:skip_error]
  end
end