Skip to content

Commit fbd0469

Browse files
committed
Gist public handler completed
1 parent 3547813 commit fbd0469

File tree

3 files changed

+87
-5
lines changed

3 files changed

+87
-5
lines changed

github3/handlers/gists.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,55 @@
11
#!/usr/bin/env python
22
# -*- encoding: utf-8 -*-
33

4-
from .base import Handler
5-
from .. import models
4+
from .base import Handler, MimeTypeMixin
5+
from github3 import models
66

77

8-
class Gist(Handler):
9-
""" Gist handler """
8+
class Gist(Handler, MimeTypeMixin):
9+
""" Gist handler with public access """
1010

1111
prefix = 'gists'
1212

1313
def __repr__(self):
1414
return '<Gist handler>'
1515

16+
def all_gists(self, limit=None):
17+
""" Return all public gists
18+
19+
NOTE: It returns all gists in github environment. Maybe you
20+
want to use `limit` parameter
21+
"""
22+
23+
return self._get_resources('', model=models.Gist, limit=limit)
24+
1625
def get(self, gist_id):
17-
""" Return gist """
26+
""" Return gist
27+
28+
param `gist_id`: Gist id
29+
"""
1830

1931
return self._get_resource(gist_id, model=models.Gist)
2032

33+
def get_comments(self, gist_id, limit=None):
34+
""" Return gist's comments
35+
36+
param `gist_id`: Gist id
37+
param `limit`: Number of comments
38+
"""
39+
40+
return self._get_resources('%s/comments' % gist_id,
41+
model=models.GistComment, limit=limit,
42+
headers=self.mime_header())
43+
44+
def get_comment(self, comment_id):
45+
""" Return gist's comment
46+
47+
param `comment_id`: Comment id
48+
"""
49+
50+
return self._get_resource('comments/%s' % comment_id,
51+
model=models.GistComment, headers=self.mime_header())
52+
2153

2254
class AuthGist(Gist):
2355

github3/tests/get_handlers_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ def test_get_gists(self):
2626
auth_gists = self.auth_gh.gists
2727

2828
self.assertIsInstance(anom_gists, handlers.gists.Gist)
29+
self.assertEquals(anom_gists.prefix, 'gists')
2930
self.assertIsInstance(auth_gists, handlers.gists.AuthGist)
31+
self.assertEquals(anom_gists.prefix, 'gists')
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
from unittest import TestCase
5+
from mock import Mock, patch
6+
from github3 import api
7+
from github3.models import Gist, GistComment
8+
from github3.handlers.base import Handler
9+
10+
11+
class TestGistHandler(TestCase):
12+
13+
def setUp(self):
14+
self.gh = api.Github()
15+
self.handler = self.gh.gists
16+
17+
@patch.object(Handler, '_get_resources')
18+
def test_get_gists(self, get):
19+
gists = self.handler.all_gists()
20+
get.assert_called_with('', model=Gist, limit=None)
21+
22+
@patch.object(Handler, '_get_resource')
23+
def test_get(self, get):
24+
gist = self.handler.get(1)
25+
get.assert_called_with(1, model=Gist)
26+
27+
@patch.object(Handler, '_get_resources')
28+
def test_get_comments(self, get):
29+
comments = self.handler.get_comments(1)
30+
get.assert_called_with('1/comments', model=GistComment, limit=None,
31+
headers=None)
32+
33+
@patch.object(Handler, '_get_resource')
34+
def test_get_comment(self, get):
35+
comment = self.handler.get_comment(1)
36+
get.assert_called_with('comments/1', model=GistComment, headers=None)
37+
38+
class TestAuthGistHandler(TestCase):
39+
40+
def setUp(self):
41+
self.gh = api.Github('test', 'pass')
42+
self.handler = self.gh.gists
43+
44+
def test_inherit(self):
45+
self.assertTrue(hasattr(self.handler, 'get'))
46+
self.assertTrue(hasattr(self.handler, 'get_comments'))
47+
self.assertTrue(hasattr(self.handler, 'get_comment'))
48+

0 commit comments

Comments
 (0)