Updated rulings_object to the new refactor scheme.

This commit is contained in:
2018-03-03 18:15:55 -05:00
parent f0cda14141
commit 64e9cf0632
1 changed files with 14 additions and 22 deletions

View File

@ -59,62 +59,54 @@ class RulingsObject(object):
if self.scryfallJson['object'] == 'error': if self.scryfallJson['object'] == 'error':
raise Exception(self.scryfallJson['details']) raise Exception(self.scryfallJson['details'])
def __checkForKey(self, key): def _checkForKey(self, key):
try: try:
return self.scryfallJson[key] return self.scryfallJson[key]
except KeyError: except Exception:
return None raise KeyError('This object has no key \'{}\''.format(key))
def __checkForTupleKey(self, parent, num, key): def _checkForTupleKey(self, parent, num, key):
try: try:
return self.scryfallJson[parent][num][key] return self.scryfallJson[parent][num][key]
except KeyError: except Exception:
return None raise KeyError('This ruling has no key \'{}\''.format(key))
def object(self): def object(self):
if self.__checkForKey('object') is None: self._checkForKey('object')
raise KeyError('This object has no key \'object\'')
return self.scryfallJson['object'] return self.scryfallJson['object']
def has_more(self): def has_more(self):
if self.__checkForKey('has_more') is None: self._checkForKey('has_more')
raise KeyError('This object has no key \'has_more\'')
return self.scryfallJson['has_more'] return self.scryfallJson['has_more']
def data(self): def data(self):
if self.__checkForKey('data') is None: self._checkForKey('data')
raise KeyError('This object has no key \'data\'')
return self.scryfallJson['data'] return self.scryfallJson['data']
def data_length(self): def data_length(self):
if self.__checkForKey('data') is None: self._checkForKey('data')
raise KeyError('This object has no key \'data\'')
return len(self.scryfallJson['data']) return len(self.scryfallJson['data'])
def ruling_object(self, num): def ruling_object(self, num):
if self.__checkForTupleKey('data', num, 'object') is None: self._checkForTupleKey('data', num, 'object')
raise KeyError('This ruling has no key \'object\'')
return self.scryfallJson['data'][num]['object'] return self.scryfallJson['data'][num]['object']
def ruling_source(self, num): def ruling_source(self, num):
if self.__checkForTupleKey('data', num, 'source') is None: self._checkForTupleKey('data', num, 'source')
raise KeyError('This ruling has no key \'source\'')
return self.scryfallJson['data'][num]['source'] return self.scryfallJson['data'][num]['source']
def ruling_published_at(self, num): def ruling_published_at(self, num):
if self.__checkForTupleKey('data', num, 'published_at') is None: self._checkForTupleKey('data', num, 'published_at')
raise KeyError('This ruling has no key \'published_at\'')
return self.scryfallJson['data'][num]['published_at'] return self.scryfallJson['data'][num]['published_at']
def ruling_comment(self, num): def ruling_comment(self, num):
if self.__checkForTupleKey('data', num, 'comment') is None: self._checkForTupleKey('data', num, 'comment')
raise KeyError('This ruling has no key \'comment\'')
return self.scryfallJson['data'][num]['comment'] return self.scryfallJson['data'][num]['comment']