gmail_client.rb
def fetch_messages(gmail, label, criteria, save_attachments)
    mails = []
    gmail.mailbox(label).search(criteria).each do |email|
      subject = "#{Mail::Encodings.value_decode(email.subject)}"
      puts "----------"
      puts "[#{email.uid}] #{subject}"
      from = sender(email.from)
      to = recipients(email.to)
      attachments = attachments(email)
      body = extract_body(email)

      # TODO: saving attachments should be configurable
      if save_attachments && email.message.attachments.any?
        puts "Attachments:"
        email.message.attachments.each do |attachment|
          save_attachment(email, attachment)
        end
      end

      mails << { :uid => email.uid,
                 :date => Time.parse(email.date),
                 :subject => subject,
                 :from => from,
                 :to => to,
                 :body => body,
                 :attachments => attachments }
    end
    mails
  end
# returns an array with all emails for specified label and one search criteria
returns an array with all emails for specified label and one search criteria
returns an array with all emails for specified label and one search criteria
def fetch_messages(gmail, label, criteria, save_attachments)
    mails = []
    gmail.mailbox(label).search(criteria).each do |email|
      subject = "#{Mail::Encodings.value_decode(email.subject)}"
      puts "----------"
      puts "[#{email.uid}] #{subject}"
      from = sender(email.from)
      to = recipients(email.to)
      attachments = attachments(email)
      body = extract_body(email)
      if save_attachments && email.message.attachments.any?
        puts "Attachments:"
        email.message.attachments.each do |attachment|
          save_attachment(email, attachment)
        end
      end
      mails << { :uid => email.uid,
                 :date => Time.parse(email.date),
                 :subject => subject,
                 :from => from,
                 :to => to,
                 :body => body,
                 :attachments => attachments }
    end
    mails
  end
def fetch_messages ( gmail , label , criteria , save_attachments ) mails = [ ] gmail . mailbox ( label ) . search ( criteria ) . each do | email | subject = "#{Mail::Encodings.value_decode(email.subject)}" puts "----------" puts "[#{email.uid}] #{subject}" from = sender ( email . from ) to = recipients ( email . to ) attachments = attachments ( email ) body = extract_body ( email ) if save_attachments && email . message . attachments . any? puts "Attachments:" email . message . attachments . each do | attachment | save_attachment ( email , attachment ) end end mails << { :uid => email . uid , :date => Time . parse ( email . date ) , :subject => subject , :from => from , :to => to , :body => body , :attachments => attachments } end mails end
returns an array with all emails for specified label and one search criteria
returns an array with all emails for specified label and one search criteria
# TODO: saving attachments should be configurable
gmail
label
criteria
save_attachments