Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test: | ||
nosetests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
] | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
@property | ||
def permissions(self): | ||
response = self.graph.get('%s/permissions' % self.id) | ||
return map(lambda x: x[0], response['data'][0].items()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
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:
And make
Field
a descriptor (http://docs.python.org/reference/datamodel.html#descriptors).