Added GistComment support

This commit is contained in:
Kenneth Reitz
2011-03-31 04:31:19 -04:00
parent ebc788c569
commit bb430d939a
+51 -3
View File
@@ -32,7 +32,7 @@ import cStringIO
import os.path
import urllib2
from dateutil.parser import parse as dtime
from datetime import datetime
try:
import simplejson as json
@@ -67,13 +67,17 @@ class Gist(object):
self.epic_embed_url = url + '.pibb'
self.json_url = url + '.json'
self.post_url = GIST_BASE % 'gists/%s' % self.id
self.comments = []
def __repr__(self):
return '<gist %s>' % self.id
def __getattribute__(self, name):
"""Get attributes, but only if needed."""
# Only make external API calls if needed
if name in ('owner', 'description', 'created_at', 'public',
'files', 'filenames', 'repo'):
'files', 'filenames', 'repo', 'comments'):
if not hasattr(self, '_meta'):
self._meta = self._get_meta()
@@ -103,7 +107,14 @@ class Gist(object):
setattr(self, key, value)
elif key == 'created_at':
# Attach datetime
setattr(self, key, dtime(value))
datetime.strptime(value[:-6], '%Y/%m/%d %H:%M:%S')
elif key == 'comments':
_comments = []
for comment in value:
c = GistComment().from_api(comment)
_comments.append(c)
setattr(self, 'comments', _comments)
else:
# Attach properties to object
setattr(self, key, unicode(value))
@@ -214,3 +225,40 @@ class Gists(object):
# Return a list of Gist objects
return [Gist(json=g)
for g in json.load(urllib2.urlopen(_url))['gists']]
class GistComment(object):
"""Gist comments."""
def __init__(self):
self.body = None
self.created_at = None
self.gravatar_id = None
self.id = None
self.updated_at = None
self.user = None
def __repr__(self):
return '<gist-comment %s>' % self.id
@staticmethod
def from_api(jsondict):
"""Returns new instance of GistComment containing given api dict."""
comment = GistComment()
comment.body = jsondict.get('body', None)
comment.created_at = datetime.strptime(jsondict.get('created_at')[:-6], '%Y/%m/%d %H:%M:%S')
comment.gravatar_id = jsondict.get('gravatar_id', None)
comment.id = jsondict.get('id', None)
comment.updated_at = datetime.strptime(jsondict.get('updated_at')[:-6], '%Y/%m/%d %H:%M:%S')
comment.user = jsondict.get('user', None)
return comment