Class: Parse::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/model/email.rb

Overview

This class provides email validation for Parse properties. It wraps a string value and provides validation according to RFC 5322.

When declaring a property of type :email, the framework will automatically add a validation to ensure the email is either nil or a valid email address format.

Examples:

class Contact < Parse::Object
  property :email, :email
  property :work_email, :email, required: true
end

contact = Contact.new
contact.email = "user@example.com"
contact.email.valid?     # => true
contact.email.local      # => "user"
contact.email.domain     # => "example.com"

contact.email = "invalid"
contact.email.valid?     # => false

Version:

  • 3.0.0

Constant Summary collapse

EMAIL_REGEX =

RFC 5322 compliant email regex (simplified but robust version) This regex validates most common email formats while avoiding catastrophic backtracking. For stricter validation, consider using a dedicated email validation library.

/\A[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\z/
DISPOSABLE_DOMAINS =

Common disposable email domains (for optional filtering)

%w[
  mailinator.com guerrillamail.com tempmail.com throwaway.email
  10minutemail.com fakeinbox.com trashmail.com
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(address) ⇒ Parse::Email #new(email) ⇒ Parse::Email

Creates a new Email instance.

Examples:

Parse::Email.new("user@example.com")
Parse::Email.new("  USER@EXAMPLE.COM  ")  # Will normalize

Overloads:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/parse/model/email.rb', line 59

def initialize(value)
  @raw = nil
  @address = nil

  if value.is_a?(String)
    @raw = value
    @address = normalize(value)
  elsif value.is_a?(Parse::Email)
    @raw = value.raw
    @address = value.address
  elsif value.respond_to?(:to_s) && !value.nil?
    @raw = value.to_s
    @address = normalize(@raw)
  end
end

Instance Attribute Details

#addressString (readonly)

Returns the normalized email address (or nil if invalid input).

Returns:

  • (String)

    the normalized email address (or nil if invalid input)



45
46
47
# File 'lib/parse/model/email.rb', line 45

def address
  @address
end

#rawString (readonly)

Returns the raw input value.

Returns:

  • (String)

    the raw input value



42
43
44
# File 'lib/parse/model/email.rb', line 42

def raw
  @raw
end

Class Method Details

.typecast(value) ⇒ Parse::Email?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Type casting support for Parse properties. This allows the property system to convert values to Email instances.

Parameters:

  • value (Object)

    the value to typecast

Returns:



207
208
209
210
211
# File 'lib/parse/model/email.rb', line 207

def self.typecast(value)
  return nil if value.nil?
  return value if value.is_a?(Parse::Email)
  Parse::Email.new(value)
end

Instance Method Details

#==(other) ⇒ Boolean

Check equality with another email.

Parameters:

Returns:

  • (Boolean)

    true if the emails are equal



181
182
183
184
185
186
187
188
189
# File 'lib/parse/model/email.rb', line 181

def ==(other)
  if other.is_a?(Parse::Email)
    @address == other.address
  elsif other.is_a?(String)
    @address == normalize(other)
  else
    false
  end
end

#as_json(*args) ⇒ String?

Returns the email address for JSON serialization.

Returns:

  • (String, nil)

    the email address for JSON serialization



92
93
94
# File 'lib/parse/model/email.rb', line 92

def as_json(*args)
  @address
end

#blank?Boolean

Returns true if the email is blank/nil.

Returns:

  • (Boolean)

    true if the email is blank/nil



192
193
194
# File 'lib/parse/model/email.rb', line 192

def blank?
  @address.blank?
end

#disposable?Boolean

Check if this email is from a disposable email service. Note: This is a basic check against a small list. For production use, consider using a dedicated disposable email detection service.

Examples:

Parse::Email.new("user@mailinator.com").disposable?  # => true
Parse::Email.new("user@gmail.com").disposable?       # => false

Returns:

  • (Boolean)

    true if the domain is a known disposable email provider



152
153
154
155
156
# File 'lib/parse/model/email.rb', line 152

def disposable?
  d = domain
  return false unless d
  DISPOSABLE_DOMAINS.include?(d)
end

#domainString?

Get the domain part of the email (after @).

Examples:

Parse::Email.new("user@example.com").domain  # => "example.com"

Returns:

  • (String, nil)

    the domain or nil if invalid



125
126
127
128
# File 'lib/parse/model/email.rb', line 125

def domain
  return nil unless valid?
  @address.split("@").last
end

#localString?

Get the local part of the email (before @).

Examples:

Parse::Email.new("user@example.com").local  # => "user"

Returns:

  • (String, nil)

    the local part or nil if invalid



114
115
116
117
# File 'lib/parse/model/email.rb', line 114

def local
  return nil unless valid?
  @address.split("@").first
end

#maskedString?

Format the email with the local part obscured for privacy.

Examples:

Parse::Email.new("username@example.com").masked  # => "u***e@example.com"

Returns:

  • (String, nil)

    the masked email or nil if invalid



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/parse/model/email.rb', line 164

def masked
  return nil unless valid?
  l = local
  d = domain
  return nil unless l && d

  if l.length <= 2
    "#{l[0]}*@#{d}"
  else
    "#{l[0]}#{"*" * [l.length - 2, 3].min}#{l[-1]}@#{d}"
  end
end

#normalize(value) ⇒ String?

Normalize an email address.

  • Strips whitespace

  • Converts to lowercase

Parameters:

  • value (String)

    the email string

Returns:

  • (String, nil)

    the normalized email or nil if blank



81
82
83
84
# File 'lib/parse/model/email.rb', line 81

def normalize(value)
  return nil if value.blank?
  value.to_s.strip.downcase
end

#present?Boolean

Returns true if the email is present.

Returns:

  • (Boolean)

    true if the email is present



197
198
199
# File 'lib/parse/model/email.rb', line 197

def present?
  !blank?
end

#tldString?

Get the top-level domain (TLD) of the email.

Examples:

Parse::Email.new("user@example.com").tld  # => "com"
Parse::Email.new("user@example.co.uk").tld  # => "uk"

Returns:

  • (String, nil)

    the TLD or nil if invalid



137
138
139
140
141
# File 'lib/parse/model/email.rb', line 137

def tld
  d = domain
  return nil unless d
  d.split(".").last
end

#to_sString?

Returns the normalized email address.

Returns:

  • (String, nil)

    the normalized email address



87
88
89
# File 'lib/parse/model/email.rb', line 87

def to_s
  @address
end

#valid?Boolean

Check if this email address is valid.

Examples:

Parse::Email.new("user@example.com").valid?  # => true
Parse::Email.new("invalid").valid?           # => false

Returns:

  • (Boolean)

    true if the email is valid format



103
104
105
106
# File 'lib/parse/model/email.rb', line 103

def valid?
  return false if @address.blank?
  EMAIL_REGEX.match?(@address)
end