Last active
July 28, 2021 17:55
-
-
Save cmoad/725cd9cabd7ef02f49d86f16228c1bd9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "levenshtein" | |
class Api::DuplicateController | |
include HTTParty | |
def show | |
base_url = "https://api.salesloft.com/v2/people.json?" + | |
"?per_page=50&include_paging_counts=true" | |
auth = "Bearer #{ENV['Authtoken']}" | |
headers = { | |
Authorization: auth | |
} | |
page = 1 | |
results = { duplicates: [], data: [] } | |
loop do | |
response = HTTParty.get(url + "&page=#{page}", headers: headers) | |
data = response.parsed_response["data"] | |
page = response.parsed_response["metadata"]["next_page"] | |
results[:duplicates] << get_duplicates(get_emails(data)) | |
results[:data] << data | |
break if page.nil? | |
end | |
render json: results | |
end | |
def get_duplicates(emails) | |
duplicates = {} | |
emails.each do |e1| | |
emails.each do |e2| | |
next if e1 == e2 | |
if Levenshtein.normalized_distance(e1, e2) < 0.2 | |
duplicates[e1] ? duplicates[e1] << e2 : duplicates[e1] = [e2] | |
end | |
end | |
end | |
duplicates | |
end | |
def get_emails(results) | |
results.map { |r| r[:email] } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
describe Api::DuplicateController do | |
let(:base_url) { | |
"https://api.salesloft.com/v2/people.json" + | |
"?per_page=50&include_paging_counts=true&page=1" | |
} | |
let(:response) {{ | |
metadata: { paging: { next_page: nil }}, | |
data: [ | |
{ id: 1, email: "place4382.com" }, | |
{ id: 2, email: "place2.com" }, | |
{ id: 3, email: "place.com" }, | |
] | |
}} | |
let(:ok_response) { | |
double(HTTParty::Response, code: 200, parsed_response: response) | |
} | |
describe "#show" do | |
it "calls HTTParty" do | |
expect(HTTParty).to receive(:get).with(base_url).and_return(ok_response) | |
get :show | |
end | |
end | |
describe "#get_duplicates" do | |
it "checks for duplicates" do | |
expect(HTTParty).to receive(:get).with(base_url).and_return(ok_response) | |
expect(response_json[:duplicates]).to eq( | |
{"place2.com"=>["place.com"], "place.com"=>["place2.com"]} | |
) | |
get :show | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment