Class: Email::Verification::Base
- Inherits:
-
Object
- Object
- Email::Verification::Base
- Defined in:
- lib/email/verification/base.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
Instance Method Summary collapse
- #email_body(email) ⇒ Object
- #force_utf8(string) ⇒ Object
-
#initialize(configuration: ::Email::Verification.configuration) ⇒ Base
constructor
A new instance of Base.
- #log(message) ⇒ Object
- #retrieve_verification_code(email:, password:, host:, port: 993, enable_ssl: true, count: :all, mailboxes: %w(Inbox),, settings: {}, proxy: nil) ⇒ Object
- #set_retriever(email:, password:, host:, port: 993, enable_ssl: true, proxy: nil) ⇒ Object
- #settings_provided?(settings = {}) ⇒ Boolean
Constructor Details
#initialize(configuration: ::Email::Verification.configuration) ⇒ Base
Returns a new instance of Base.
6 7 8 |
# File 'lib/email/verification/base.rb', line 6 def initialize(configuration: ::Email::Verification.configuration) self.configuration = configuration end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
4 5 6 |
# File 'lib/email/verification/base.rb', line 4 def configuration @configuration end |
Instance Method Details
#email_body(email) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/email/verification/base.rb', line 73 def email_body(email) body = (email.html_part || email.text_part || email)&.body begin body = body&.decoded rescue Mail::UnknownEncodingType body = body&.encoded end body = force_utf8(body) unless body.to_s.empty? return body end |
#force_utf8(string) ⇒ Object
87 88 89 |
# File 'lib/email/verification/base.rb', line 87 def force_utf8(string) string&.encode("UTF-8", invalid: :replace, undef: :replace, replace: '')&.force_encoding('UTF-8') end |
#log(message) ⇒ Object
91 92 93 |
# File 'lib/email/verification/base.rb', line 91 def log() puts "[Email::Verification] - #{Time.now.to_s}: #{}" if self.configuration.verbose end |
#retrieve_verification_code(email:, password:, host:, port: 993, enable_ssl: true, count: :all, mailboxes: %w(Inbox),, settings: {}, proxy: nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/email/verification/base.rb', line 33 def retrieve_verification_code(email:, password:, host:, port: 993, enable_ssl: true, count: :all, mailboxes: %w(Inbox), settings: {}, proxy: nil) emails = [] result = nil begin set_retriever(email: email, password: password, host: host, port: port, enable_ssl: enable_ssl, proxy: proxy) mailboxes.each do |mailbox| Mail.find(mailbox: mailbox, order: :desc, count: count)&.each do |email| log("From: #{email.from&.first&.strip}. Subject: #{email.subject}") if settings_provided?(settings) matching_address = email.from&.first&.strip == settings[:address] matching_subject = settings[:subject].nil? || (!settings[:subject].nil? && !(email.subject =~ settings[:subject]).nil?) emails << email_body(email) if matching_address && matching_subject else emails << email_body(email) end end end rescue Net::IMAP::NoResponseError => e raise ::Email::Verification::Errors::InvalidCredentialsError.new("Please check account/password settings for email #{email}!") end if settings_provided?(settings) = emails.first&.to_s result = &.match(settings[:regex])&.[](:match) else result = emails end return result end |
#set_retriever(email:, password:, host:, port: 993, enable_ssl: true, proxy: nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/email/verification/base.rb', line 10 def set_retriever(email:, password:, host:, port: 993, enable_ssl: true, proxy: nil) retriever = :imap = { address: host, port: port, user_name: email, password: password, enable_ssl: enable_ssl } if proxy && !proxy.empty? && !proxy[:host].to_s.empty? && !proxy[:port].to_s.empty? retriever = ::Net::IMAP::Proxy::RetrieverMethod proxy[:host] = "http://#{proxy[:host]}" unless proxy[:host] =~ /^http(s)?:\/\//i = .merge(proxy_address: proxy[:host], proxy_port: proxy[:port]) log("Using proxy #{proxy[:host]}:#{proxy[:port]} to fetch messages from #{[:address]}:#{[:port]}") end Mail.defaults do retriever_method retriever, end end |
#settings_provided?(settings = {}) ⇒ Boolean
69 70 71 |
# File 'lib/email/verification/base.rb', line 69 def settings_provided?(settings = {}) settings && !settings.empty? end |