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 1ce094a commit 04dc01d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/email_repair/mechanic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ def repairs
]
end

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

emails.each do |email|
repaired_email = repair(email)
repaired_email ? valid_emails << repaired_email : invalid_emails << email
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
Original file line number Diff line number Diff line change
@@ -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.valid_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 04dc01d

Please sign in to comment.