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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/app/models/concerns/email_able.rb', line 14

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



57
58
59
# File 'lib/app/models/concerns/email_able.rb', line 57

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



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

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)


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

def email_bounced?
  email_bounced_at.present?
end

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

Return the gravatar URL based on email address



82
83
84
85
86
87
88
89
# File 'lib/app/models/concerns/email_able.rb', line 82

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/app/models/concerns/email_able.rb', line 64

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)


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

def valid_email?
  email_enabled? && !email_bounced?
end