Module: EmailAble

Extended by:
ActiveSupport::Concern
Includes:
App47Logger
Defined in:
lib/app/models/concerns/email_able.rb

Overview

Objects that emails can be sent too…

Class Method Summary collapse

Instance Method Summary collapse

Methods included from App47Logger

clean_params, #clean_params, delete_parameter_keys, #log_controller_error, log_debug, #log_debug, log_error, #log_error, log_exception, #log_message, log_message, #log_warn, log_warn, mask_parameter_keys, #update_flash_messages

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/app/models/concerns/email_able.rb', line 8

def self.included(base)
  base.class_eval do
    field :email, type: String
    field :email_bounced_at, type: Time
    field :email_bounce_reason, type: String
    field :unconfirmed_email, type: String
    field :email_enabled, type: Mongoid::Boolean, default: true
    #
    # Validations
    #
    validates :email, email_format: { strict: true, allow_blank: true }
    #
    # Callbacks
    #
    before_validation :downcase_email
  end
end

Instance Method Details

#bounced(reason) ⇒ Object

Set the email to a bounced status with the given reason



51
52
53
# File 'lib/app/models/concerns/email_able.rb', line 51

def bounced(reason)
  set email_bounced_at: Time.now.utc, email_bounce_reason: reason
end

#email_bounce_date(date = nil) ⇒ Object

Support legacy apps with old name



29
30
31
32
# File 'lib/app/models/concerns/email_able.rb', line 29

def email_bounce_date(date = nil)
  self.email_bounced_at = date if date.present?
  email_bounced_at
end

#email_bounced?Boolean

Has the email bounced?

Returns:

  • (Boolean)


44
45
46
# File 'lib/app/models/concerns/email_able.rb', line 44

def email_bounced?
  email_bounced_at.present?
end

#gravatar_url(size = '32', default = 'mm') ⇒ Object

Return the gravatar URL based on email address



76
77
78
79
80
81
82
83
# File 'lib/app/models/concerns/email_able.rb', line 76

def gravatar_url(size = '32', default = 'mm')
  self.email ||= '[email protected]'
  if SystemConfiguration.fips_mode?
    "https://www.gravatar.com/avatar/#{Digest::SHA2.hexdigest(email)}?s=#{size}&d=#{default}"
  else
    "https://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email)}?s=#{size}&d=#{default}"
  end
end

#reset_bounce_status(current_member = nil) ⇒ Object

Reset the bounced email



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/app/models/concerns/email_able.rb', line 58

def reset_bounce_status(current_member = nil)
  return unless SystemConfiguration.mail_gun_configured? && email_bounced?

  reset_url = "https://api.mailgun.net/v3/#{SystemConfiguration.smtp_domain}/bounces/#{CGI.escape(email)}"
  RestClient.delete(reset_url, user: 'api', password: SystemConfiguration.mailgun_api_key)
rescue RestClient::Exception => error
  log_error "Unable to reset email bounce status: #{inspect}", error
ensure
  if current_member.present?
    update_and_log current_member, email_bounced_at: nil, email_bounce_reason: nil
  else
    set email_bounced_at: nil, email_bounce_reason: nil
  end
end

#valid_email?Boolean

Is this a valid email to send to?

Returns:

  • (Boolean)


37
38
39
# File 'lib/app/models/concerns/email_able.rb', line 37

def valid_email?
  email_enabled? && !email_bounced?
end