Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code challenge #52

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
#
# Read more on Dockerfile best practices at the source:
# https://docs.docker.com/develop/develop-images/dockerfile_best-practices
RUN apt-get update && apt-get install -y --no-install-recommends postgresql-client nodejs
RUN apt-get update && apt-get install -y --no-install-recommends postgresql-client nodejs npm

# Inside the container, create an app directory and switch into it
RUN mkdir /app
Expand Down
26 changes: 26 additions & 0 deletions parserator_web/static/js/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
/* TODO: Flesh this out to connect the form to the API and render results
in the #address-results div. */
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('submit').addEventListener('click', async (e) => {
const inputString = document.getElementById('address').value;
const url = `api/parse/?input_string=${encodeURIComponent(inputString)}`;

try {
const response = await fetch(url, {
method: "GET",
headers: {
'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]').value,
}
});

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

const data = await response.json();
console.log(data);
} catch (error) {
console.error('Error:', error);
}
}, false);
});


6 changes: 5 additions & 1 deletion parserator_web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ class AddressParse(APIView):
def get(self, request):
# TODO: Flesh out this method to parse an address string using the
# parse() method and return the parsed components to the frontend.
return Response({})
address_components, address_type = AddressParse.parse(request.input_string)
input_string = request.input_string
return Response({input_string, address_components, address_type})

def parse(self, address):
# TODO: Implement this method to return the parsed components of a
# given address using usaddress: https://github.com/datamade/usaddress
address_components, address_type = zip(usaddress.parse(address))
return address_components, address_type

8 changes: 6 additions & 2 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import pytest
import urllib.parse


def test_api_parse_succeeds(client):
# TODO: Finish this test. Send a request to the API and confirm that the
# data comes back in the appropriate format.
address_string = '123 main st chicago il'
pytest.fail()
client.get('api/parse/', {'input_string': address_string})
# assert client.get(url)
# pytest.fail()


def test_api_parse_raises_error(client):
# TODO: Finish this test. The address_string below will raise a
# RepeatedLabelError, so ParseAddress.parse() will not be able to parse it.
address_string = '123 main st chicago il 123 main st'
pytest.fail()
client.get('api/parse/', {'input_string': address_string})
# pytest.fail()