Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Luiza's Takeaway #2226

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/**/.DS_Store
/coverage

.env

# Local cache of Rubocop remote config
.rubocop-*
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ end

group :development, :test do
gem 'rubocop', '1.20'
gem 'twilio-ruby'
gem 'dotenv-rails'
end
60 changes: 60 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,68 @@
GEM
remote: https://rubygems.org/
specs:
actionpack (7.0.2.4)
actionview (= 7.0.2.4)
activesupport (= 7.0.2.4)
rack (~> 2.0, >= 2.2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actionview (7.0.2.4)
activesupport (= 7.0.2.4)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activesupport (7.0.2.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
ansi (1.5.0)
ast (2.4.2)
builder (3.2.4)
concurrent-ruby (1.1.10)
crass (1.0.6)
diff-lcs (1.4.4)
docile (1.4.0)
dotenv (2.7.6)
dotenv-rails (2.7.6)
dotenv (= 2.7.6)
railties (>= 3.2)
erubi (1.10.0)
i18n (1.10.0)
concurrent-ruby (~> 1.0)
loofah (2.17.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
method_source (1.0.0)
mini_portile2 (2.8.0)
minitest (5.15.0)
nokogiri (1.13.4)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
parallel (1.20.1)
parser (3.0.2.0)
ast (~> 2.4.1)
racc (1.6.0)
rack (2.2.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.4.2)
loofah (~> 2.3)
railties (7.0.2.4)
actionpack (= 7.0.2.4)
activesupport (= 7.0.2.4)
method_source
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rainbow (3.0.0)
rake (13.0.6)
regexp_parser (2.1.1)
rexml (3.2.5)
rspec (3.10.0)
Expand Down Expand Up @@ -48,16 +102,22 @@ GEM
simplecov_json_formatter (0.1.3)
terminal-table (3.0.1)
unicode-display_width (>= 1.1.1, < 3)
thor (1.2.1)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (2.0.0)
zeitwerk (2.5.4)

PLATFORMS
ruby

DEPENDENCIES
dotenv-rails
rspec
rubocop (= 1.20)
simplecov
simplecov-console
twilio-ruby

RUBY VERSION
ruby 3.0.2p107
Expand Down
87 changes: 30 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
Takeaway Challenge
==================
```
_________
r== | |
_ // | M.A. | ))))
|_)//(''''': | |
// \_____:_____.-------D )))))
// | === | / \
.:'//. \ \=| \ / .:'':./ )))))
:' // ': \ \ ''..'--:'-.. ':
'. '' .' \:.....:--'.-'' .'
':..:' ':..:'

```

Instructions

Instalation
-------
```
$ git clone https:/LGretzk/takeaway-challenge.git

* Feel free to use google, your notes, books, etc. but work on your own
* If you refer to the solution of another coach or student, please put a link to that in your README
* If you have a partial solution, **still check in a partial solution**
* You must submit a pull request to this repo with your code by 9am Monday morning
$ cd takeaway-challenge

Task
$ bundle
```

Instructions
-----

* Fork this repo
* Run the command 'bundle' in the project directory to ensure you have all the gems
* Write a Takeaway program with the following user stories:
* Use IRB to require the order.rb file

* Create a new instance of Order to order food.

* Use show_dishes method to see the menu.

* Use add_dish(id, quantity) to order dish.

* Use remove_dish(id) to remove dish from your order.

* Use update_dish_quantity(id, quantity) to update the quantity of your chosen dish.

* Use show_current_order to preview your order.

* Use total to get the total amount you would need to pay for your order.

* Use place_order(Confirmation.new) to place your order.


User stories
-----

```
As a customer
Expand All @@ -46,38 +54,3 @@ As a customer
So that I am reassured that my order will be delivered on time
I would like to receive a text such as "Thank you! Your order was placed and will be delivered before 18:52" after I have ordered
```

* Hints on functionality to implement:
* Ensure you have a list of dishes with prices
* The text should state that the order was placed successfully and that it will be delivered 1 hour from now, e.g. "Thank you! Your order was placed and will be delivered before 18:52".
* The text sending functionality should be implemented using Twilio API. You'll need to register for it. It’s free.
* Use the twilio-ruby gem to access the API
* Use the Gemfile to manage your gems
* Make sure that your Takeaway is thoroughly tested and that you use mocks and/or stubs, as necessary to not to send texts when your tests are run
* However, if your Takeaway is loaded into IRB and the order is placed, the text should actually be sent
* Note that you can only send texts in the same country as you have your account. I.e. if you have a UK account you can only send to UK numbers.

* Advanced! (have a go if you're feeling adventurous):
* Implement the ability to place orders via text message.

* A free account on Twilio will only allow you to send texts to "verified" numbers. Use your mobile phone number, don't worry about the customer's mobile phone.

> :warning: **WARNING:** think twice before you push your **mobile number** or **Twilio API Key** to a public space like GitHub :eyes:
>
> :key: Now is a great time to think about security and how you can keep your private information secret. You might want to explore environment variables.

* Finally submit a pull request before Monday at 9am with your solution or partial solution. However much or little amount of code you wrote please please please submit a pull request before Monday at 9am


In code review we'll be hoping to see:

* All tests passing
* High [Test coverage](https:/makersacademy/course/blob/main/pills/test_coverage.md) (>95% is good)
* The code is elegant: every class has a clear responsibility, methods are short etc.

Reviewers will potentially be using this [code review rubric](docs/review.md). Referring to this rubric in advance will make the challenge somewhat easier. You should be the judge of how much challenge you want this at this moment.

Notes on Test Coverage
------------------

You can see your [test coverage](https:/makersacademy/course/blob/main/pills/test_coverage.md) when you run your tests.
31 changes: 31 additions & 0 deletions lib/confirmation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'twilio-ruby'
require 'dotenv'
Dotenv.load

class Confirmation

def initialize
@number = ENV['TO_PHONE_NUMBER']
end

def send_text
account_sid = ENV['ACCOUNT_SID']
auth_token = ENV['AUTH_TOKEN']
client = Twilio::REST::Client.new(account_sid, auth_token)

from = ENV['FROM_PHONE_NUMBER']

client.messages.create(
from: from,
to: @number,
body: "Thank you! Your order was placed and will be delivered before #{delivery_time}"
)
end

def delivery_time
time = Time.now
delivered_by = (time + (60 * 60))
delivered_by.to_s.split()[1][0,5]
end

end
44 changes: 44 additions & 0 deletions lib/menu.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

class Menu

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class adheres well to SRP, it's succinctly written.

There are some over complexities - like each dish being available or not, but this doesn't affect the rest of the implementation.


DISHES = [
{ id: 1, name: 'Spinach & Cheddar Pancakes', price: 7, available?: true, quantity: 0 },
{ id: 2, name: 'Veggie Breakfast', price: 8, available?: true, quantity: 0 },
{ id: 3, name: 'Halloumi Sandwhich', price: 7, available?: true, quantity: 0 },
{ id: 4, name: 'Soup of the Day', price: 8, available?: true, quantity: 0 },
{ id: 5, name: 'Ceaser Salad', price: 8, available?: true, quantity: 0 },
{ id: 6, name: 'Pizza', price: 9, available?: true, quantity: 0 },
{ id: 7, name: 'Vegan Pizza', price: 10, available?: true, quantity: 0 },
{ id: 8, name: 'Yum Yum Salad', price: 11, available?: true, quantity: 0 },
{ id: 9, name: 'Singapore Laksa', price: 11, available?: true, quantity: 0 },
{ id: 10, name: 'Spiced Noodles', price: 11, available?: true, quantity: 0 },
{ id: 11, name: 'Phad Thai', price: 11, available?: true, quantity: 0 },
{ id: 12, name: 'Caponata', price: 11, available?: false, quantity: 0 }
].freeze

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some complexities in using a structure like this, when it comes to traversing each data point later in your implementation to print / change dishes.

I'll share an example I wrote for another dev:

Hashes are most useful where you have a key value pair - eg. if the only information you needed to record was the item name and price, a simple hash could look like:

@menu = { "1. Mattar Paneer" => 12.50, "Black Daal" => 7.50 }

@menu[item_name] will return the cost of that item.

For data requiring more than two values, where key/value isn't enough, I would suggest defining a new object, for example:

class Dish
    def initialize(name, price)
        @name = name
        @price = price
    end

    # getter methods for retrieving information
end

This way, you could work with an array of Dishes inside your Menu class, eg. @menu = [Dish.new(args), Dish.new(args), etc.]

Singular dish data can be accessed with @menu[0].name, etc. Which can be quite a bit easier than traversing a complex hash!

attr_reader :dishes

def initialize
@dishes = DISHES
end

def display
@dishes.map do |dish|
p "#{dish[:id]}. #{dish[:name]} - £#{dish[:price]} - #{dish[:available?] ? 'available' : 'unavailable'}"
end
end

def select_dish(id)
@dishes.select { |dish| dish[:id] == id }.first
end

def dish_available?(id)
dish = select_dish(id)
dish[:available?]
end

def id_valid?(id)
@dishes.select { |dish| dish[:id] == id } != []
end

end
93 changes: 93 additions & 0 deletions lib/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require_relative 'menu'
require_relative 'confirmation'

class Order

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is doing a lot of things! Each method is concisely written, but there's something of an array of responsibilities that could potentially be grouped further.

If you were to group these methods together following this technique, which further classes would you consider drawing out? https:/makersacademy/skills-workshops/blob/main/practicals/object_oriented_design/encapsulation.md


attr_reader :current_order, :order_placed

def initialize
@current_order = []
@order_placed = false
end

def show_dishes
@@menu.display
end

def add_dish(id, quantity)
select_dish(id)
if dish_already_added?(id)
add_by_updating_quantity(id, quantity)
else
@dish[:quantity] = quantity
@current_order << @dish
end
end

def remove_dish(id)
fail_if_dish_not_added(id)
index = @current_order.index { |dish| dish[:id] == id }
@current_order.delete_at(index)
end

def update_dish_quantity(id, quantity)
fail_if_dish_not_added(id)
remove_dish(id) if quantity < 1
@current_order.each { |dish| dish[:quantity] = quantity if dish[:id] == id }
end

def show_current_order
fail_if_order_empty
@current_order.map do |dish|
p "#{dish[:quantity]} x ##{dish[:id]} #{dish[:name]} - £#{dish[:price] * dish[:quantity]}"
end
puts "Order total: £#{total}"
end

def total
fail_if_order_empty
@current_order.map { |dish| dish[:price] * dish[:quantity] }.reduce(:+)
end

def place_order(confirmation)
fail_if_order_empty || (fail 'Order already placed' if @order_placed == true)
@order_placed = true
puts "Your order has been placed. Order summary:"
show_current_order
confirmation.send_text ### this would have had the tel number passed on as argument
end

private

@@menu = Menu.new

def select_dish(id)
fail_if_invalid_id(id) || fail_if_dish_unavailable(id)
@dish = @@menu.select_dish(id)
end

def add_by_updating_quantity(id, quantity)
@current_order.each { |dish| dish[:quantity] = (dish[:quantity] + quantity) if dish[:id] == id }
end

def fail_if_invalid_id(id)
fail 'Invalid id' unless @@menu.id_valid?(id)
end

def fail_if_dish_unavailable(id)
fail 'Dish unavailable' unless @@menu.dish_available?(id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't necessarily need a new method for each failure being raised.

SRP in methods would include barriers to that responsibility being carried out - so I would include the fail message inside the relevant method, and maybe draw out the conditional itself to its own method, rather.

end

def fail_if_dish_not_added(id)
fail 'Dish has not been added' unless dish_already_added?(id)
end

def fail_if_order_empty
fail 'Current order empty' if @current_order.empty?
end

def dish_already_added?(id)
(@current_order.select { |dish| dish[:id] == id }) != []
end

end
Empty file added spec/confirmation_spec.rb
Empty file.
Loading