Skip to content

Commit

Permalink
Merge d1d1d8a into 2379c56
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaOndieki committed Jun 27, 2018
2 parents 2379c56 + d1d1d8a commit 1b3bc44
Show file tree
Hide file tree
Showing 16 changed files with 722 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ python:
# command to install dependencies
install:
- pip install -r requirements.txt
- pip install nose
- pip install coveralls
- pip install coverage

# command to run tests
script: nosetests --with-coverage --cover-package=ridemyway && coveralls
Expand Down
25 changes: 6 additions & 19 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
aniso8601==2.0.1
attrs==17.4.0
certifi==2018.1.18
chardet==3.0.4
aniso8601==3.0.2
click==6.7
coverage==4.5.1
coveralls==1.3.0
docopt==0.6.2
Flask==0.12.2
Flask-JWT-Extended==3.7.1
Flask==1.0.2
Flask-JWT-Extended==3.10.0
Flask-RESTful==0.3.6
gunicorn==19.7.1
idna==2.6
gunicorn==19.8.1
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
nose==1.3.7
pluggy==0.6.0
py==1.5.2
PyJWT==1.6.0
pytest==3.4.2
pytz==2018.3
requests==2.18.4
PyJWT==1.6.4
pytz==2018.4
six==1.11.0
urllib3==1.22
Werkzeug==0.14.1
16 changes: 15 additions & 1 deletion ridemyway/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Flask
from config import config
from .api.v1.routes import v1
from flask_jwt_extended import JWTManager


# An in memory database
Expand Down Expand Up @@ -40,6 +41,19 @@ def create_app(config_name):
app = Flask(__name__)
app.database = database
app.config.from_object(config[config_name])

app.config['BUNDLE_ERRORS'] = True
app.config['JWT_SECRET_KEY'] = 'super-secret'
app.config['JWT_BLACKLIST_ENABLED'] = True
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access', 'refresh']
app.jwt = JWTManager(app)
app.blacklist = set()

@app.jwt.token_in_blacklist_loader
def check_if_token_in_blacklist(decrypted_token):
jti = decrypted_token['jti']
return jti in app.blacklist

# Register Blueprint here
app.register_blueprint(v1, url_prefix="/api/v1")

return app
7 changes: 7 additions & 0 deletions ridemyway/api/v1/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@
v1 = Blueprint('v1', __name__)
api = Api(v1)
add = api.add_resource


# Add routes here
add(r.All, '/all') # GET
add(r.Rides, '/rides') # GET, POST
add(r.Ride, '/rides/<int:rideId>') # GET
add(r.Request, '/rides/<int:rideId>/requests') # POST
130 changes: 130 additions & 0 deletions ridemyway/controllers/ride_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from ridemyway.models.ride import Ride
from datetime import datetime
from flask import current_app as app
import re
from ridemyway.utils.validators import date_has_passed


class RideController():
"""
Controls all CRUD operations of the Ride object.
"""

def create_ride(self, **Kwargs):
"""
Creates and adds a ride to the app database.
Returns:
A success status if success adding ride,
failed status otherwise.
"""
try:
ride_ids = [x for x in app.database['Rides']]
if ride_ids:
ride_id = max(ride_ids) + 1
else:
ride_id = 1
date_offered = datetime.now().strftime('%b %d %Y %H:%M%p')
self.new_ride = Ride(
ride_id=ride_id,
departure=Kwargs['departure'],
origin=Kwargs['origin'],
destination=Kwargs['destination'],
vehicle_number_plate=
Kwargs['vehicle_number_plate'],
capacity=Kwargs['capacity'],
cost=Kwargs['cost'],
date_offered=date_offered,
availability='available')
ride = self.new_ride.__dict__
app.database['Rides'][self.new_ride.ride_id] = ride
status = {
'status': 'success',
'message': 'Ride created successfully',
'attributes': {
'location':
'/api/v1/rides/' + str(ride['ride_id']),
'repr':
str(ride['ride_id']) + ' - from ' +
ride['origin'] + ' to ' +
ride['destination']
}
}
return(status)
except Exception as e:
status = {
'status': 'failed',
'message': 'Exceptions',
'meta': {
'errors': 1
},
'errors': [
{
str(type(e)): str(e)
}
]
}
return(status)

