Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/services/usps_in_person_proofing/mock/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def self.request_facilities_response
load_response_fixture('request_facilities_response.json')
end

def self.request_facilities_response_with_unordered_distance
load_response_fixture('request_facilities_response_with_unordered_distance.json')
end

def self.request_facilities_response_with_duplicates
load_response_fixture('request_facilities_response_with_duplicates.json')
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
{
"postOffices": [
{
"parking": "Lot",
"hours": [
{
"weekdayHours": "9:00 AM - 6:00 PM"
},
{
"saturdayHours": "9:00 AM - 3:00 PM"
},
{
"sundayHours": "Closed"
}
],
"distance": "6.63 mi",
"streetAddress": "4750 J ST",
"city": "SACRAMENTO",
"phone": "916-227-6503",
"name": "CAMELLIA",
"zip4": "9998",
"state": "CA",
"zip5": "95819"
},
{
"parking": "Lot",
"hours": [
{
"weekdayHours": "9:00 AM - 6:00 PM"
},
{
"saturdayHours": "9:00 AM - 3:00 PM"
},
{
"sundayHours": "Closed"
}
],
"distance": "3.87 mi",
"streetAddress": "660 J ST STE 170",
"city": "SACRAMENTO",
"phone": "916-498-9145",
"name": "DOWNTOWN PLAZA",
"zip4": "9996",
"state": "CA",
"zip5": "95814"
},
{
"parking": "Street",
"hours": [
{
"weekdayHours": "9:00 AM - 6:00 PM"
},
{
"saturdayHours": "9:00 AM - 3:00 PM"
},
{
"sundayHours": "Closed"
}
],
"distance": "0.05 mi",
"streetAddress": "3775 INDUSTRIAL BLVD",
"city": "WEST SACRAMENTO",
"phone": "916-556-3406",
"name": "INDUSTRIAL WEST SACRAMENTO",
"zip4": "9998",
"state": "CA",
"zip5": "95799"
},
{
"parking": "Lot",
"hours": [
{
"weekdayHours": "9:00 AM - 6:00 PM"
},
{
"saturdayHours": "9:00 AM - 3:00 PM"
},
{
"sundayHours": "Closed"
}
],
"distance": "4.53 mi",
"streetAddress": "2121 BROADWAY",
"city": "SACRAMENTO",
"name": "BROADWAY",
"phone": "916-227-6503",
"zip4": "9998",
"state": "CA",
"zip5": "95818"
},
{
"parking": "Street",
"hours": [
{
"weekdayHours": "9:00 AM - 6:00 PM"
},
{
"saturdayHours": "9:00 AM - 3:00 PM"
},
{
"sundayHours": "Closed"
}
],
"distance": "2.77 mi",
"streetAddress": "900 SACRAMENTO AVE",
"city": "WEST SACRAMENTO",
"phone": "916-556-3406",
"name": "BRODERICK",
"zip4": "9998",
"state": "CA",
"zip5": "95605"
},
{
"parking": "Lot",
"hours": [
{
"weekdayHours": "9:00 AM - 6:00 PM"
},
{
"saturdayHours": "9:00 AM - 3:00 PM"
},
{
"sundayHours": "Closed"
}
],
"distance": "5.33 mi",
"streetAddress": "1618 ALHAMBRA BLVD",
"city": "SACRAMENTO",
"phone": "916-227-6503",
"name": "FORT SUTTER",
"zip4": "9998",
"state": "CA",
"zip5": "95816"
},
{
"parking": "Lot",
"hours": [
{
"weekdayHours": "9:00 AM - 6:00 PM"
},
{
"saturdayHours": "9:00 AM - 3:00 PM"
},
{
"sundayHours": "Closed"
}
],
"distance": "4.55 mi",
"streetAddress": "5930 S LAND PARK DR",
"city": "SACRAMENTO",
"phone": "916-262-3107",
"name": "LAND PARK",
"zip4": "9998",
"state": "CA",
"zip5": "95822"
},
{
"parking": "Lot",
"hours": [
{
"weekdayHours": "9:00 AM - 6:00 PM"
},
{
"saturdayHours": "9:00 AM - 3:00 PM"
},
{
"sundayHours": "Closed"
}
],
"distance": "5.49 mi",
"streetAddress": "2929 35TH ST",
"city": "SACRAMENTO",
"phone": "916-227-6509",
"name": "OAK PARK",
"zip4": "9998",
"state": "CA",
"zip5": "95817"
},
{
"parking": "Lot",
"hours": [
{
"weekdayHours": "9:00 AM - 5:00 PM"
},
{
"saturdayHours": "9:00 AM - 5:00 PM"
},
{
"sundayHours": "Closed"
}
],
"distance": "2.22 mi",
"streetAddress": "1601 MERKLEY AVE",
"city": "WEST SACRAMENTO",
"name": "WEST SACRAMENTO",
"phone": "916-556-3406",
"state": "CA",
"zip4": "9998",
"zip5": "95691"
}
]
}

7 changes: 6 additions & 1 deletion app/services/usps_in_person_proofing/proofer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def request_facilities(location)
end.body

facilities = parse_facilities(response)
dedupe_facilities(facilities)
dedupe_facilities = dedupe_facilities(facilities)
sort_by_ascending_distance(dedupe_facilities)
end

# Temporary function to return a static set of facilities
Expand Down Expand Up @@ -230,5 +231,9 @@ def dedupe_facilities(facilities)
[facility.address, facility.city, facility.state, facility.zip_code_5]
end
end

def sort_by_ascending_distance(facilities)
facilities.sort_by { |f| f[:distance].to_f }
end
end
end
10 changes: 10 additions & 0 deletions spec/services/usps_in_person_proofing/proofer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ def check_for_token_refresh_and_method_call(cache, redis)
check_facility(facilities[0])
end

it 'returns facilities sorted by ascending distance' do
stub_request_facilities_with_unordered_distance
facilities = subject.request_facilities(location)

expect(facilities.count).to be > 1
facilities.each_cons(2) do |facility_a, facility_b|
expect(facility_a.distance).to be <= facility_b.distance
end
end

it 'does not return duplicates' do
stub_request_facilities_with_duplicates
facilities = subject.request_facilities(location)
Expand Down
9 changes: 9 additions & 0 deletions spec/support/usps_ipp_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def stub_request_facilities
)
end

def stub_request_facilities_with_unordered_distance
stub_request(:post, %r{/ivs-ippaas-api/IPPRest/resources/rest/getIppFacilityList}).to_return(
status: 200,
body:
UspsInPersonProofing::Mock::Fixtures.request_facilities_response_with_unordered_distance,
headers: { 'content-type' => 'application/json' },
)
end

def stub_request_facilities_with_duplicates
stub_request(:post, %r{/ivs-ippaas-api/IPPRest/resources/rest/getIppFacilityList}).to_return(
status: 200,
Expand Down