Sha256: 458174a03c4d5203c068c53d01592911bda35fb9df5b46e667c589ae01e34b15
Contents?: true
Size: 1.52 KB
Versions: 2
Compression:
Stored size: 1.52 KB
Contents
require 'net/imap' require 'mail' module EmailBills class GmailChecker def initialize(box = '', user = ENV['GMAIL_USER'], pass = ENV['GMAIL_PASSWORD']) @user = user @pass = pass @mailbox = box end def login return if @logged_in @imap = Net::IMAP.new 'imap.gmail.com', 993, true @imap.login(@user, @pass) @logged_in = true end def get_since(date, box = @mailbox) login @imap.select(box) seq_ids = @imap.search(['SINCE', date.strftime('%d-%b-%Y')]) uids = @imap.fetch(seq_ids, 'UID').map { |item| item.attr['UID'] } get_from_server uids end def get_from_server(uids) uids.map do |uid| items = @imap.fetch(uid, 'RFC822') item = items.first.attr['RFC822'] if items && items.count > 0 extract_and_build uid, item if item end end def extract_and_build(uid, imap_message) mail = Mail.new imap_message Message.new uid: uid, body: get_body(mail), received_date: mail.date, subject: mail.subject, message_id: mail.message_id, raw: mail.encoded end def get_body(mail) GmailChecker.html_part(mail) || GmailChecker.plain_part(mail) end def self.plain_part(mail) mail.multipart? ? (mail.text_part ? mail.text_part.body.encoded : nil) : mail.body.encoded end def self.html_part(mail) mail.html_part ? mail.html_part.body.encoded : nil end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
email_bills-0.1.1 | lib/email_bills/gmail_checker.rb |
email_bills-0.1.0 | lib/email_bills/gmail_checker.rb |