Class: Trueform::Validation

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_meanObject (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

#emailObject (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

Returns:

  • (Boolean)


66
67
68
# File 'lib/trueform/validation.rb', line 66

def deliverable?
  @deliverable
end

#disposable?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/trueform/validation.rb', line 58

def disposable?
  @disposable
end

#freemail?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/trueform/validation.rb', line 54

def freemail?
  @freemail
end

#hashObject



87
88
89
# File 'lib/trueform/validation.rb', line 87

def hash
  to_h.hash
end

#mx_records?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/trueform/validation.rb', line 62

def mx_records?
  @mx_records
end

#to_hObject



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

Returns:

  • (Boolean)


50
51
52
# File 'lib/trueform/validation.rb', line 50

def valid_format?
  @valid_format
end