Class: Artichoke::Poller
- Inherits:
-
Object
- Object
- Artichoke::Poller
- Defined in:
- lib/artichoke/poller.rb
Instance Method Summary collapse
- #client ⇒ Object
-
#find(options = {}) ⇒ Object
poller.find(“Sample Email”, timeout:75, content:[“specific positioning”, “footer”], attachments:[“picture.jpg”, “spreadsheet.csv”], skip_error: false).
-
#initialize ⇒ Poller
constructor
A new instance of Poller.
Constructor Details
#initialize ⇒ Poller
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
#client ⇒ Object
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)
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(={}) raise ArgumentError.new("Email Subject required") unless [:message_subject] begin Timeout::timeout([:timeout]|| 75) do while true do client do gm_string = "newer:#{@gmail_start_time.to_i} subject:"+[:message_subject]+" " if [:attachments] [:attachments].each{|| gm_string += +" "} gm_string+= " has:attachment" end @gmail.inbox.emails(:gm => gm_string).each do |email| = email. if (.date.to_i >= @gmail_start_time.to_i) && (.subject == [:message_subject]) body = (.text_part.try(:decoded) || .html_part.try(:decoded) || .body.to_s.force_encoding('utf-8')) return Message.new() if ([:content]|| []).all?{|c| body =~ /#{Regexp.escape(c)}/} end end end end end rescue Timeout::Error => e raise "No email was found with subject: #{[:message_subject]} >= #{@gmail_start_time}, content(s): #{[:content]||'N/A'}, attachment(s) #{[:attachments]||'N/A'}" unless [:skip_error] end end |