class Minfraud::Components::Email

Email corresponds to the email object of a minFraud request.

@see dev.maxmind.com/minfraud/#Email_(/email)

Constants

TYPO_DOMAINS

Attributes

address[RW]

This field must be either be a valid email address or an MD5 of the lowercased email used in the transaction. Important: if using the MD5 hash, please be sure to convert the email address to lowercase before calculating its MD5 hash. Instead of converting an address to an MD5 hash yourself, please use the hash_address attribute in this class.

@return [String, nil]

domain[RW]

The domain of the email address used in the transaction.

@return [String, nil]

hash_address[RW]

By default, the address will be sent in plain text. If this is set true, the address will instead be sent as an MD5 hash.

@return [Boolean, nil]

Public Class Methods

new(params = {}) click to toggle source

@param params [Hash] Hash of parameters. Each key/value should

correspond to one of the available attributes.
# File lib/minfraud/components/email.rb, line 36
def initialize(params = {})
  @address      = params[:address]
  @domain       = params[:domain]
  @hash_address = params[:hash_address]

  validate
end

Public Instance Methods

to_json(*_args) click to toggle source

A JSON representation of Minfraud::Components::Email.

@return [Hash]

Calls superclass method Minfraud::Components::Base#to_json
# File lib/minfraud/components/email.rb, line 47
def to_json(*_args)
  json = super

  if json['address'] && !json['domain']
    _, domain = address.split('@', 2)
    if domain
      domain         = clean_domain(domain)
      json['domain'] = domain if domain
    end
  end

  if json.delete('hash_address') && json['address']
    hash = hash_email_address(json['address'])

    # We could consider clearing the key if !hash.
    json['address'] = hash if hash
  end

  json
end

Private Instance Methods

clean_domain(domain) click to toggle source
# File lib/minfraud/components/email.rb, line 116
def clean_domain(domain)
  domain = domain.strip

  # We could use delete_suffix!, but that is in Ruby 2.5+ only.
  domain.sub!(/\.\z/, '')

  domain = SimpleIDN.to_ascii(domain)

  if TYPO_DOMAINS.key?(domain)
    domain = TYPO_DOMAINS[domain]
  end

  domain
end
clean_email_address(address) click to toggle source
# File lib/minfraud/components/email.rb, line 84
def clean_email_address(address)
  address = address.strip
  address.downcase!

  local_part, domain = address.split('@', 2)
  return nil if !local_part || !domain

  domain = clean_domain(domain)

  if domain == 'yahoo.com'
    local_part.sub!(/\A([^-]+)-.*\z/, '\1')
  else
    local_part.sub!(/\A([^+]+)\+.*\z/, '\1')
  end

  "#{local_part}@#{domain}"
end
hash_email_address(address) click to toggle source
# File lib/minfraud/components/email.rb, line 77
def hash_email_address(address)
  address = clean_email_address(address)
  return nil if !address

  Digest::MD5.hexdigest(address)
end
validate() click to toggle source
# File lib/minfraud/components/email.rb, line 70
def validate
  return if !Minfraud.enable_validation

  validate_email('email', @address)
  validate_string('domain', 255, @domain)
end