Created
June 12, 2017 22:14
-
-
Save seansu4you87/de612d8bf020f5aade29fd8e6ca498d2 to your computer and use it in GitHub Desktop.
Refactoring Interview v0.0.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import uuid | |
class Cart: | |
@classmethod | |
def get(cls, id): | |
return Cart() | |
def __init__(self): | |
self.creator = Consumer() | |
self.promotion = "$5 off" | |
self.store = Store() | |
self.subtotal = 5412 | |
class Order: | |
def __init__(self, cart, charge): | |
self.cart = cart | |
self.charge = charge | |
self.email_message = "Hi, we have processed your order!" | |
class Delivery: | |
def __init__(self, contents): | |
self.contents = contents | |
class Consumer: | |
def __init__(self): | |
self.name = "Sean Yu" | |
self.default_card = "4242424242424242" | |
self.email = "[email protected]" | |
class Store: | |
def __init__(self): | |
self.order_protocol = "FAX" | |
def is_open(self): | |
return True | |
def log(str): | |
print(str) | |
class Curl: | |
MOCKS = { | |
"doordash.com/dispatch/get_order_protocol_is_killed": lambda (_): False, | |
"doordash.com/fee/get_fee": lambda (_): 599, | |
"siftscience.com/get_checkout_fraud": lambda (_): False, | |
"stripe.com/charge": lambda (params): { | |
"status": 200, | |
"charge": { | |
"id": uuid.uuid4(), | |
"card": params["card"], | |
"amount": params["amount"], | |
} | |
}, | |
"mailchimp.com/emails": lambda (params): log("sending to {}:\n\n{}\n".format(params["to"], params["message"])), | |
} | |
@classmethod | |
def get(cls, url, params): | |
print("Network I/O, GET {} with {}".format(url, params)) | |
time.sleep(0.5) | |
return Curl.MOCKS[url](params) | |
@classmethod | |
def post(cls, url, params): | |
print("Network I/O, POST {} with {}".format(url, params)) | |
time.sleep(0.5) | |
return Curl.MOCKS[url](params) | |
def checkout(cart_id, | |
card, | |
delivery_fee, | |
ip_address, | |
is_asap, | |
is_consumer_pickup, | |
is_depot): | |
cart = Cart.get(cart_id) | |
consumer = cart.creator | |
if not card: | |
card = consumer.default_card | |
if card is None: | |
print("Cart is submitted without any credit card") | |
return False | |
is_fraud = Curl.get("siftscience.com/get_checkout_fraud", { | |
"cart": cart, | |
"consumer": consumer, | |
"card": card, | |
"is_asap": is_asap, | |
"ip_address": ip_address, | |
}) | |
if is_fraud: | |
print("Fraud!!!") | |
return False | |
store = cart.store | |
if delivery_fee is None: | |
delivery_fee = Curl.get("doordash.com/fee/get_fee", {"cart": cart}) | |
charge = Curl.post("stripe.com/charge", { | |
"card": card, | |
"amount": cart.subtotal, | |
}) | |
order = Order(cart, charge) | |
cart.charge = charge | |
Curl.post("mailchimp.com/emails", { | |
"message": order.email_message, | |
"to": order.cart.creator.email, | |
}) | |
return True | |
if __name__ == "__main__": | |
checkout(1234, "1234432156788765", 399, "127.94.0.2", True, False, False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment