Permalink
Browse files

Initial commit

  • Loading branch information...
1 parent 704cbf2 commit fe8e8b3f6b51ba9c0b6859f51e46fbf2b9db30e2 @jgorset committed May 4, 2012
Showing with 218 additions and 2 deletions.
  1. +2 −0 Makefile
  2. +17 −0 README.rst
  3. +4 −2 facebook/__init__.py
  4. +32 −0 facebook/entity.py
  5. +30 −0 facebook/session.py
  6. +20 −0 facebook/user.py
  7. +26 −0 tests/test_entity.py
  8. +30 −0 tests/test_session.py
  9. +57 −0 tests/test_user.py
View
@@ -0,0 +1,2 @@
+test:
+ nosetests
View
@@ -6,6 +6,23 @@ About
Facebook makes it even easier to interact with Facebook's Graph API.
+Usage
+-----
+
+::
+
+ import facebook
+
+ with facebook.session(oauth_token) as session:
+ user = session.User('johannes.gorset')
+
+ user.id #=> 586052336
+ user.first_name #=> 'Johannes'
+ user.last_name #=> 'Gorset'
+ user.name #=> 'Johannes Gorset'
+ user.link #=> 'http://facebook.com/johannesgorset'
+ user.permissions #=> ['user_location', 'user_relationships']
+
Installation
------------
View
@@ -1,3 +1,5 @@
-"""TODO: Document your package."""
-
from .version import __version__
+
+from session import Session, session
+from entity import Entity
+from user import User
View
@@ -0,0 +1,32 @@
+from facepy import GraphAPI
+
+class Entity(object):
+ """Entities are the base class for anything on Facebook."""
+
+ oauth_token = None
+ """A string describing an OAuth token."""
+
+ def __init__(self, id, oauth_token=None):
+ """
+ Get the entity with the given id.
+
+ :param id: An integer or string describing the Facebook id of the entity.
+ :param oauth_token: A string describing an OAuth token.
+ """
+ self.oauth_token = oauth_token
+ self.id = id
+
+ def load(self):
+ self.response = self.graph.get('%s' % self.id)
+
+ @property
+ def graph(self):
+ return GraphAPI(self.oauth_token)
+
+ def __getattr__(self, missing_attribute):
+ for attribute, key, parser in self.attributes:
+ if attribute == missing_attribute:
+ if not hasattr(self, 'response'):
+ self.load()
+
+ return parser(self.response[key])
View
@@ -0,0 +1,30 @@
+"""
+facebook.session
+~~~~~~~~~~~~~~~~
+
+This module provides a Session object to manage and persist settings across
+queries to the Facebook API.
+"""
+
+from .user import Entity, User
+
+class Session(object):
+ """A Facebook session."""
+
+ def __init__(self, oauth_token=None):
+ self.oauth_token = oauth_token
+
+ def User(self, *args, **kwargs):
+ kwargs['oauth_token'] = self.oauth_token
+
+ return User(*args, **kwargs)
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ pass
+
+def session(*args, **kwargs):
+ """Returns a :class:`Session` for context management."""
+ return Session(*args, **kwargs)
View
@@ -0,0 +1,20 @@
+from entity import Entity
+
+class User(Entity):
+ """User instances represent Facebook users."""
+
+ attributes = [
+ ('facebook_id', 'id', int),
+ ('username', 'username', str),
+ ('first_name', 'first_name', str),
+ ('last_name', 'last_name', str),
+ ('name', 'name', str),
+ ('locale', 'locale', str),
+ ('gender', 'gender', str),
+ ('link', 'link', str)
+ ]
@tomwys

tomwys May 4, 2012

I'm not sure if this is good approach for that. You loose supports from IDEs that way (and any other kind of code inspection).

Maybe we can do something like that:

class User(Entity):
    facebook_id = Field('id', int) # or IntField('id')
    ....

And make Field a descriptor (http://docs.python.org/reference/datamodel.html#descriptors).

@jgorset

jgorset May 5, 2012

Owner

Yes, that would be much cleaner. Thanks!

+
+ @property
+ def permissions(self):
+ response = self.graph.get('%s/permissions' % self.id)
+ return map(lambda x: x[0], response['data'][0].items())
View
@@ -0,0 +1,26 @@
+"""Tests for ``facebook.entity``."""
+
+from nose.tools import *
+from mock import patch, Mock as mock
+from facepy import GraphAPI
+
+from facebook import Entity
+
+@patch.object(GraphAPI, 'get')
+def test_get(mock):
+ mock.return_value = {
+ 'username': 'johannesgorset',
+ 'first_name': 'Johannes',
+ 'last_name': 'Gorset',
+ 'name': 'Johannes Gorset',
+ 'locale': 'en_GB',
+ 'gender': 'male',
+ 'link': 'http://facebook.com/johannesgorset',
+ 'id': '586052336'
+ }
+
+ entity = Entity('johannes.gorset')
+
+ entity.load()
+
+ mock.assert_called_with('johannes.gorset')
View
@@ -0,0 +1,30 @@
+"""Tests for ``facebook.session``."""
+
+from nose.tools import *
+from mock import patch, Mock as mock
+from facepy import GraphAPI
+
+import facebook
+
+@patch.object(GraphAPI, 'get')
+def test_session(mock):
+ mock.return_value = {
+ 'username': 'johannesgorset',
+ 'first_name': 'Johannes',
+ 'last_name': 'Gorset',
+ 'name': 'Johannes Gorset',
+ 'locale': 'en_GB',
+ 'gender': 'male',
+ 'link': 'http://facebook.com/johannesgorset',
+ 'id': '586052336'
+ }
+
+ oauth_token = 'abcdefghijklmnopqrstuvwxyz'
+
+ with facebook.session(oauth_token) as session:
+ user = session.User('johannes.gorset')
+
+ assert_equal(user.oauth_token, oauth_token)
+
+ user = facebook.User('johannes.gorset')
+ assert_equal(user.oauth_token, None)
View
@@ -0,0 +1,57 @@
+"""Tests for ``facebook.user``."""
+
+from nose.tools import *
+from mock import patch, Mock as mock
+from facepy import GraphAPI
+
+import facebook
+from facebook import User
+
+@patch.object(GraphAPI, 'get')
+def test_get(mock):
+ mock.return_value = {
+ 'username': 'johannesgorset',
+ 'first_name': 'Johannes',
+ 'last_name': 'Gorset',
+ 'name': 'Johannes Gorset',
+ 'locale': 'en_GB',
+ 'gender': 'male',
+ 'link': 'http://facebook.com/johannesgorset',
+ 'id': '586052336'
+ }
+
+ user = User('johannes.gorset')
+
+ user.load()
+
+ mock.assert_called_with('johannes.gorset')
+
+ assert_equal(user.username, 'johannesgorset')
+ assert_equal(user.first_name, 'Johannes')
+ assert_equal(user.last_name, 'Gorset')
+ assert_equal(user.name, 'Johannes Gorset')
+ assert_equal(user.locale, 'en_GB')
+ assert_equal(user.gender, 'male')
+ assert_equal(user.link, 'http://facebook.com/johannesgorset')
+ assert_equal(user.facebook_id, 586052336)
+
+@patch.object(GraphAPI, 'get')
+def test_permissions(mock):
+ mock.return_value = {
+ 'data': [
+ {
+ 'user_location': 1,
+ 'user_relationships': 1
+ }
+ ]
+ }
+
+ with facebook.session('<token>') as session:
+ user = session.User('johannes.gorset')
+
+ permissions = user.permissions
+
+ mock.assert_called_with('johannes.gorset/permissions')
+
+ assert_in('user_location', permissions)
+ assert_in('user_relationships', permissions)

0 comments on commit fe8e8b3

Please sign in to comment.