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

takeaway-challenge #2234

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**/.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'
end
93 changes: 92 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,93 @@
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)
faraday (1.10.0)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
faraday-httpclient (~> 1.0)
faraday-multipart (~> 1.0)
faraday-net_http (~> 1.0)
faraday-net_http_persistent (~> 1.0)
faraday-patron (~> 1.0)
faraday-rack (~> 1.0)
faraday-retry (~> 1.0)
ruby2_keywords (>= 0.0.4)
faraday-em_http (1.0.0)
faraday-em_synchrony (1.0.0)
faraday-excon (1.1.0)
faraday-httpclient (1.0.1)
faraday-multipart (1.0.3)
multipart-post (>= 1.2, < 3)
faraday-net_http (1.0.1)
faraday-net_http_persistent (1.2.0)
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
i18n (1.10.0)
concurrent-ruby (~> 1.0)
jwt (2.3.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)
multipart-post (2.1.1)
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 @@ -36,6 +115,7 @@ GEM
rubocop-ast (1.11.0)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
ruby2_keywords (0.0.5)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand All @@ -48,19 +128,30 @@ GEM
simplecov_json_formatter (0.1.3)
terminal-table (3.0.1)
unicode-display_width (>= 1.1.1, < 3)
thor (1.2.1)
twilio-ruby (5.66.2)
faraday (>= 0.9, < 2.0)
jwt (>= 1.5, <= 2.5)
nokogiri (>= 1.6, < 2.0)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (2.0.0)
zeitwerk (2.5.4)

PLATFORMS
ruby

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

RUBY VERSION
ruby 3.0.2p107

BUNDLED WITH
2.2.26
2.3.12
83 changes: 41 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,54 @@ Takeaway Challenge

```

Instructions
Status -> Work in Progress
-------

* 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
* takeaway currently doesn't allow dishes to be deleted from order -> needs to be updated
Copy link

Choose a reason for hiding this comment

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

this is a good idea - wish I'd thought of this!

* takeaway currently doesn't check for availability -> needs to be updated
* sms function currently not working
* classes need restructuring
* tests need updating
* rafactoring needed

Navigate
-----

```
laura@Lauras-Air takeaway-challenge % irb
=>
#<TakeAway:0x000000014fa299f0
...
3.0.2 :002 > take.view_menu
["1. Mattar Paneer £12.5",
"2. Black Daal £7.5",
"3. Raita £3.5",
"4. Garlic Naan £3.5"]
3.0.2 :003 > take.add_dish(1, 1)
=> ["1 x Mattar Paneer £12.5/each, has been added to your order -> £[12.5]"]
3.0.2 :004 > take.add_dish(2, 1)
=> ["1 x Black Daal £7.5/each, has been added to your order -> £[7.5]"]
3.0.2 :005 > take.add_dish(4, 1)
=> ["1 x Garlic Naan £3.5/each, has been added to your order -> £[3.5]"]
3.0.2 :006 > take.add_dish(4, 2)
=> ["2 x Garlic Naan £3.5/each, has been added to your order -> £[7.0]"]
3.0.2 :007 > take.add_dish(2, 1)
=> ["1 x Black Daal £7.5/each, has been added to your order -> £[7.5]"]
3.0.2 :008 > take.view_basket
=>
["1 x Mattar Paneer £12.5/each -> £[12.5]",
"2 x Black Daal £7.5/each -> £15.0",
"3 x Garlic Naan £3.5/each -> £10.5"]
3.0.2 :009 > take.order_total
=> "Your order total is £38.0"
```

Task
-----

* 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:
* Takeaway program with the following user stories:

```
As a customer
Expand All @@ -45,39 +79,4 @@ I would like to check that the total I have been given matches the sum of the va
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://github.com/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://github.com/makersacademy/course/blob/main/pills/test_coverage.md) when you run your tests.
```
24 changes: 24 additions & 0 deletions lib/confirmation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'twilio-ruby'
require 'dotenv'
Dotenv.load

class Confirmation

def initialize
@to = ENV['TO_PHONE_NUMBER']
from = ENV['TWILIO_PHONE_NUMBER']
end

def send
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_AUTH_TOKEN']
client = Twilio::REST::Client.new(account_sid, auth_token)

client.messages.create(
from: from,
to: @to,
body: "Thank you for your order"
)
end

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

attr_reader :menu

def initialize
@menu = [
{ :item => 1, :dish => "Mattar Paneer", :price => 12.50 },
{ :item => 2, :dish => "Black Daal", :price => 7.50 },
{ :item => 3, :dish => "Raita", :price => 3.50 },
{ :item => 4, :dish => "Garlic Naan", :price => 3.50 }
]
end
Copy link

@eoinmakers eoinmakers May 6, 2022

Choose a reason for hiding this comment

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

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!


def view
@menu.map { |dish| "#{dish[:item]}. #{dish[:dish]} £#{dish[:price]}" }

Choose a reason for hiding this comment

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

Were you to use just a single key/value hash as the one I suggested in my first comment, you could refactor this to something like:

puts "#{key}:#{value}"

This will print the full menu.

end

def existing_dish(number, qty)
@menu.select { |dish| dish[:item] == number } != []
end

def selection(number, qty)
@menu.select { |dish| dish[:item] == number }
end

end
14 changes: 14 additions & 0 deletions lib/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Order

DELIVERY = 2700

def initialize(time = Time.new)
@time = time
end

def delivery_time
duration = @time + DELIVERY
duration.strftime("%H:%M")
end

end
68 changes: 68 additions & 0 deletions lib/take_away.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class TakeAway

attr_reader :menu, :order, :take_away

def initialize(menu = Menu.new, confirmation = Confirmation.new)
@confirmation = confirmation
@menu = menu
@order = []
@take_away = 0
@total = 0
@current_dish = []
end

def view_menu
@menu.view
end

def add_dish(number, qty)
fail "please enter a valid dish number" if @menu.existing_dish(number, qty) != true
if duplicate_dish(number, qty) == true
Copy link

Choose a reason for hiding this comment

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

This seems like a good idea. I just kept adding to the 'basket' array of hashes, so your idea of actually editing a quantity value seems a lot more like a real user experience

update_qty(number, qty)
else
@order << current_dish(number, qty)
end
dish_added_message(number, qty)
end

def view_basket
@order.map { |dish| "#{dish[:qty]} x #{dish[:dish]} £#{dish[:price]}/each -> £#{dish[:subtotal]}" }
end

def order_total
"Your order total is £#{total}"
end

def place_order
@confirmation.send
end

private

def duplicate_dish(number, qty)
@order.select { |dish| dish[:item] == number } != []
end

def update_qty(number, qty)
@order.map { |dish| (dish[:qty] = (dish[:qty] + qty)) && (dish[:subtotal] = dish[:qty] * dish[:price]) if (dish[:item] == number) }
end

def current_dish(number, qty)
(@menu.selection(number, qty).push(:qty => qty, :subtotal => sub_total(number, qty))).reduce(&:merge)
end

def dish_added_message(number, qty)
(@menu.selection(number, qty)).map do |dish|
"#{qty} x #{dish[:dish]} £#{dish[:price]}/each, has been added to your order -> £#{(sub_total(number, qty))}"
end

Choose a reason for hiding this comment

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

Although this is functional, the functionality to receive a confirmation for each item added to the order is not mentioned in the spec - you should stick as closely as possible to the spec as possible. This helps keep your code clean / simple!

end

def sub_total(number, qty)

Choose a reason for hiding this comment

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

You could omit this and only calculate the total at runtime / when the user confirms the order.

sub_total = @menu.selection(number, qty)
sub_total.map { |item| item[:price] * qty }
end

def total
@order.map { |item| (item[:subtotal]) }.flatten.sum
end
end
11 changes: 11 additions & 0 deletions spec/confirmation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'confirmation'

describe Confirmation do

subject(:confirmation) { Confirmation.new }

it "should create an instance of class Confirmation" do
expect(confirmation).to be_instance_of(Confirmation)
end

end
Loading