Class: Parse::Email
- Inherits:
-
Object
- Object
- Parse::Email
- 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.
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
-
#address ⇒ String
readonly
The normalized email address (or nil if invalid input).
-
#raw ⇒ String
readonly
The raw input value.
Class Method Summary collapse
-
.typecast(value) ⇒ Parse::Email?
private
Type casting support for Parse properties.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check equality with another email.
-
#as_json(*args) ⇒ String?
The email address for JSON serialization.
-
#blank? ⇒ Boolean
True if the email is blank/nil.
-
#disposable? ⇒ Boolean
Check if this email is from a disposable email service.
-
#domain ⇒ String?
Get the domain part of the email (after @).
-
#initialize(value) ⇒ Email
constructor
Creates a new Email instance.
-
#local ⇒ String?
Get the local part of the email (before @).
-
#masked ⇒ String?
Format the email with the local part obscured for privacy.
-
#normalize(value) ⇒ String?
Normalize an email address.
-
#present? ⇒ Boolean
True if the email is present.
-
#tld ⇒ String?
Get the top-level domain (TLD) of the email.
-
#to_s ⇒ String?
The normalized email address.
-
#valid? ⇒ Boolean
Check if this email address is valid.
Constructor Details
#new(address) ⇒ Parse::Email #new(email) ⇒ Parse::Email
Creates a new Email instance.
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
#address ⇒ String (readonly)
Returns the normalized email address (or nil if invalid input).
45 46 47 |
# File 'lib/parse/model/email.rb', line 45 def address @address end |
#raw ⇒ String (readonly)
Returns 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.
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.
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.
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.
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.
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 |
#domain ⇒ String?
Get the domain part of the email (after @).
125 126 127 128 |
# File 'lib/parse/model/email.rb', line 125 def domain return nil unless valid? @address.split("@").last end |
#local ⇒ String?
Get the local part of the email (before @).
114 115 116 117 |
# File 'lib/parse/model/email.rb', line 114 def local return nil unless valid? @address.split("@").first end |
#masked ⇒ String?
Format the email with the local part obscured for privacy.
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
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.
197 198 199 |
# File 'lib/parse/model/email.rb', line 197 def present? !blank? end |
#tld ⇒ String?
Get the top-level domain (TLD) of the email.
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_s ⇒ String?
Returns 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.
103 104 105 106 |
# File 'lib/parse/model/email.rb', line 103 def valid? return false if @address.blank? EMAIL_REGEX.match?(@address) end |