Commits

Ferran Pegueroles committed d3dbc87

Initial commit

  • Participants

Comments (0)

Files changed (9)

+syntax: glob
+node_modules
+*.pyc
+.venv
+.env
+data.db
+*.log
+.ropeproject
+htmlcov
+cover
+.coverage

File peerreach/__init__.py

+__version__= "0.1"
+
+from .api import Api, TooMuchArgsException,  NoArgsException
+from .parsers import RawParser, JSONParser, ObjectParser

File peerreach/api.py

+import urllib
+import requests
+
+from parsers import JSONParser
+
+
+class ApiException(Exception):
+    pass
+
+
+class NoArgsException(ApiException):
+    pass
+
+class TooMuchArgsException(ApiException):
+    pass
+
+class HttpError(ApiException):
+   pass
+
+
+class Api(object):
+
+    def __init__(self, parser=None, url=None):
+
+        self.api_url = url or 'http://api.peerreach.com/v1'
+        self.api_user_url = self.api_url + '/user/lookup.json'
+        self.api_multi_user_url = self.api_url + '/multi-user/lookup.json'
+
+        if parser:
+           self.parser = parser
+        else:
+           self.parser = JSONParser()
+
+    def parse(self, data):
+        return self.parser.parse(data)
+
+
+    def get_data(self, url, **options):
+
+        response = requests.get(url, **options)
+
+        if str(response.status)[0] != '2':
+           raise HttpException(response.status)
+
+        return self.parse(response.data)
+
+
+    def user_lookup(self, user_id=None, screen_name=None):
+        if not any((user_id, screen_name)):
+           raise NoArgsException()
+        if all((user_id, screen_name)):
+           raise TooMuchArgsException()
+
+        if user_id:
+           params = {"user_id": user_id}
+        else:
+           params = {"screen_name": screen_name}
+
+        url = "%s?%s" % (self.api_user_url, urllib.urlencode(params))
+
+        return self.get_data(url)
+
+    def multi_user_lookup(self, user_ids=None, screen_names=None):
+        if not any((user_ids, screen_names)):
+           raise NoArgsException()
+        if all((user_ids, screen_names)):
+           raise TooMuchArgsException()
+
+        if user_ids:
+           params = {"user_id": ",".join(map(str, user_ids))}
+        else:
+           params = {"screen_name": ",".join(screen_names)}
+
+        url = "%s?%s" % (self.api_multi_user_url, urllib.urlencode(params))
+
+        return self.get_data(url)

File peerreach/parsers.py

+import json
+
+class RawParser(object):
+    def parse(self, raw):
+        return raw
+
+class JSONParser(object):
+    def parse(self, raw):
+        return json.loads(raw)
+
+class PeerreachObject(object):
+    def __init__(self, raw):
+        data = json.loads(raw)
+        self.__dict__.update(data)
+
+class ObjectParser(object):
+    def __init__(self, klass=PeerreachObject):
+        self.klass = klass
+
+    def parse(self, raw):
+        return self.klass(raw)

File peerreach/peerreach.py

