Skip to content

Commit

Permalink
DRY code
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagoyoussef committed May 11, 2022
1 parent 2803c73 commit 7a2256a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
11 changes: 5 additions & 6 deletions app/interactions/actions/contact_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ def maybe_change_email
end

def maybe_filtering_old_failed_records
if contact.validation_events.count > 1
contact.validation_events.order!(created_at: :asc)
while contact.validation_events.count >= 1
contact.validation_events.first.destroy
end
end
validation_events = contact.validation_events
return unless validation_events.count > 1

validation_events.order!(created_at: :asc)
validation_events.first.destroy while validation_events.count >= 1
end

def maybe_remove_address
Expand Down
35 changes: 14 additions & 21 deletions app/interactions/actions/email_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,27 @@ def calculate_check_level
Rails.env.test? && check_level == 'smtp' ? :mx : check_level.to_sym
end

def destroy_old_validations(validation_events, minimum_size)
return unless validation_events.count > minimum_size

validation_events.order!(created_at: :asc)
validation_events.first.destroy while validation_events.count > minimum_size
end

def filtering_old_failed_records(result)
if @check_level == "mx" && !result.success && validation_eventable.validation_events.count > 3
validation_eventable.validation_events.order!(created_at: :asc)
while validation_eventable.validation_events.count > 3
validation_eventable.validation_events.first.destroy
end
end
events = validation_eventable.validation_events

if @check_level == "mx" && result.success && validation_eventable.validation_events.count > 1
validation_eventable.validation_events.order!(created_at: :asc)
while validation_eventable.validation_events.count > 1
validation_eventable.validation_events.first.destroy
end
end
destroy_old_validations(events, 3) if @check_level == 'mx' && !result.success

if @check_level == "smtp" && validation_eventable.validation_events.count > 1
validation_eventable.validation_events.order!(created_at: :asc)
while validation_eventable.validation_events.count > 1
validation_eventable.validation_events.first.destroy
end
end
destroy_old_validations(events, 1) if @check_level == 'mx' && result.success

destroy_old_validations(events, 1) if @check_level == 'smtp'
end

def save_result(result)
contacts = Contact.where(email: email)

if !result.success && @check_level == "mx"
if !result.success && @check_level == 'mx'
result_validation = Actions::AAndAaaaEmailValidation.call(email: @email, value: 'A')
output_a_and_aaaa_validation_results(email: @email,
result: result_validation,
Expand Down Expand Up @@ -96,8 +90,7 @@ def check_for_records_value(domain:, value:)
when 'AAAA'
ress = dns.getresources domain, Resolv::DNS::Resource::IN::AAAA
end

result = ress.map { |r| r.address }
result = ress.map(&:address)
end

result
Expand Down

0 comments on commit 7a2256a

Please sign in to comment.