-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Timbo's Takeaway #2225
base: main
Are you sure you want to change the base?
Timbo's Takeaway #2225
Changes from all commits
5c8acf2
56a00f3
4d54ccc
848651d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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-* |
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', '5.66.2' | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Menu | ||
|
||
attr_reader :menu | ||
|
||
def initialize | ||
|
||
@menu = { | ||
"Pepperoni Pizza" => 9, | ||
"Margherita Pizza" => 7, | ||
"Neapolitan Pizza" => 11, | ||
"Fiorentina Pizza" => 10, | ||
} | ||
|
||
end | ||
|
||
def show_menu | ||
|
||
@menu | ||
|
||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
require_relative 'menu' | ||
require 'rubygems' | ||
require 'twilio-ruby' | ||
require 'time' | ||
# require 'sinatra' | ||
|
||
# post '/message' do | ||
# number = params['From'] | ||
# body = params['Body'] | ||
|
||
# content_type 'text/xml' | ||
# "<Response> | ||
# <Message> | ||
# Hello #{number}. You said: #{body} | ||
# </Message> | ||
# </Response>" | ||
# end | ||
|
||
class Order | ||
|
||
attr_reader :order, :order_total, :simp_order | ||
|
||
def initialize | ||
|
||
@menu = Menu.new | ||
@order = [] | ||
@order_total = 0 | ||
@simp_order = "" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd put all the text stuff into its own class. I did have difficulties getting it to work from the order object, but my feeling was since it's a new noun, it should probably be its own class. |
||
account_sid = ENV['ACCOUNT_SID'] | ||
auth_token = ENV['AUTH_TOKEN'] | ||
|
||
@client = Twilio::REST::Client.new(account_sid, auth_token) | ||
|
||
end | ||
|
||
def pick_item(item, quantity) | ||
|
||
fail "Please choose from the menu" unless @menu.menu.has_key?(item) | ||
quantity.times { @order << item } | ||
|
||
end | ||
|
||
def order_value | ||
|
||
count = 0 | ||
@order.each do |order_item| | ||
if @menu.menu.has_key?(order_item) | ||
count += @menu.menu[order_item] | ||
end | ||
end | ||
@order_total + count | ||
|
||
end | ||
|
||
def arrival_time | ||
|
||
(Time.now + 1 * 60 * 60).strftime("%k:%M") | ||
|
||
end | ||
|
||
def place_order | ||
|
||
send_order_text | ||
"Thanks! Your order has been received and will be with you by #{arrival_time}" | ||
|
||
end | ||
|
||
def order_summary | ||
|
||
"Your order so far is: #{simplify_order}" | ||
|
||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be re-written so as not to be hard coded, but I like that you added a function that states the quantity of each item - that's not something I did. |
||
def simplify_order | ||
|
||
pep = @order.count("Pepperoni Pizza") | ||
marg = @order.count("Margherita Pizza") | ||
neap = @order.count("Neapolitan Pizza") | ||
fior = @order.count("Fiorentina Pizza") | ||
|
||
@simp_order << "#{pep}x Pepperoni Pizza " if @order.include?("Pepperoni Pizza") | ||
@simp_order << "#{marg}x Margherita Pizza " if @order.include?("Margherita Pizza") | ||
@simp_order << "#{neap}x Neapolitan Pizza " if @order.include?("Neapolitan Pizza") | ||
@simp_order << "#{fior}x Fiorentina Pizza " if @order.include?("Fiorentina Pizza") | ||
|
||
@simp_order << "| Totalling: £#{order_value}" | ||
|
||
end | ||
|
||
private | ||
|
||
def send_order_text | ||
|
||
@client.messages.create( | ||
from: '+19805504523', | ||
to: ENV['PHONE_NUM'], | ||
body: "Thanks for your order! #{@simp_order} will be with you by: #{arrival_time}" | ||
) | ||
|
||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'menu' | ||
|
||
describe Menu do | ||
|
||
context "the menu can list its items, along with their prices" do | ||
|
||
it "initialises with a hash, which is the menu" do | ||
|
||
expect(subject.menu).to eq({ | ||
"Pepperoni Pizza" => 9, | ||
"Margherita Pizza" => 7, | ||
"Neapolitan Pizza" => 11, | ||
"Fiorentina Pizza" => 10, | ||
}) | ||
|
||
end | ||
|
||
it "displays the menu when called" do | ||
|
||
expect(subject.show_menu).to eq subject.menu | ||
|
||
end | ||
|
||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require 'order' | ||
require 'time' | ||
|
||
describe Order do | ||
|
||
context "A customer can select a number of items from the menu as their order" do | ||
|
||
it "Subject responds to the pick_item method" do | ||
|
||
expect(subject).to respond_to :pick_item | ||
|
||
end | ||
|
||
it "A customer can start their order by adding an item to their order" do | ||
|
||
subject.pick_item("Pepperoni Pizza", 1) | ||
expect(subject.order).to eq(["Pepperoni Pizza"]) | ||
|
||
end | ||
|
||
it "A customer can check their order total is correct by summing up the prices of their order items" do | ||
|
||
subject.pick_item("Pepperoni Pizza", 4) | ||
expect(subject.order_value).to eq 36 | ||
|
||
end | ||
|
||
it "Raises an error when customers try to order something that's not on the menu" do | ||
|
||
expect { subject.pick_item("Hawaiian", 1) }.to raise_error("Please choose from the menu") | ||
|
||
end | ||
|
||
it "A customer can order an item multiple times in one go" do | ||
|
||
subject.pick_item("Pepperoni Pizza", 4) | ||
expect(subject.order).to eq(["Pepperoni Pizza", "Pepperoni Pizza", "Pepperoni Pizza", "Pepperoni Pizza"]) | ||
|
||
end | ||
|
||
end | ||
|
||
context "A customer can place an order" do | ||
|
||
it "the time method can be used to print the current time plus one hour" do | ||
|
||
expect(subject.arrival_time).to eq (Time.now + 1 * 60 * 60).strftime("%k:%M") | ||
|
||
end | ||
|
||
it "a customer can submit their order" do | ||
|
||
subject.pick_item("Pepperoni Pizza", 4) | ||
allow(subject).to receive(:send_order_text).and_return "Text sent!" | ||
|
||
expect(subject.place_order).to eq "Thanks! Your order has been received and will be with you by #{subject.arrival_time}" | ||
|
||
end | ||
|
||
it "a customer gets an current order summary when adding an item to their order" do | ||
|
||
subject.pick_item("Pepperoni Pizza", 4) | ||
|
||
expect(subject.order_summary).to eq "Your order so far is: #{subject.simp_order}" | ||
|
||
end | ||
|
||
it "a simplified order summary gives quantities of items ordered" do | ||
|
||
subject.pick_item("Pepperoni Pizza", 4) | ||
subject.pick_item("Fiorentina Pizza", 4) | ||
subject.order_value | ||
|
||
expect(subject.simplify_order).to eq "4x Pepperoni Pizza 4x Fiorentina Pizza | Totalling: £#{subject.order_value}" | ||
|
||
end | ||
|
||
end | ||
|
||
end |
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.
This could be re-written to import different menus, but I agree that a new menu instance should be initialised with a specific menu.