# -----------------------------------------------------------------------
- def __init__(self, facebook, create_birthday_only = False):
+ def __init__(self, service_id, facebook, create_birthday_only = False):
- """Initialise the Facebook service, finding Facebook API keys in gconf and
- having a gui_callback available."""
-
self.fb = facebook
+ self._service_id = service_id
self._friends_by_name = {}
self._friends_by_url = {}
# -----------------------------------------------------------------------
- def get_name(self):
- return "Facebook"
-
-
- # -----------------------------------------------------------------------
def get_friends(self):
+ """Returns all friends on Facebook"""
+
return self._contacts_by_friend.keys()
+ # -----------------------------------------------------------------------
def get_contacts_with_match(self):
+ """Returns a dict, where each key value pair is a contact (key) that
+ matched a friend (value)"""
+
return self._friends_by_contact
+
+ # -----------------------------------------------------------------------
def get_unmatched_friends(self):
+ """Returns a list of all friends that didn't match a contact."""
+
return self._friends_by_name.values()
# -----------------------------------------------------------------------
def pre_process_contact(self, contact):
- """Registers URLs of all previous mappings, and makes sure that any Friends with those
- URLs don't get match by name."""
+ """Registers URLs of all previous mappings, and makes sure that any
+ friend with such a URL don't get match by name."""
+
for url in contact.get_urls():
self._known_urls.add(url)
# -----------------------------------------------------------------------
def process_friends(self):
+ """Retreives data from Facebook and parse that into Friend
+ objects."""
def if_defined(data, key, callback):
if key in data and data[key]:
# -----------------------------------------------------------------------
def process_contact(self, contact):
+ """If the contact is matched with a friend, that friend is returned,
+ otherwise None."""
+
matched_friend = None
if self._friends_by_contact.has_key(contact):
matched_friend = self._friends_by_contact[contact]
# -----------------------------------------------------------------------
def _get_friends_data(self):
- """Returns a list of dicts, where each dict represents a friend/contact"""
+ """Returns a list of dicts, where each dict represents the raw data
+ of a friend"""
return self.fb.users.getInfo(self.fb.friends.get(), Service.attrs)
import unittest
-
class FakeContact():
id_counter = 0
def __init__(self, name, addr, id=None):
def get_identifiers(self):
return [canonical(self.name)]
+
class TestFacebookService(unittest.TestCase):
def setUp(self):
- self.testee = Service(None)
+ self.testee = Service("facebook", None)
- def test_that_process_known_contact_returns_friend_object(self):
+ def test_that_process_contact_returns_friend_object_for_known_contact(self):
known_url = 'http://www.facebook.com/profile.php?id=123456'
known_contact = FakeContact('Facebook Junkie', [known_url])
self._fake_server_response([{'uid':'123456','name':'Facebook Junkie'}])
assert isinstance(result, Friend)
+ def test_that_process_contact_returns_None_for_unknown_contact(self):
+ known_contact = FakeContact('Facebook Junkie', [])
+ self._fake_server_response([])
+
+ self.testee.process_friends()
+ result = self.testee.process_contact(known_contact)
+ assert result is None
+
+
def test_main_flow_one_match_by_url_one_by_name(self):
# arrange
self.existing_address = 'http://www.facebook.com/profile.php?id=123456'
def test_name_collision_avoided_by_previous_matching(self):
- contact_do_match = FakeContact("Same Name", ["http://www.facebook.com/profile.php?id=123"], 1);
- contact_no_match = FakeContact("Same Name", [None], 2)
+ name = "Same Name"
+ contact_do_match = FakeContact(name, ["http://www.facebook.com/profile.php?id=123"], 1);
+ contact_no_match = FakeContact(name, [None], 2)
- data = [{'uid':'123','name':'Same Name'}]
+ data = [{'uid':'123','name':name}]
self._fake_server_response(data)
self._run_service([contact_no_match, contact_do_match])
def test_name_collision_avoided_only_one_person_matched(self):
- contact_do_match = FakeContact("Same Name", ["http://twitter.com/same_name"]);
- contact_no_match = FakeContact("Same Name", [None])
+ name = "Same Name"
+ contact_do_match = FakeContact(name, ["Contact 1"]);
+ contact_no_match = FakeContact(name, ["Contact 2"])
- data = [{'uid':'123','name':'Same Name'}]
+ data = [{'uid':'123','name':name}]
self._fake_server_response(data)
self._run_service([contact_no_match, contact_do_match])