Skip to content

Commit

Permalink
feat: Version 1.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
Keda87 committed Jun 2, 2016
1 parent 07f0daa commit e32c892
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 46 deletions.
41 changes: 32 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,52 @@ cn = ChunkNorris()
cn.random()

# Get multiple random jokes.
cn.random(total=5)
data = cn.random(total=5)
for i in data:
print(i.id)
print(i.joke)
print(i.categories)

# Get random jokes with manipulate actor name (first name or last name).
cn.random(first_name='John')
cn.random(last_name='Doe')
cn.random(first_name='John', last_name='Doe')
data = cn.random(first_name='John')
data = cn.random(last_name='Doe')
data = cn.random(first_name='John', last_name='Doe')
print(data.id)
print(data.joke)
print(data.categories)

# Get random jokes filter by categories (must be an iterable).
cn.random(categories=['nerdy', 'geeks'])
data = cn.random(categories=['nerdy', 'geeks'])
print(data.id)
print(data.joke)
print(data.categories)

# Get random jokes filter exclude by categories (must be an iterable).
cn.random(exclude_categories=['nerdy'])
data = cn.random(exclude_categories=['nerdy'])
print(data.id)
print(data.joke)
print(data.categories)

# Get jokes by specific ID.
cn.get_jokes_by_id(15)
data = cn.get_jokes_by_id(15)
print(data.id)
print(data.joke)
print(data.categories)

# Get total jokes count.
cn.get_jokes_count()
data = cn.get_jokes_count()
print(data) # int

# Get all jokes categories.
cn.get_jokes_categories()
data = cn.get_jokes_categories()
for i in data:
print i # string

# Get entire jokes data.
cn.get_all_jokes()
for i in data:
print(i.id)
print(i.joke)
print(i.categories)
```

58 changes: 51 additions & 7 deletions chuck.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class NorrisException(Exception):
pass


class Norris(object):

def __init__(self):
self.id = None
self.joke = None
self.categories = None


class ChuckNorris(object):

def __init__(self):
Expand Down Expand Up @@ -48,25 +56,61 @@ def random(self, total=None, first_name=None, last_name=None,

try:
response = requests.get(url, params=params)
return response.json()
data = response.json()
data = data['value']
if isinstance(data, list):
results = []
for i in data:
norris = Norris()
norris.id = i['id']
norris.joke = i['joke']
norris.categories = i['categories']
results.append(norris)
return results

norris = Norris()
norris.id = data['id']
norris.joke = data['joke']
norris.categories = data['categories']
return norris
except ValueError:
return None
return Norris()

def get_jokes_by_id(self, joke_id):
if isinstance(joke_id, str):
raise NorrisException('ID is not a number.')
url = self.base_url + '%d/' % joke_id
response = requests.get(url)
return response.json()
try:
response = requests.get(url)
data = response.json()
data = data['value']
norris = Norris()
norris.id = data['id']
norris.joke = data['joke']
norris.categories = data['categories']
return norris
except ValueError:
return Norris()

def get_jokes_count(self):
url = self.base_url + 'count/'
response = requests.get(url)
return response.json()
return response.json()['value']

def get_jokes_categories(self):
url = self.base_url.replace('jokes/', 'categories')
response = requests.get(url)
return response.json()
return response.json()['value']

def get_all_jokes(self):
response = requests.get(self.base_url)
return response.json()
data = response.json()
data = data['value']
results = []
for i in data:
norris = Norris()
norris.id = i['id']
norris.joke = i['joke']
norris.categories = i['categories']
results.append(norris)
return results
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='chuck-norris-python',
version='0.3',
version='1.0',
packages=find_packages(exclude=['contrib', 'docs', 'tests*']),
license='MIT',
long_description='Python wrapper for "The Internet Chuck Norris Database" http://www.icndb.com/api/',
Expand Down
55 changes: 26 additions & 29 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,47 @@ def setUp(self):

def test_retrieve_jokes_random(self):
data = self.cn.random()
self.assertTrue('type' in data)
self.assertEqual(data['type'], 'success')
self.assertTrue('value' in data)
self.assertTrue('id' in data['value'])
self.assertTrue('joke' in data['value'])
self.assertTrue('categories' in data['value'])
self.assertIsNotNone(data)
self.assertIsNotNone(data.id)
self.assertIsNotNone(data.joke)
self.assertIsNotNone(data.categories)
self.assertTrue(isinstance(data.categories, list))

def test_retrieve_jokes_by_id(self):
data = self.cn.get_jokes_by_id(15)
self.assertTrue('type' in data)
self.assertEqual(data['type'], 'success')
self.assertTrue('value' in data)
self.assertTrue('id' in data['value'])
self.assertTrue('joke' in data['value'])
self.assertTrue('categories' in data['value'])
self.assertEqual(data['value']['joke'], 'When Chuck Norris goes to donate blood, he declines the syringe, and instead requests a hand gun and a bucket.')
self.assertIsNotNone(data)
self.assertIsNotNone(data.id)
self.assertIsNotNone(data.joke)
self.assertIsNotNone(data.categories)
self.assertTrue(isinstance(data.categories, list))
self.assertEqual(data.id, 15)
self.assertEqual(data.joke, 'When Chuck Norris goes to donate blood, he declines the syringe, and instead requests a hand gun and a bucket.')

def test_retrieve_jokes_categories(self):
data = self.cn.get_jokes_categories()
self.assertTrue('type' in data)
self.assertTrue('value' in data)
self.assertTrue(hasattr(data['value'], '__iter__'))
self.assertTrue(isinstance(data, list))

def test_retrieve_jokes_count(self):
data = self.cn.get_jokes_count()
self.assertTrue('type' in data)
self.assertEqual(data['type'], 'success')
self.assertTrue('value' in data)
self.assertTrue(isinstance(data['value'], int))
self.assertTrue(isinstance(data, int))

def test_retrieve_jokes_random_3_records(self):
data = self.cn.random(total=3)
self.assertTrue('type' in data)
self.assertEqual(data['type'], 'success')
self.assertTrue('value' in data)
self.assertTrue(hasattr(data['value'], '__iter__'))
self.assertEqual(len(data['value']), 3)
self.assertTrue(isinstance(data, list))
self.assertEqual(len(data), 3)
for i in data:
self.assertIsNotNone(i)
self.assertIsNotNone(i.id)
self.assertIsNotNone(i.joke)
self.assertIsNotNone(i.categories)

def test_retrieve_jokes_random_limit_category(self):
data = self.cn.random(categories=['nerdy'])
self.assertTrue('type' in data)
self.assertEqual(data['type'], 'success')
self.assertTrue('value' in data)
self.assertEqual(data['value']['categories'][0], 'nerdy')
self.assertIsNotNone(data)
self.assertIsNotNone(data.id)
self.assertIsNotNone(data.joke)
self.assertIsNotNone(data.categories)
self.assertEqual(data.categories[0], 'nerdy')


if __name__ == '__main__':
Expand Down

0 comments on commit e32c892

Please sign in to comment.