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

Russell's TakeAway Challenge #2228

Open
wants to merge 4 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
3 changes: 3 additions & 0 deletions .gitignore
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-*
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ end

group :development, :test do
gem 'rubocop', '1.20'
gem 'twilio-ruby'
end
36 changes: 36 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,39 @@ GEM
ast (2.4.2)
diff-lcs (1.4.4)
docile (1.4.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)
jwt (2.3.0)
mini_portile2 (2.8.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)
rainbow (3.0.0)
regexp_parser (2.1.1)
rexml (3.2.5)
Expand Down Expand Up @@ -36,6 +66,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,6 +79,10 @@ GEM
simplecov_json_formatter (0.1.3)
terminal-table (3.0.1)
unicode-display_width (>= 1.1.1, < 3)
twilio-ruby (5.66.2)
faraday (>= 0.9, < 2.0)
jwt (>= 1.5, <= 2.5)
nokogiri (>= 1.6, < 2.0)
unicode-display_width (2.0.0)

PLATFORMS
Expand All @@ -58,6 +93,7 @@ DEPENDENCIES
rubocop (= 1.20)
simplecov
simplecov-console
twilio-ruby

RUBY VERSION
ruby 3.0.2p107
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ Takeaway Challenge

```

Instructions
Instructions on how to use
-------

* 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
* Open IRB. require './lib/order.rb'.
* Create a new instance of Order.
* Type order.menu.view_dishes to see the list of dishes with prices.
* Type order.add_dish('Burger') to add a dish and see price.
* Type order.view_order to see a subtotal and total of your order.
* To finish the task I would Implement twilio text message so the customer would know their order was placed and would arrive at a certain time.

Task
-----
Expand Down
25 changes: 25 additions & 0 deletions lib/menu.rb
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?

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?

end
end
69 changes: 69 additions & 0 deletions lib/order.rb
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
22 changes: 22 additions & 0 deletions spec/menu_spec.rb
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
46 changes: 46 additions & 0 deletions spec/order_spec.rb
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