-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Russell's TakeAway Challenge #2228
Open
Rmorbey
wants to merge
4
commits into
makersacademy:main
Choose a base branch
from
Rmorbey:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
/**/.DS_Store | ||
/coverage | ||
|
||
# Environment variables (including keys and phone number) | ||
.env | ||
|
||
# Local cache of Rubocop remote config | ||
.rubocop-* |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ end | |
|
||
group :development, :test do | ||
gem 'rubocop', '1.20' | ||
gem 'twilio-ruby' | ||
end |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
|
||
class Menu | ||
|
||
attr_reader :dishes | ||
|
||
def initialize | ||
|
||
@dishes = { | ||
"Burger" => 5, | ||
"Chips" => 3, | ||
"Hot Dog" => 4, | ||
"Wings" => 3 | ||
} | ||
|
||
end | ||
|
||
def view_dishes | ||
dishes.each { |item, price| p "#{item} £#{price}" } | ||
end | ||
|
||
def check_if_available(item) | ||
raise 'That dish is not available. Please select another dish.' if @dishes[item].nil? | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require_relative 'menu' | ||
require 'rubygems' | ||
require 'twilio-ruby' | ||
require 'time' | ||
require 'dotenv/load' | ||
|
||
class Order | ||
|
||
attr_reader :menu, :customer_order | ||
|
||
def initialize(menu = Menu.new) | ||
@menu = menu | ||
@customer_order = Hash.new(0) | ||
|
||
account_sid = ENV['TWILIO_ACCOUNT_SID'] | ||
auth_token = ENV['TWILIO_AUTH_TOKEN'] | ||
|
||
@client = Twilio::REST::Client.new(account_sid, auth_token) | ||
|
||
end | ||
|
||
def add_dish(item) | ||
@menu.check_if_available(item) | ||
@customer_order[item] += @menu.dishes[item] | ||
end | ||
|
||
def view_order | ||
order_subtotals | ||
order_total | ||
end | ||
|
||
def place_order | ||
send_text | ||
"Thank you! Your order was placed and will be delivered by #{arrival_time}" | ||
end | ||
|
||
private | ||
|
||
def order_subtotals | ||
@customer_order.each do |item, price| | ||
if price.positive? | ||
quantity = (@customer_order[item] / @menu.dishes[item]).round | ||
puts "#{quantity}x #{item} £#{price}" | ||
end | ||
end | ||
end | ||
|
||
def checkout_total | ||
@customer_order.select { |_, price| price.positive? }.values.reduce(:+) | ||
end | ||
|
||
def order_total | ||
puts "==========" | ||
puts "TOTAL: £#{checkout_total}" | ||
end | ||
|
||
def arrival_time | ||
(Time.now + 1 * 60 * 60).strftime("%k:%M") | ||
end | ||
|
||
def send_text | ||
|
||
@client.messages.create( | ||
from: ENV['TWILIO_NUM'], | ||
to: ENV['PHONE_NUM'], | ||
body: "Thank you! Your order was placed and will be delivered by #{arrival_time}" | ||
) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'menu' | ||
|
||
describe Menu do | ||
describe '#view_dishes' do | ||
it 'shows a list of dishes with prices' do | ||
|
||
expect(subject.view_dishes).to eq({ | ||
"Burger" => 5, | ||
"Chips" => 3, | ||
"Hot Dog" => 4, | ||
"Wings" => 3 | ||
}) | ||
end | ||
end | ||
|
||
describe '#check_if_available' do | ||
|
||
it 'raises an error message if the dish is not available on the menu' do | ||
expect { subject.check_if_available("Pie") }.to raise_error 'That dish is not available. Please select another dish.' | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'order' | ||
|
||
describe Order do | ||
|
||
it 'creates an instance of Order' do | ||
expect(subject).to be_instance_of(Order) | ||
end | ||
|
||
it 'has a default empty hash to store customer\'s order' do | ||
expect(subject.customer_order).to eq Hash.new(0) | ||
end | ||
|
||
describe '#add_dish' do | ||
|
||
it 'adds a dish to customer\'s order' do | ||
expect { subject.add_dish('Burger') }.to change { subject.customer_order }.from(Hash.new(0)).to ({ 'Burger' => 5 }) | ||
end | ||
|
||
it 'stores a dish in the customer\'s order' do | ||
subject.add_dish('Burger') | ||
expect(subject.customer_order).to include('Burger') | ||
end | ||
end | ||
|
||
describe '#view_order' do | ||
|
||
context 'customer\'s order contains dishes' do | ||
before(:each) { subject.add_dish('Burger') } | ||
before(:each) { subject.add_dish('Chips') } | ||
before(:each) { subject.add_dish('Wings') } | ||
|
||
it 'prints dishes from the customer\'s order with quantities, prices and total' do | ||
# using a HEREdoc | ||
expect { subject.view_order }.to output(<<-order | ||
1x Burger £5 | ||
1x Chips £3 | ||
1x Wings £3 | ||
========== | ||
TOTAL: £11 | ||
order | ||
).to_stdout | ||
end | ||
|
||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are both of these messages needed? Would just the 'raise' message work without the return one?