Skip to content

Commit

Permalink
fix: cleanse inputs that may have issues
Browse files Browse the repository at this point in the history
Closes #124
  • Loading branch information
stdavis committed Jun 6, 2023
1 parent b5eaf8e commit 44b0d08
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/masquerade/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from requests.models import HTTPError

from .providers import open_sgid, web_api
from .utils import cleanse_text

load_dotenv()

Expand Down Expand Up @@ -176,7 +177,7 @@ def suggest():
search_text = request.args.get('text')
max_results = request.args.get('maxSuggestions') or DEFAULT_MAX_SUGGESTIONS

return {'suggestions': open_sgid.get_suggestions(search_text, max_results)}
return {'suggestions': open_sgid.get_suggestions(cleanse_text(search_text), max_results)}


@app.route(f'{GEOCODE_SERVER_ROUTE}/findAddressCandidates')
Expand All @@ -197,7 +198,7 @@ def find_candidates():
candidate = open_sgid.get_candidate_from_magic_key(magic_key, out_spatial_reference)
candidates = [candidate]
else:
single_line_address = request.args.get('Single Line Input')
single_line_address = cleanse_text(request.args.get('Single Line Input'))
max_locations = request.args.get('maxLocations')
try:
candidates = web_api.get_candidates_from_single_line(
Expand Down Expand Up @@ -258,16 +259,18 @@ def geocode_addresses():

#: prefer zip over city and return no match if neither is passed
try:
zone = address['attributes']['Zip']
zone = cleanse_text(address['attributes']['Zip'])
except KeyError:
try:
zone = address['attributes']['City']
zone = cleanse_text(address['attributes']['City'])
except KeyError:
locations.append(no_match)
continue

try:
candidate = web_api.get_candidate_from_parts(address['attributes']['Address'], zone, out_spatial_reference)
candidate = web_api.get_candidate_from_parts(
cleanse_text(address['attributes']['Address']), zone, out_spatial_reference
)

if candidate is None:
candidate = no_match
Expand Down
11 changes: 11 additions & 0 deletions src/masquerade/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python
# * coding: utf8 *
"""
Utility functions
"""


def cleanse_text(text):
""" removes leading or trailing spaces and quotes
"""
return text.strip().replace('"', '').replace('\'', '')
10 changes: 10 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from masquerade.utils import cleanse_text


def test_removes_spaces():
assert cleanse_text(' hello ') == 'hello'


def test_removes_quotes():
assert cleanse_text('"hello"') == 'hello'
assert cleanse_text('test \'hello') == 'test hello'

0 comments on commit 44b0d08

Please sign in to comment.