/takeaway-challenge

Use the Twilio Gem to order food

Primary LanguageRubyOtherNOASSERTION

TakeAway Challenge (week 2) - Debora Piu

This weekend challenge's instructions are summarised below:

  • create a TakeAway with a menu
  • show the menu
  • implement placing orders
  • check total cost
  • send SMS confirmation with delivery time

    New functionality to implement:

    If the cost is correct the customer is sent a text saying that the order was placed successfully and that it will be delivered in 1 hour. The text sending functionality should be implemented using Twilio API. Use mocks and/or stubs, as necessary to not to send texts when your tests are run.

    My approach Build Status

    NOUN VERB
    Takeaway @sum_total,
    show_menu(), place_order(), send_confirmation()
    Menu @menu = [],
    add(), to_string()
    Dish @dish_name, @price

    As the weekly goal was getting familiar with dependency injection, I spend quite a long time implementing a Dish class, called from the Menu class via methods like menu_add(dish, price), show_menu.
    The program was in working conditions until it got complicated retrieving the dish prices from the Dish class, via the Menu class, while placing orders in the Takeaway class.
    Therefore, after exercising on the Dish class to better understand the weekly goal on dependency injection, I applied the YAGNI principle to my program and got rid of the Dish class and all of the methods in the Menu class, hardcoding a menu in a Hash.

    NOUN VERB
    Takeaway @sum_total,
    show_menu(), place_order(), send_confirmation()
    Menu @menu = {}
    Twilio send_confirmation()


    Place_order method was implemented and Twilio API was my next challenge. After reading Twilio docs and creating a module in working order, I had to mock the send_confirmation method under place_order, not to send SMS when running RSpec.

    After Monday's review

    NOUN VERB
    Takeaway show_menu(), place_order(), checkout()
    Display to_string()
    Menu @menu,
    add()
    Dish @dish_name, @price
    Order @total_cost, @new_order,
    place_order(), update_total_cost(), find_price(),
    complete_order(), delivery(), clear_order()
    SMS @client,
    set_delivery_time(), send_confirmation()

    After peer and group review on Monday morning, I decided to retrieve my old code and debug it to make the actual Menu class dynamic. The Menu class now creates instances of Dish class via add() and stores the instances in an Array. Order class has also been restored and all methods calculating and storing the actual order have been moved.

    Takeaway class still runs the program but stores only methods which call other classes actually running them.
    show_menu calls the Display class which calls Menu(via Order - I'll come to this later on), which calls Dish.
    place_order and checkout call the Order class.

    Takeaway initializes Order.new and Display.new, however Menu.new has been initialized in the Order class as it came easier once calling dish.price. If Menu.new would be initialized via Takeaway, the Order class would need to either require Takeaway or Menu to get to dish.price, however this dependency injection would create a new instance and not calling the one used to add() dishes. I momentarily fixed this by initializing Menu.new in the Order class and calling it from Takeaway to then send it to Display. A better solution should come with more experience.

    The above paragraph was the actual challenge that made me to switch to hard-coded menu over the weekend, to get through the rest of the tasks. find_price in the Order class works now, however I am sure that there is a better code for this functionality.

    Twilio has been refactored into a class SMS and stubbed the same way as before in RSpec. Creating a Fake_client class to call in RSpec, will be a solution once I gain more experience.