+
+
+# http://api.peerreach.com/v1/user/lookup.json?screen_name=fredwilson
+# http://api.peerreach.com/v1/user/lookup.json?user_id=fredwilson
+
+# http://api.peerreach.com/v1/multi-user/lookup.json?screen_name=PeerReach,Scobleizer
+# http://api.peerreach.com/v1/multi-user/lookup.json?user_id=PeerReach,Scobleizer
+
+# __init__.py
+
+# __version__= "0.1"
+
+# from .api import Api
+# from .parsers import RawParser, JSONParser, ObjectParser
+
+
+# parsers.py
+
+
+# class RawParser(object):
+#     def parse(raw):
+#         return raw
+
+# class JSONParser(object):
+#     def parse(raw):
+#         return json.loads(raw)
+
+# class PeerreachObject(object):
+#     def __init__(self, raw):
+#         data = json.loads(raw)
+#         self.__dict__.update(data)
+
+# class ObjectParser(object):
+#     def __init__(self, klass=PeerreachObject):
+#     	self.klass = klass
+
+#     def parse(raw):
+#         return klass(raw)
+
+
+# api.py
+
+# import urllib
+
+# class ApiException(Exception):
+#     pass
+
+
+# class NoArgsException(ApiException):
+#     pass
+
+# class TooMuchArgsException(ApiException):
+#     pass
+
+# class HttpError(ApiException)
+#    pass
+
+
+# class Api(object):
+
+
+#     def __init__(self, parser=None, url=None):
+
+# 	self.api_url = url or 'http://api.peerreach.com/v1'
+# 	aelf.api_user_url = api_url + '/user/lookup.json'
+# 	aelf.api_multi_user_url = api_url + '/multi-user/lookup.json'
+
+#         if parser:
+#            self.parser = parser
+# 	slse:
+# 	   self.parser = JSONParser()
+
+#     def parse(data):
+#     	return self.parser.parse(data)
+
+
+#     def get_data(self, url, **options):
+
+#         response = requests.get(url, **options)
+
+#         if str(response.status)[0] != '2':
+#            raise HttpException(response.status)
+
+#         return self.parse(response.data)
+
+
+#     def user_lookup(self, user_id=None, screen_name=None):
+#     	if not any(user_id, screen_name):
+#     	   raise NoArgsException()
+#     	if all(user_id, screen_name):
+#     	   raise TooMuchArgsException()
+
+#     	if user_id:
+#     	   params = {"user_id": user_id}
+#     	else:
+#     	   params = {"screen_name": screen_name}
+
+#     	url = "%s?%s" % (self.api_user_url, urllib.urlencode(params))
+
+#         return self.get_data(url)
+
+#     def multi_user_lookup(self, user_ids=None, screen_names=None):
+#     	if not any(user_ids, screen_names):
+#     	   raise NoArgsException()
+#     	if all(user_ids, screen_names):
+#     	   raise TooMuchArgsException()
+
+#     	if self.user_id:
+#     	   params = {"user_id": ",".join(map(str, user_id))}
+#     	else:
+#     	   params = {"screen_name": ",".join(screen_name)}
+
+#         url = "%s?%s" % (self.api_multi_user_url, urllib.urlencode(params))
+
+#         return self.get_data(url)
+
+
+# runner.py
+
+
+# def main():
+
+#    # -r --raw : Output raw data
+#    # -i --ids : Use ids instead of screen names
+
+#    args, options = parser.parse_args()
+
+#    if not args:
+#       parser.error()
+
+#    if options.raw:
+#       parser = RawParser()
+#    else:
+#       parser = JSONParser()
+
+#    api = Api(parser=parser)
+
+
+#    if len(args) == 1:
+#       if option.ids:
+#          data = api.user_lookup(user_id=args[0])
+#       else:
+#          data = api.user_lookup(screen_name=args[0])
+#    else:
+#       if option.ids:
+#          data = api.multi_user_lookup(user_ids=args)
+#       else:
+#          data = api.multi_user_lookup(screen_names=args)
+
+#    if options.raw:
+#       print data
+#    else:
+#       pprint.pprint(data)
+
+
+
+# tests.py
+
+
+# class TestParser(TestCase):
+
+#     raw = "{'some_raw_data': 1234}"
+
+#     def setUp(self):
+#         pass
+
+#     def test_raw_parse():
+#         parser = RawParser()
+
+#         parsed = parser.parse(self.raw)
+#         self.assertEqual(parsed, self.raw)
+
+#     def test_json_parse():
+#         parser = JSONParser()
+
+#         parsed = parser.parse(self.raw)
+#         self.assertEqual(json.dumps(parsed), self.raw)
+
+#     def test_object_parse():
+#         parser = ObjectParser()
+
+#         parsed = parser.parse(self.raw)
+
+#         data = json.loads(self.raw)
+
+#         self.assertEqual(parsed.some_raw_data, data['some_raw_data'])
+
+
+# class TestApi(TestCase):
+
+#     def test_init_default_url(self):
+#         api = Api()
+#         self.assertEqual(api.api_url, ''http://api.peerreach.com/v1')
+
+#     def test_init_lookup_url(self):
+#         api = Api()
+#         self.assertEqual(api.api_user_url, self.api_url + '/user/lookup.json')
+#         self.assertEqual(api.api_multi_user_url, self.api_url + '/multi-user/lookup.json')
+
+#     def test_init_set_url(self):
+#         api = Api(url='http://test')
+#         self.assertEqual(api.api_url, ''http://test')
+
+#     def test_init_default_parser(self):
+#         api = Api()
+#         self.assertTrue(isinstance(api.parser, JSONParser)
+
+#     def test_init_set_parser(self):
+#         class DummyParser:
+#             pass
+#         parser = DummyParser()
+#         api = Api(parser=parser)
+#         self.assertEqual(parser, api.parser)
+
+#     def test_parser(self):
+#         api = Api()
+
+#         raw = "{'aaa': 'bbbb', 'cccc': 123}"
+#         data = api.parse(raw)
+
+#         self.assertEqual(data, json.loads(raw)
+
+#     @mock("requests.get')
+#     def test_get_data(self, get):
+
+#         class Response:
+#             status_code = 200
+#             data = "test response"
+
+#         get.return_value = Response()
+
+
+#         api.Api()
+
+#         test_url = "http://test"
+
+#         data = api.get_url(test_url)
+
+#         self.assertEqual(data, Response().data)
+#         get.assert_called_once_with(url)
+
+
+#     class DummyApi(Api):
+#         self.get_data(url):
+#             self.called_url = url
+#             return SAMPLE_RETURN
+
+#     def test_user_lookup_user_id(self):
+#         api = DummyApi()
+
+#         data = api.user_lookup(user_id=1111)
+#         self.assertEqual(data , SAMPLE_RETURN)
+#         self.assertEqual(api.called_url, "http:/.......")
+
+#     def test_user_lookup_screen_name(self):
+#         api = DummyApi()
+
+#         data = api.user_lookup(screen_name='test_user')
+#         self.assertEqual(data , SAMPLE_RETURN)
+#         self.assertEqual(api.called_url, "http:/.......")
+
+#     def test_user_lookup_no_data(self):
+# 	api = Api()
+
+# 	self.assertRaises(NoArgsException, api.user_lookup)
+
+#     def test_user_lookup_too_data(self):
+# 	api = Api()
+
+# 	self.assertRaises(TooMuchArgsException, api.user_lookup, user_id=1111. screen_name='test')
+
+#     def test_multi_user_lookup_user_ids(self):
+#         api = DummyApi()
+
+#         data = api.multi_user_lookup(user_ids=[1111,2222])
+#         self.assertEqual(data , SAMPLE_RETURN)
+#         self.assertEqual(api.called_url, "http:/.......")
+
+#     def test_multi_user_lookup_screen_names(self):
+#         api = DummyApi()
+
+#         data = api.multi_user_lookup(screen_names=['test1', 'test2'])
+#         self.assertEqual(data , SAMPLE_RETURN)
+#         self.assertEqual(api.called_url, "http:/.......")
+
+#     def test_multi_user_lookup_no_data(self):
+# 	api = Api()
+
+# 	self.assertRaises(NoArgsException, api.multi_user_lookup)
+
+#     def test_multi_user_lookup_too_data(self):
+# 	api = Api()
+
+# 	self.assertRaises(TooMuchArgsException, api.multi_user_lookup, user_ids=1111. screen_names='test')
+
+
+# requirements.txt
+
+# mock
+# coverage
+# nose
+# requests

File peerreach/runner.py

+
+def main():
+
+   # -r --raw : Output raw data
+   # -i --ids : Use ids instead of screen names
+
+   args, options = parser.parse_args()
+
+   if not args:
+      parser.error()
+
+   if options.raw:
+      parser = RawParser()
+   else:
+      parser = JSONParser()
+
+   api = Api(parser=parser)
+
+
+   if len(args) == 1:
+      if option.ids:
+         data = api.user_lookup(user_id=args[0])
+      else:
+         data = api.user_lookup(screen_name=args[0])
+   else:
+      if option.ids:
+         data = api.multi_user_lookup(user_ids=args)
+      else:
+         data = api.multi_user_lookup(screen_names=args)
+
+   if options.raw:
+      print data
+   else:
+      pprint.pprint(data)
+
+
+
+if __name__ == '__main__':
+   main()

File requirements.txt

+coverage==3.6b2
+mock==1.0.1
+nose==1.2.1
+requests==1.0.4

File tests/__init__.py

+

File tests/test_peerreach.py

+
+from unittest import TestCase
+import mock
+import json
+
+from peerreach import Api
+from peerreach import JSONParser, RawParser, ObjectParser
+from peerreach import TooMuchArgsException, NoArgsException
+
+
+class TestParser(TestCase):
+
+    raw = '{"some_raw_data": 1234}'
+
+    def setUp(self):
+        pass
+
+    def test_raw_parse(self):
+        parser = RawParser()
+
+        parsed = parser.parse(self.raw)
+        self.assertEqual(parsed, self.raw)
+
+    def test_json_parse(self):
+        parser = JSONParser()
+
+        parsed = parser.parse(self.raw)
+        self.assertEqual(json.dumps(parsed), self.raw)
+
+    def test_object_parse(self):
+        parser = ObjectParser()
+
+        parsed = parser.parse(self.raw)
+
+        data = json.loads(self.raw)
+
+        self.assertEqual(parsed.some_raw_data, data['some_raw_data'])
+
+
+SAMPLE_RETURN = object()
+
+class DummyApi(Api):
+    def get_data(self, url):
+        self.called_url = url
+        return SAMPLE_RETURN
+
+
+class TestApi(TestCase):
+
+    def test_init_default_url(self):
+        api = Api()
+        self.assertEqual(api.api_url, 'http://api.peerreach.com/v1')
+
+    def test_init_lookup_url(self):
+        api = Api()
+        self.assertEqual(api.api_user_url, api.api_url + '/user/lookup.json')
+        self.assertEqual(api.api_multi_user_url, api.api_url + '/multi-user/lookup.json')
+
+    def test_init_set_url(self):
+        api = Api(url='http://test')
+        self.assertEqual(api.api_url, 'http://test')
+
+    def test_init_default_parser(self):
+        api = Api()
+        self.assertTrue(isinstance(api.parser, JSONParser))
+
+    def test_init_set_parser(self):
+        class DummyParser:
+            pass
+        parser = DummyParser()
+        api = Api(parser=parser)
+        self.assertEqual(parser, api.parser)
+
+    def test_parser(self):
+        api = Api()
+
+        raw = '{"aaa": "bbbb", "cccc": 123}'
+        data = api.parse(raw)
+
+        self.assertEqual(data, json.loads(raw))
+
+    @mock.patch("requests.get")
+    def test_get_data(self, get):
+
+        class Response:
+            status = 200
+            data = '{"data": "test response"}'
+
+        get.return_value = Response()
+
+
+        api = Api()
+
+        test_url = "http://test"
+
+        data = api.get_data(test_url)
+
+        self.assertEqual(data, json.loads(Response().data))
+        get.assert_called_once_with(test_url)
+
+    def test_user_lookup_user_id(self):
+        api = DummyApi()
+
+        data = api.user_lookup(user_id=1111)
+        self.assertEqual(data , SAMPLE_RETURN)
+        self.assertEqual(api.called_url, 'http://api.peerreach.com/v1/user/lookup.json?user_id=1111')
+
+    def test_user_lookup_screen_name(self):
+        api = DummyApi()
+
+        data = api.user_lookup(screen_name='test_user')
+        self.assertEqual(data , SAMPLE_RETURN)
+        self.assertEqual(api.called_url, 'http://api.peerreach.com/v1/user/lookup.json?screen_name=test_user')
+
+    def test_user_lookup_no_data(self):
+        api = Api()
+
+        self.assertRaises(NoArgsException, api.user_lookup)
+
+    def test_user_lookup_too_data(self):
+        api = Api()
+
+        self.assertRaises(TooMuchArgsException, api.user_lookup, user_id=1111, screen_name='test')
+
+    def test_multi_user_lookup_user_ids(self):
+        api = DummyApi()
+
+        data = api.multi_user_lookup(user_ids=[1111,2222])
+        self.assertEqual(data , SAMPLE_RETURN)
+        self.assertEqual(api.called_url, 'http://api.peerreach.com/v1/multi-user/lookup.json?user_id=1111%2C2222')
+
+    def test_multi_user_lookup_screen_names(self):
+        api = DummyApi()
+
+        data = api.multi_user_lookup(screen_names=['test1', 'test2'])
+        self.assertEqual(data , SAMPLE_RETURN)
+        self.assertEqual(api.called_url, 'http://api.peerreach.com/v1/multi-user/lookup.json?screen_name=test1%2Ctest2')
+
+    def test_multi_user_lookup_no_data(self):
+        api = Api()
+
+        self.assertRaises(NoArgsException, api.multi_user_lookup)
+
+    def test_multi_user_lookup_too_data(self):
+        api = Api()
+
+        self.assertRaises(TooMuchArgsException, api.multi_user_lookup, user_ids=1111, screen_names='test')