Skip to content

Instantly share code, notes, and snippets.

@phsteve
Created July 16, 2014 00:59
Show Gist options
  • Save phsteve/63bec9dc600ebd998758 to your computer and use it in GitHub Desktop.
Save phsteve/63bec9dc600ebd998758 to your computer and use it in GitHub Desktop.
diff --git a/dispatch.py b/dispatch.py
index ba7c064..fd5f136 100644
--- a/dispatch.py
+++ b/dispatch.py
@@ -1,29 +1,36 @@
import requests
import json
-base_url = 'http://maps.googleapis.com/maps/api/distancematrix/json'
+class Client(object):
+ def __init__(self, server):
+ self.server = server
-def fetch_distance_matrix(ambulances, job):
- #ambulance = string of "lat,lon"
- #job = string of "lat,lon"
- payload = {'origins': '|'.join(ambulances), 'destinations': job}
- resp = requests.get(base_url, params=payload)
- json_resp = json.loads(resp.content)
- return json_resp
+ def extract_distances(self, ambulances, job):
+ distance_matrix = self.server.fetch_distance_matrix(ambulances, job)
+ return [row['elements'][0]['duration']['value'] for row in distance_matrix['rows']]
-def get_distances(distance_matrix):
- return [row['elements'][0]['duration']['value'] for row in distance_matrix['rows']]
+ def index_of_closest(self, distances):
+ return distances.index(min(distances))
+
+class Server(object):
+ def __init__(self):
+ self.base_url = 'http://maps.googleapis.com/maps/api/distancematrix/json'
+
+ def fetch_distance_matrix(self, ambulances, job):
+ #ambulance = string of "lat,lon"
+ #job = string of "lat,lon"
+ payload = {'origins': '|'.join(ambulances), 'destinations': job}
+ resp = requests.get(self.base_url, params=payload)
+ json_resp = json.loads(resp.content)
+ return json_resp
+
+class MockServer(object):
+ def fetch_distance_matrix(self, ambulances, job):
+ f = open('test_data.json')
+ json_resp = json.loads(''.join(f.readlines()))
+ return json_resp
-def choose_index(distances):
- smallest = min(distances)
- return distances.index(smallest)
if __name__ == '__main__':
- #print foo(['40.690574,-73.958873', '40.685709,-73.955226'], '40.689947,-73.964302')
- # f = open('test_data.json')
- # json_resp = json.loads(''.join(f.readlines()))
- # distances = get_distances(json_resp)
- # print get_distances(json_resp)
- lst = [3, 1, 7]
- print choose_index(lst)
\ No newline at end of file
+ pass
\ No newline at end of file
diff --git a/tests.py b/tests.py
index a9ea7be..a4b8f0b 100644
--- a/tests.py
+++ b/tests.py
@@ -5,14 +5,19 @@ import dispatch
class TestSequenceFunctions(unittest.TestCase):
+ def setUp(self):
+ self.server = dispatch.MockServer()
+ self.client = dispatch.Client(self.server)
+
def test_get_distances(self):
- f = open('test_data.json')
- json_resp = json.loads(''.join(f.readlines()))
- distances = dispatch.get_distances(json_resp)
- self.assertEquals(distances, [48, 181])
+ origins = ['1,1', '2,2']
+ destination = '3,3'
+ distances = self.client.extract_distances(origins, destination)
+ self.assertEquals([48, 181], distances)
- def test_choose_index(self):
- pass
+ def test_index_of_closest(self):
+ self.assertEquals(self.client.index_of_closest([3, 1, 7]), 1)
+
if __name__ == "__main__":
unittest.main()
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment