Skip to content

Commit

Permalink
adding support for lookup by address
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Edwards authored and Chris Edwards committed Jan 3, 2017
1 parent 4c5c1a4 commit 4612a7c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

import { getDistrictByLatLng } from 'congressional-district-finder';
import {
getDistrictByLatLng,
getDistrictByAddress
} from 'congressional-district-finder';
import { getMembers, setProPublicaKey } from './propublica';


Expand All @@ -20,21 +23,29 @@ function response(body, statusCode = 200, headers = DEFAULT_HEADERS) {
function handler(event, context, callback) {

const { queryStringParameters } = event;
const { latLng } = queryStringParameters || {};
const { latLng, address } = queryStringParameters || {};

setProPublicaKey(process.env.PROPUBLICA_KEY);

if (!latLng) {
if (!latLng && !address) {

const message = 'Must provide a query string value: "latLng", ' +
'a comma delimited set of coordinates.';
'a comma delimited set of coordinates,' +
' or a query string value: "address".';
callback(null, response({ message }, 400));

} else {

const [lat, lng] = latLng.split(',');
let getDistrict;

getDistrictByLatLng(lat, lng)
if (address) {
getDistrict = () => getDistrictByAddress(address);
} else {
const [lat, lng] = latLng.split(',');
getDistrict = () => getDistrictByLatLng(lat, lng);
}

getDistrict()
.then(({ district }) => getMembers(district.districtCode))
.then((result) => {
callback(null, response({ result }))
Expand Down
22 changes: 22 additions & 0 deletions test/test_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,26 @@ describe('The Index Lambda Handler', () => {
});
});
});

context('with an address in the US', () => {

const event = {
queryStringParameters: {
address: '45 Main Street Brooklyn'
}
};

it('finds a congressional district', (done) => {
handler(event, {}, (err, { body }) => {
try {
const { result } = JSON.parse(body);
expect(result.district)
.to.eq('NY-07');
done();
} catch (err) {
done(err);
}
});
});
});
});

0 comments on commit 4612a7c

Please sign in to comment.