diff options
author | Christian Struck <christian@struck.se> | 2013-02-04 21:16:12 +0100 |
---|---|---|
committer | Christian Struck <christian@struck.se> | 2013-02-04 21:16:12 +0100 |
commit | 1ede09d2c27066f161e65aec0a309c9cdc32a54b (patch) | |
tree | 14ae6defb41af13b8abad77059f2ca486d426b9f /bvggrabber/api/__init__.py | |
parent | e1981063359b0910614e5ea3f861a5ce1e54970d (diff) | |
download | bvg-grabber-1ede09d2c27066f161e65aec0a309c9cdc32a54b.tar.gz bvg-grabber-1ede09d2c27066f161e65aec0a309c9cdc32a54b.tar.bz2 bvg-grabber-1ede09d2c27066f161e65aec0a309c9cdc32a54b.zip |
made Response object json serializable reworked tests and removed to_json from departure object
Diffstat (limited to 'bvggrabber/api/__init__.py')
-rw-r--r-- | bvggrabber/api/__init__.py | 22 |
1 files changed, 8 insertions, 14 deletions
diff --git a/bvggrabber/api/__init__.py b/bvggrabber/api/__init__.py index c489e8b..3cd1239 100644 --- a/bvggrabber/api/__init__.py +++ b/bvggrabber/api/__init__.py @@ -9,6 +9,7 @@ from math import floor from dateutil.parser import parse from bvggrabber.utils.format import fullformat, timeformat +from bvggrabber.utils.json import ObjectJSONEncoder def compute_remaining(start, end): @@ -32,9 +33,9 @@ class QueryApi(object): class Response(object): - def __init__(self, state, departures, error=None): + def __init__(self, state, station=None, departures=None, error=None): self._state = state - self._departures = departures + self._departures = [(station, departures)] self._error = error def merge(self, other): @@ -49,6 +50,10 @@ class Response(object): raise TypeError("The given object is not a response object") @property + def to_json(self): + return ObjectJSONEncoder(ensure_ascii=False).encode(self.departures) + + @property def state(self): return self._state @@ -88,18 +93,7 @@ class Departure(object): @property def remaining(self): - return compute_remaining(self.now, self.when) - - @property - def to_json(self): - return json.dumps({'start': self.start, - 'end': self.end, - 'line': self.line, - 'now_full': fullformat(self.now), - 'now_hour': timeformat(self.now), - 'when_full': fullformat(self.when), - 'when_hour': timeformat(self.when), - 'remaining': round(self.remaining)}) + return int(compute_remaining(self.now, self.when)) def __eq__(self, other): """Two departures are assumed to be equal iff their remaining time |