Skip to content

Commit 9e7d4e6

Browse files
committed
API change: Added gravatar_id in User model
Also fix some bugs, update test and PEP8
1 parent 0664d60 commit 9e7d4e6

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

github3/converters.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,24 @@ def loads(self, raw_resource):
7575
idl = self.model.idl()
7676
attrs.update(
7777
{attr: raw_resource[attr] for attr in idl.get('strs',())
78-
if raw_resource.get(attr)})
78+
if attr in raw_resource})
7979
attrs.update(
8080
{attr: raw_resource[attr] for attr in idl.get('ints',())
81-
if raw_resource.get(attr)})
81+
if attr in raw_resource})
8282
attrs.update(
8383
{attr: self._parse_date(raw_resource[attr])
84-
for attr in idl.get('dates',()) if raw_resource.get(attr)})
84+
for attr in idl.get('dates',()) if attr in raw_resource})
8585
attrs.update(
8686
{attr: raw_resource[attr] for attr in idl.get('bools',())
87-
if raw_resource.get(attr)})
87+
if attr in raw_resource})
8888
attrs.update(
8989
{attr: self._parse_map(model, raw_resource[attr])
9090
for attr, model in idl.get('maps',{}).items()
91-
if raw_resource.get(attr)})
91+
if attr in raw_resource})
9292
attrs.update(
9393
{attr: self._parse_collection_map(model, raw_resource[attr])
9494
for attr, model in idl.get('collection_maps',{}).items()
95-
if raw_resource.get(attr)})
95+
if attr in raw_resource})
9696

9797
return self.model(attrs)
9898

github3/models/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ def __init__(self, attrs=None):
1414
setattr(self, attr, value)
1515
super(BaseResource, self).__init__()
1616

17+
def __len__(self):
18+
return len(self.__dict__)
19+
1720
@classmethod
1821
def idl(self):
1922
raise NotImplementedError('Each model need subcass that method')

github3/models/user.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from .base import BaseResource
77

8+
89
class Plan(BaseResource):
910
"""Github Plan object model."""
1011

@@ -18,6 +19,7 @@ def idl(self):
1819
def __repr__(self):
1920
return '<Plan %s>' % self.name
2021

22+
2123
class Key(BaseResource):
2224
"""Github Key object model."""
2325

@@ -31,20 +33,23 @@ def idl(self):
3133
def __repr__(self):
3234
return '<Key %s>' % self.title
3335

36+
3437
class User(BaseResource):
3538
"""Github User object model."""
3639

3740
@classmethod
3841
def idl(self):
3942
return {
40-
'strs': ['login','avatar_url', 'url', 'name', 'company', 'blog',
41-
'location', 'email', 'bio', 'html_url', 'type'],
43+
'strs': [
44+
'login', 'avatar_url', 'gravatar_id', 'url', 'name',
45+
'company', 'blog', 'location', 'email', 'bio', 'html_url',
46+
'type'],
4247
'ints': [
4348
'id', 'public_repos', 'public_gists', 'followers', 'following',
4449
'total_private_repos', 'owned_private_repos', 'private_gists',
4550
'disk_usage', 'collaborators'],
4651
'maps': {'plan': Plan},
47-
'dates': ['created_at',],
52+
'dates': ['created_at', ],
4853
'bools': ['hireable', ],
4954
}
5055

@@ -54,6 +59,7 @@ def __repr__(self):
5459
#def handler(self):
5560
# return self._gh.user_handler(self.login, force=True)
5661

62+
5763
class AuthUser(User):
5864
"""Github Authenticated User object model."""
5965

@@ -62,4 +68,3 @@ class AuthUser(User):
6268

6369
def __repr__(self):
6470
return '<AuthUser %s>' % self.login
65-

github3/tests/converters_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
from datetime import datetime
88

99
API_STUB = {
10-
'test_str': 'string', 'test_int': 1,
11-
'test_date': '2008-01-14T04:33:35Z', 'test_bool': True,
10+
'test_str': 'string',
11+
'test_int': 1,
12+
'test_date': '2008-01-14T04:33:35Z',
13+
'test_bool': True,
1214
'map': {'test_str': 'string'},
1315
'dict_map': {
1416
'map1': {
@@ -55,6 +57,7 @@ def setUp(self):
5557

5658
def test_loads(self):
5759
parsed_model = self.modelizer.loads(API_STUB)
60+
self.assertEquals(len(parsed_model), len(API_STUB))
5861
self.assertEquals(parsed_model.test_str, 'string')
5962
self.assertEquals(parsed_model.test_int, 1)
6063
self.assertEquals(

github3/tests/fixtures.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"login": "octocat",
66
"id": 1,
77
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
8+
"gravatar_id": "somehexcode",
89
"url": "https://api.github.com/users/octocat",
910
"name": "monalisa octocat",
1011
"company": "GitHub",
@@ -24,24 +25,29 @@
2425

2526
GET_LINK = '<https://api.github.com/gists/public?page=2>; rel="next", \
2627
<https://api.github.com/gists/public?page=5>; rel="last"'
28+
2729
GET_RESOURCES = [
2830
{'login': 'octocat'},
2931
{'login': 'octocat'}
3032
]
33+
3134
GET_SHORT_USERS = [
3235
{
3336
"login": "octocat",
3437
"id": 1,
3538
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
39+
"gravatar_id": "somehexcode",
3640
"url": "https://api.github.com/users/octocat"
3741
},
3842
{
3943
"login": "octocat",
4044
"id": 1,
4145
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
46+
"gravatar_id": "somehexcode",
4247
"url": "https://api.github.com/users/octocat"
4348
},
4449
]
50+
4551
GET_SHORT_ORGS = [
4652
{
4753
"login": "github",
@@ -63,6 +69,7 @@
6369
"login": "octocat",
6470
"id": 1,
6571
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
72+
"gravatar_id": "somehexcode",
6673
"url": "https://api.github.com/users/octocat"
6774
},
6875
"name": "Hello-World",
@@ -80,6 +87,7 @@
8087
"created_at": "2011-01-26T19:01:12Z"
8188
}
8289
]
90+
8391
GET_SHORT_GISTS = [
8492
{
8593
"url": "https://api.github.com/gists/1",
@@ -90,6 +98,7 @@
9098
"login": "octocat",
9199
"id": 1,
92100
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
101+
"gravatar_id": "somehexcode",
93102
"url": "https://api.github.com/users/octocat"
94103
},
95104
"files": {

0 commit comments

Comments
 (0)