Class: Trueform::Validation
- Inherits:
-
Object
- Object
- Trueform::Validation
- Defined in:
- lib/trueform/validation.rb
Constant Summary collapse
- BOOLEAN_FIELDS =
%w[ is_valid_format is_freemail is_disposable has_mx_records is_deliverable ].freeze
Instance Attribute Summary collapse
-
#did_you_mean ⇒ Object
readonly
Returns the value of attribute did_you_mean.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #deliverable? ⇒ Boolean
- #disposable? ⇒ Boolean
- #freemail? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(email:, valid_format:, freemail:, disposable:, mx_records:, did_you_mean:, deliverable:) ⇒ Validation
constructor
A new instance of Validation.
- #mx_records? ⇒ Boolean
- #to_h ⇒ Object
- #valid_format? ⇒ Boolean
Constructor Details
#initialize(email:, valid_format:, freemail:, disposable:, mx_records:, did_you_mean:, deliverable:) ⇒ Validation
Returns a new instance of Validation.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/trueform/validation.rb', line 39 def initialize(email:, valid_format:, freemail:, disposable:, mx_records:, did_you_mean:, deliverable:) @email = email.dup.freeze @valid_format = valid_format @freemail = freemail @disposable = disposable @mx_records = mx_records @did_you_mean = did_you_mean&.dup&.freeze @deliverable = deliverable freeze end |
Instance Attribute Details
#did_you_mean ⇒ Object (readonly)
Returns the value of attribute did_you_mean.
13 14 15 |
# File 'lib/trueform/validation.rb', line 13 def did_you_mean @did_you_mean end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
13 14 15 |
# File 'lib/trueform/validation.rb', line 13 def email @email end |
Class Method Details
.from_api_response(value) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/trueform/validation.rb', line 15 def self.from_api_response(value) valid = value.is_a?(Hash) && value["email"].is_a?(String) && BOOLEAN_FIELDS.all? { |field| value[field] == true || value[field] == false } && (value["did_you_mean"].nil? || value["did_you_mean"].is_a?(String)) unless valid raise APIError.new( "The API returned an invalid validation response.", code: "invalid_response" ) end new( email: value["email"], valid_format: value["is_valid_format"], freemail: value["is_freemail"], disposable: value["is_disposable"], mx_records: value["has_mx_records"], did_you_mean: value["did_you_mean"], deliverable: value["is_deliverable"] ) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
82 83 84 |
# File 'lib/trueform/validation.rb', line 82 def ==(other) other.is_a?(Validation) && to_h == other.to_h end |
#deliverable? ⇒ Boolean
66 67 68 |
# File 'lib/trueform/validation.rb', line 66 def deliverable? @deliverable end |
#disposable? ⇒ Boolean
58 59 60 |
# File 'lib/trueform/validation.rb', line 58 def disposable? @disposable end |
#freemail? ⇒ Boolean
54 55 56 |
# File 'lib/trueform/validation.rb', line 54 def freemail? @freemail end |
#hash ⇒ Object
87 88 89 |
# File 'lib/trueform/validation.rb', line 87 def hash to_h.hash end |
#mx_records? ⇒ Boolean
62 63 64 |
# File 'lib/trueform/validation.rb', line 62 def mx_records? @mx_records end |
#to_h ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/trueform/validation.rb', line 70 def to_h { email: @email, is_valid_format: @valid_format, is_freemail: @freemail, is_disposable: @disposable, has_mx_records: @mx_records, did_you_mean: @did_you_mean, is_deliverable: @deliverable } end |
#valid_format? ⇒ Boolean
50 51 52 |
# File 'lib/trueform/validation.rb', line 50 def valid_format? @valid_format end |