Skip to content

Commit

Permalink
Add a method for bulk sanitization of emails
Browse files Browse the repository at this point in the history
This convenience method just makes it easy to clean up lots of emails
without having messy logic in controllers or other POROs.
  • Loading branch information
QuotableWater7 committed Jun 28, 2016
1 parent 44365bd commit 16aca05
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Expand Up @@ -7,6 +7,9 @@ TrailingComma:
AccessModifierIndentation:
EnforcedStyle: outdent

MethodLength:
Max: 15

CollectionMethods:
PreferredMethods:
collect: 'map'
Expand Down
20 changes: 20 additions & 0 deletions lib/email_repair/mechanic.rb
Expand Up @@ -11,6 +11,26 @@ def repairs
]
end

def repair_all(emails)
sanitized_emails = []
invalid_emails = []

emails.each do |email|
repaired_email = repair(email)

if repaired_email
sanitized_emails << repaired_email
else
invalid_emails << email
end
end

OpenStruct.new(
sanitized_emails: sanitized_emails,
invalid_emails: invalid_emails,
)
end

def repair(email)
return unless email

Expand Down
15 changes: 15 additions & 0 deletions spec/lib/email_repair/mechanic_spec.rb
@@ -1,6 +1,21 @@
require 'spec_helper'

module EmailRepair
describe Mechanic, '#repair_all' do

it 'sanitizes an array of emails and returns bulk results' do
mechanic = Mechanic.new
salvageable_emails = %w(One@@two.com three@four.com)
sanitized_emails = %w(one@two.com three@four.com)
bad_emails = %w(bleep@blop plooooooop)

result = mechanic.repair_all(salvageable_emails + bad_emails)
expect(result.sanitized_emails).to match_array sanitized_emails
expect(result.invalid_emails).to match_array bad_emails
end

end

describe Mechanic, '#repair' do
let(:mechanic) { Mechanic.new }

Expand Down

0 comments on commit 16aca05

Please sign in to comment.