Class: Email::Verification::Gmail

Inherits:
Base
  • Object
show all
Defined in:
lib/email/verification/gmail.rb

Instance Attribute Summary

Attributes inherited from Base

#configuration

Instance Method Summary collapse

Methods inherited from Base

#email_body, #force_utf8, #initialize, #log, #set_retriever, #settings_provided?

Constructor Details

This class inherits a constructor from Email::Verification::Base

Instance Method Details

#normal_retrieve_verification_code(email:, password:, mark_as_read: true, count: :all, mailboxes: %w(Inbox),, settings: {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/email/verification/gmail.rb', line 13

def normal_retrieve_verification_code(email:, password:, mark_as_read: true, count: :all, mailboxes: %w(Inbox), settings: {})
  emails    =   []
  result    =   nil
    
  begin
    args    =   settings_provided?(settings) ? {from: settings[:address]} : {}
    
    ::Gmail.connect(email, password) do |gmail|
      gmail.inbox.emails(args).each do |email|
        log("Email - from: #{email.from.first.name}, subject: #{email.subject}")
        
        if settings_provided?(settings)
          matching_name       =   settings[:from].to_s.empty? || (!settings[:from].to_s.empty? && email.from.first.name == settings[:from])
          matching_subject    =   settings[:subject].nil?     || (!settings[:subject].nil? && !(email.subject =~ settings[:subject]).nil?)
          
          emails  <<  email_body(email) if matching_name && matching_subject
        else
          emails  <<  email_body(email)
        end
        
        email.read! if mark_as_read
      end
    end
  
  rescue Net::IMAP::BadResponseError => e
    raise ::Email::Verification::Errors::InvalidCredentialsError.new("You need to enable logins for less secure apps in Gmail for #{email}!")
  end
  
  if settings_provided?(settings)
    message     =   emails.last&.to_s
    result      =   message&.match(settings[:regex])&.[](:match)
  else
    result      =   emails
  end
  
  return result
end

#retrieve_verification_code(email:, password:, mark_as_read: true, count: :all, mailboxes: %w(Inbox),, settings: {}, proxy: nil) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/email/verification/gmail.rb', line 5

def retrieve_verification_code(email:, password:, mark_as_read: true, count: :all, mailboxes: %w(Inbox), settings: {}, proxy: nil)
  if proxy && !proxy.empty? && !proxy[:host].to_s.empty? && !proxy[:port].to_s.empty?
    return super(email: email, password: password, host: "imap.gmail.com", port: 993, enable_ssl: true, mailboxes: mailboxes, count: count, settings: settings, proxy: proxy)
  else
    return normal_retrieve_verification_code(email: email, password: password, mark_as_read: mark_as_read, count: count, mailboxes: mailboxes, settings: settings)
  end
end