diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-04-06 16:17:06 +0200 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-04-06 16:17:06 +0200 |
commit | bd844547c7eacd2ab44cfda79bef499961a19fd5 (patch) | |
tree | f079a43400c83145d3ea4ab5466b13b74bcbd530 | |
parent | b7c6edf9d519ee1f3516d6e4cf60ca25269a74d0 (diff) |
Naive ActivityStream object
-rw-r--r-- | pyapd/objects.py | 29 | ||||
-rw-r--r-- | tests/test_objects.py | 22 |
2 files changed, 51 insertions, 0 deletions
diff --git a/pyapd/objects.py b/pyapd/objects.py new file mode 100644 index 0000000..19aa65f --- /dev/null +++ b/pyapd/objects.py @@ -0,0 +1,29 @@ +import json + + +class ActivityObject(): + PROPERTIES = [ + 'id', + 'type', + ] + + def __init__(self, *args, **kw): + self.context = "https://www.w3.org/ns/activitystreams" + for p in self.PROPERTIES: + if p in kw: + setattr(self, p, kw[p]) + + @classmethod + def from_jsons(cls, data: str): + return cls(**json.loads(data)) + + def to_jsonable(self): + data = {} + for p in self.PROPERTIES: + v = getattr(self, p, None) + if v is not None: + data[p] = v + return data + + def to_jsons(self): + return json.dumps(self.to_jsonable()) diff --git a/tests/test_objects.py b/tests/test_objects.py new file mode 100644 index 0000000..9646269 --- /dev/null +++ b/tests/test_objects.py @@ -0,0 +1,22 @@ +import unittest + +from pyapd import objects + + +class TestObjects(unittest.TestCase): + + def test_activity_from_json(self): + act = objects.ActivityObject.from_jsons('''{ + "id": "http://example.org/123456" + }''') + self.assertIsInstance(act, objects.ActivityObject) + self.assertEqual(act.id, 'http://example.org/123456') + + def test_activity_to_json(self): + act = objects.ActivityObject( + id="http://example.org/123456" + ) + data = act.to_jsons() + self.assertIsInstance(data, str) + self.assertIn('"id": "http://example.org/123456"', data) + |