diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7e99e36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc \ No newline at end of file diff --git a/README.md b/README.md index d04fe03..f3c4c96 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,11 @@ # Geek-Jokes ## A Flask RESTful API to get random geek jokes + +## Start Up Commands +```bash +$ pip install -r requirements.txt +$ export FLASK_APP=app.py +$ export FLASK_DEBUG=1 +$ flask run +``` \ No newline at end of file diff --git a/app.py b/app.py index 30b57e3..ffcff53 100644 --- a/app.py +++ b/app.py @@ -1,37 +1,26 @@ # -*- coding: utf-8 -*- from flask import Flask from flask_restful import Resource, Api + import joke import sys - app = Flask(__name__) api = Api(app) - -reload(sys) -sys.setdefaultencoding('utf-8') - -class APP(Resource): +class App(Resource): def get(self): return {'this is soon to become an awesome->': 'website'} - def post(self): - jk = joke.getJoke() - jk = jk.encode('ascii', 'ignore').decode('ascii') - #jk = jk.encode('utf-8') - return jk -class API(Resource): +class Api(Resource): def get(self): - jk = joke.getJoke() - jk = jk.encode('ascii', 'ignore').decode('ascii') - # jk = jk.encode('utf-8') - return jk + return joke.get_joke().encode('ascii', 'ignore').decode('ascii') + -api.add_resource(APP, '/') -api.add_resource(API, '/api') +api.add_resource(App, '/') +api.add_resource(Api, '/api') if __name__ == '__main__': app.run(debug=True) diff --git a/joke.py b/joke.py index 644ef7e..74dc5de 100644 --- a/joke.py +++ b/joke.py @@ -1,14 +1,13 @@ # -*- coding: utf-8 -*- import json -from random import * +import random -def random_digits(): - range_start = 1 - range_end = 545 - return randint(range_start, range_end) -def getJoke(): + +def random_digits(item_count): + return random.randint(0, item_count) + + +def get_joke(): with open('data.json') as data_file: data = json.load(data_file) - joke = data[random_digits()] - print joke - return joke \ No newline at end of file + return data[random_digits(len(data))]