def fetch_one(self, ride_id):
try:
one_ride = app.database['Rides'][ride_id]
fetched_ride = {
'status': 'success',
'message': 'Ride fetched successfully',
'data': {
'rideId': one_ride['ride_id'],
'departure': one_ride['departure'],
'origin': one_ride['origin'],
'destination': one_ride['destination'],
'cost': one_ride['cost'],
'vehicleNumberPlate': one_ride['vehicle_number_plate'],
'capacity': one_ride['capacity'],
'dateoffered': one_ride['date_offered']
}
}
return fetched_ride, 200
except KeyError:
error_message_404 = 'Chapter 404: The Lost Resource. \
A careful and diligent search \
has been made for the desired resource, \
but it just cannot be found.'
status = {
'status': 'failed',
'message': 'NOT FOUND',
'meta': {
'errors': 1
},
'errors': [
{
404: re.sub(' +', ' ', error_message_404)
}
]
}
return status, 404

def fetch_all(self):
rides_count = 0
fetched_rides = {
'status': 'success',
'message': 'Rides fetched successfully',
'meta': {
'rides': 0
},
'data': {}
}
for key, value in app.database['Rides'].items():
if date_has_passed(value['departure']):
continue
rides_count += 1
fetched_rides['data'][key] = {
'departure': value['departure'],
'origin': value['origin'],
'destination': value['destination'],
'cost': value['cost'],
'vehicle_number_plate': value['vehicle_number_plate'],
'capacity': value['capacity'],
'dateoffered': value['date_offered']
}
fetched_rides['meta']['rides'] = rides_count
return fetched_rides
60 changes: 60 additions & 0 deletions ridemyway/controllers/ride_request_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from ridemyway.models.request import Request
from flask import current_app as app
import re
from ridemyway.utils.validators import date_has_passed


class RequestController():
"""
Controls all CRUD operations of the Request object.
"""

def create_request(self, **Kwargs):
"""
Creates and adds a request to the app database.
Returns:
A success status if success adding ride,
failed status otherwise.
"""
try:
app.database['Rides'][Kwargs['ride_id']]
request_ids = [x for x in app.database['Requests']]
if request_ids:
request_id = max(request_ids) + 1
else:
request_id = 1
self.new_request = Request(
request_id=request_id,
ride_id=Kwargs['ride_id'],
status='available'
)
request = self.new_request.__dict__
app.database['Requests'][request['request_id']] = request
status = {
'status': 'success',
'message': 'Ride request created successfully',
'attributes': {
'location':
'/api/v1/rides/' + str(request['ride_id']) + '/requests'
}
}
return status, 201
except KeyError:
error_message_404 = 'Chapter 404: The Lost Resource. \
A careful and diligent search \
has been made for the desired resource, \
but it just cannot be found.'
status = {
'status': 'failed',
'message': 'NOT FOUND',
'meta': {
'errors': 1
},
'errors': [
{
404: re.sub(' +', ' ', error_message_404)
}
]
}
return status, 404
21 changes: 21 additions & 0 deletions ridemyway/models/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Request():
"""
Creates Request objects.
**Kwargs:
ride_id: A unique identifier of the ride the request is
being made to.
request_id: A unique identifier for the request.
status: Status of the request.
"""

def __init__(self, **kwargs):
"""
Request object initializer.
Returns:
Object
"""
self.request_id = kwargs['request_id']
self.ride_id = kwargs['ride_id']
self.status = kwargs['status']
36 changes: 36 additions & 0 deletions ridemyway/models/ride.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Ride():
"""
Creates Ride objects.
**Kwargs:
ride_id: A unique identifier for the ride.
departure: Date and time the ride is to take place.
origin: Place where the ride starts.
destination: Where the ride should end.
vehicle_number_plate: The number plate of the vehicle.
capacity: Maximum number of passengers the ride will accept.
cost: The cost of receiving the ride.
date_offered: The time this ride offer was created.
availability: Status of the ride.
"""

def __init__(self, **kwargs):
"""
Ride object initializer.
Returns:
Object
"""
self.ride_id = kwargs['ride_id']
self.departure = kwargs['departure']
self.origin = kwargs['origin']
self.destination = kwargs['destination']
self.vehicle_number_plate = kwargs['vehicle_number_plate']
self.capacity = kwargs['capacity']
self.cost = kwargs['cost']
self.date_offered = kwargs['date_offered']
self.availability = kwargs['availability']

def __repr__(self):
return(str(self.ride_id) + ' - from ' +
self.origin + ' to ' + self.destination)
Loading

0 comments on commit 1b3bc44

Please sign in to comment.