From 61caa5cc0d3298d0b53180c13a2a3014e9f1fdcd Mon Sep 17 00:00:00 2001 From: Rupert Pople Date: Sat, 2 Apr 2022 17:36:04 +0100 Subject: [PATCH 1/5] user story 2 --- lib/dish.rb | 19 +++++++++++++++++++ lib/menu.rb | 33 +++++++++++++++++++++++++++++++++ lib/order.rb | 20 ++++++++++++++++++++ spec/dish_spec.rb | 11 +++++++++++ spec/menu_spec.rb | 7 +++++++ spec/order_spec.rb | 9 +++++++++ 6 files changed, 99 insertions(+) create mode 100644 lib/dish.rb create mode 100644 lib/menu.rb create mode 100644 lib/order.rb create mode 100644 spec/dish_spec.rb create mode 100644 spec/menu_spec.rb create mode 100644 spec/order_spec.rb diff --git a/lib/dish.rb b/lib/dish.rb new file mode 100644 index 0000000000..a1743a061a --- /dev/null +++ b/lib/dish.rb @@ -0,0 +1,19 @@ +class Dish + + attr_accessor = :name, :price + + def initialize(name, price) + @name = name + @price = price + end + + def name + @name + end + + def price + @price + end + + +end \ No newline at end of file diff --git a/lib/menu.rb b/lib/menu.rb new file mode 100644 index 0000000000..4a2c17d063 --- /dev/null +++ b/lib/menu.rb @@ -0,0 +1,33 @@ +require_relative 'dish' +require_relative 'order' + +class Menu + + attr_reader :order, :view_menu + attr_accessor :menu, :item_number + + def initialize + @menu = [] + end + + def add_dish(name, price) + dish = Dish.new(name, price) + @menu.push(dish) + end + + def menu + @menu + end + + + def create_order + @order = Order.new(@ticket) + end + + private + + def view_order_numbers + @order_numbers + end + +end \ No newline at end of file diff --git a/lib/order.rb b/lib/order.rb new file mode 100644 index 0000000000..f16f3c6914 --- /dev/null +++ b/lib/order.rb @@ -0,0 +1,20 @@ +require_relative 'dish' +require_relative 'menu' + +class Order + + attr_accessor :order + + def initialize(*item_number) + @order = [] + @item_numbers = [*item_number] + @ticketcount = @item_numbers.count + @ticket = [] + + Menu.menu.each_with_index do |item, index| + if index == @item_numbers[0..@ticketcount] + @order.push(item) + end + end + end +end \ No newline at end of file diff --git a/spec/dish_spec.rb b/spec/dish_spec.rb new file mode 100644 index 0000000000..39e50467db --- /dev/null +++ b/spec/dish_spec.rb @@ -0,0 +1,11 @@ +require 'dish' + +describe Dish do + dish = Dish.new("Korma", 8) + it '#name should retun the name of the dish' do + expect(dish.name).to be_a(String) + end + it '#price should return the price of the dish' do + expect(dish.price).to be_a(Integer) + end +end \ No newline at end of file diff --git a/spec/menu_spec.rb b/spec/menu_spec.rb new file mode 100644 index 0000000000..ef52af25bf --- /dev/null +++ b/spec/menu_spec.rb @@ -0,0 +1,7 @@ +require 'menu' + +describe Menu do + it '#view_menu will return an array hash that is the menu' do + expect(subject.view_menu).to be_a(Array) + end +end \ No newline at end of file diff --git a/spec/order_spec.rb b/spec/order_spec.rb new file mode 100644 index 0000000000..156a8dc579 --- /dev/null +++ b/spec/order_spec.rb @@ -0,0 +1,9 @@ +require 'order' + +describe Order do + order = Order.new + it 'order be an array' do + expect(order.order).to be_an_instance_of(Array) + end + +end \ No newline at end of file From 939e5344390df05bd79e71d951585f5ec96e00d9 Mon Sep 17 00:00:00 2001 From: Rupert Pople Date: Sat, 2 Apr 2022 20:16:02 +0100 Subject: [PATCH 2/5] user story 3 complete --- lib/order.rb | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index f16f3c6914..6692aa7c0e 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -5,16 +5,24 @@ class Order attr_accessor :order - def initialize(*item_number) + def initialize(menu, *item_number) @order = [] @item_numbers = [*item_number] - @ticketcount = @item_numbers.count - @ticket = [] - - Menu.menu.each_with_index do |item, index| - if index == @item_numbers[0..@ticketcount] - @order.push(item) - end - end + @menu = menu + @total_price = 0 + end + + def submit_order + @order = @item_numbers.map { |dish| @menu.menu[dish-1]} + end + + def check_order_price + calculate_order_price.each { |amount| @total_price+=amount} + @total_price + end + + private + def calculate_order_price + @order.map {|dish| dish.instance_variable_get(:@price)} end end \ No newline at end of file From 40f3277b5ac24dd87f572a0d7eaf42bb74b3aa29 Mon Sep 17 00:00:00 2001 From: Rupert Pople Date: Sat, 2 Apr 2022 23:59:07 +0100 Subject: [PATCH 3/5] Almost done - just need to complete tests --- Gemfile | 1 + Gemfile.lock | 34 ++++++++++++++++++++++++++++++++ lib/TwilioTextMessenger.rb | 17 ++++++++++++++++ lib/dish.rb | 3 +-- lib/menu.rb | 5 ----- lib/order.rb | 19 ++++++++++++++---- spec/TwilioTextMessenger_spec.rb | 7 +++++++ spec/menu_spec.rb | 12 +++++++++-- spec/order_spec.rb | 17 +++++++++++++--- 9 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 lib/TwilioTextMessenger.rb create mode 100644 spec/TwilioTextMessenger_spec.rb diff --git a/Gemfile b/Gemfile index 35de4a7f0e..c4651ac753 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ ruby '3.0.2' group :test do gem 'rspec' + gem 'twilio-ruby' gem 'simplecov', require: false, group: :test gem 'simplecov-console', require: false, group: :test end diff --git a/Gemfile.lock b/Gemfile.lock index 66064703c7..5730cc2f7d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,9 +5,37 @@ 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) + multipart-post (2.1.1) + nokogiri (1.13.3-arm64-darwin) + racc (~> 1.4) parallel (1.20.1) parser (3.0.2.0) ast (~> 2.4.1) + racc (1.5.1) rainbow (3.0.0) regexp_parser (2.1.1) rexml (3.2.5) @@ -36,6 +64,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) @@ -48,6 +77,10 @@ GEM simplecov_json_formatter (0.1.3) terminal-table (3.0.1) unicode-display_width (>= 1.1.1, < 3) + twilio-ruby (5.66.0) + faraday (>= 0.9, < 2.0) + jwt (>= 1.5, <= 2.5) + nokogiri (>= 1.6, < 2.0) unicode-display_width (2.0.0) PLATFORMS @@ -58,6 +91,7 @@ DEPENDENCIES rubocop (= 1.20) simplecov simplecov-console + twilio-ruby RUBY VERSION ruby 3.0.2p107 diff --git a/lib/TwilioTextMessenger.rb b/lib/TwilioTextMessenger.rb new file mode 100644 index 0000000000..b80c7fb406 --- /dev/null +++ b/lib/TwilioTextMessenger.rb @@ -0,0 +1,17 @@ +require 'twilio-ruby' + +class TwilioTextMessenger + attr_reader :message, :call + + def initialize + @message = message + account_sid = 'ACf27cbbcdf904e00bcf60428cfb4271c9' + auth_token = '18303ff4fddfac8208b8be0aab167886' + client = Twilio::REST::Client.new(account_sid, auth_token) + client.messages.create( + from: 'my twilio number', + to: 'my phone number', + body: "Thank you! Your order was placed and will be delivered before 18:52" + ) + end +end \ No newline at end of file diff --git a/lib/dish.rb b/lib/dish.rb index a1743a061a..379dce0940 100644 --- a/lib/dish.rb +++ b/lib/dish.rb @@ -14,6 +14,5 @@ def name def price @price end - - + end \ No newline at end of file diff --git a/lib/menu.rb b/lib/menu.rb index 4a2c17d063..a21fb2796b 100644 --- a/lib/menu.rb +++ b/lib/menu.rb @@ -19,11 +19,6 @@ def menu @menu end - - def create_order - @order = Order.new(@ticket) - end - private def view_order_numbers diff --git a/lib/order.rb b/lib/order.rb index 6692aa7c0e..1e38ea058c 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -1,5 +1,6 @@ require_relative 'dish' require_relative 'menu' +require_relative 'TwilioTextMessenger' class Order @@ -14,15 +15,25 @@ def initialize(menu, *item_number) def submit_order @order = @item_numbers.map { |dish| @menu.menu[dish-1]} + send_sms end - def check_order_price - calculate_order_price.each { |amount| @total_price+=amount} - @total_price + def bill + prepare_bill end private def calculate_order_price - @order.map {|dish| dish.instance_variable_get(:@price)} + @amounts = @order.map {|dish| dish.instance_variable_get(:@price)} + @amounts.each { |amount| @total_price+=amount} + "Total to pay: £#{@total_price}" + end + + def prepare_bill + @order.each { |dish| puts "#{dish.name}: £#{dish.price}"} + end + + def send_sms + TwilioTextMessenger.new end end \ No newline at end of file diff --git a/spec/TwilioTextMessenger_spec.rb b/spec/TwilioTextMessenger_spec.rb new file mode 100644 index 0000000000..a24cd08fea --- /dev/null +++ b/spec/TwilioTextMessenger_spec.rb @@ -0,0 +1,7 @@ +require 'TwilioTextMessenger' + +describe TwilioTextMessenger do + # it "should send text" do + # expect(subject).to include("Thank you! Your order was placed and will be delivered before 18:52") + # end +end \ No newline at end of file diff --git a/spec/menu_spec.rb b/spec/menu_spec.rb index ef52af25bf..72d1cfcaaf 100644 --- a/spec/menu_spec.rb +++ b/spec/menu_spec.rb @@ -1,7 +1,15 @@ require 'menu' describe Menu do - it '#view_menu will return an array hash that is the menu' do - expect(subject.view_menu).to be_a(Array) + it '#view_menu will return the list of dishes available' do + subject.add_dish("Fish",9) + subject.add_dish("Korma",8) + expect(subject.menu).to be_a(Array) end + + it "#add dish will allow use to pick as many dishes as they want" do + 20.times {subject.add_dish("Korma",8)} + expect(subject.menu.count).to eq 20 + end + end \ No newline at end of file diff --git a/spec/order_spec.rb b/spec/order_spec.rb index 156a8dc579..4e9f102e81 100644 --- a/spec/order_spec.rb +++ b/spec/order_spec.rb @@ -1,9 +1,20 @@ require 'order' +require 'menu' describe Order do - order = Order.new - it 'order be an array' do - expect(order.order).to be_an_instance_of(Array) + menu = Menu.new + menu.add_dish("Fish",9) + menu.add_dish("Korma",8) + order = Order.new(menu,1,2) + #need to sort out submit_order and bill + it "#submit_order should put chosen dishes into an order" do + expect(order.order[0]).to be_a(Dish) + expect(order.order[1]).to be_a(Dish) + end + + it "#bill to return a bill with names and price of each item" do + expect(order.bill).to include(:dish => 'Fish') + expect(order.bill).to include(:price => 8) end end \ No newline at end of file From dc39188da452e45a3d0ec228903acecd93206459 Mon Sep 17 00:00:00 2001 From: Rupert Pople Date: Sun, 3 Apr 2022 15:10:26 +0100 Subject: [PATCH 4/5] Completed --- spec/order_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/order_spec.rb b/spec/order_spec.rb index 4e9f102e81..4a785c8d2a 100644 --- a/spec/order_spec.rb +++ b/spec/order_spec.rb @@ -6,7 +6,7 @@ menu.add_dish("Fish",9) menu.add_dish("Korma",8) order = Order.new(menu,1,2) - #need to sort out submit_order and bill + order.submit_order it "#submit_order should put chosen dishes into an order" do expect(order.order[0]).to be_a(Dish) expect(order.order[1]).to be_a(Dish) From 48cf33f93da8d4f3045150a43359f7889a2fd1ba Mon Sep 17 00:00:00 2001 From: Rupert Pople Date: Mon, 4 Apr 2022 10:00:58 +0100 Subject: [PATCH 5/5] Updated text --- lib/TwilioTextMessenger.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/TwilioTextMessenger.rb b/lib/TwilioTextMessenger.rb index b80c7fb406..37bf035bae 100644 --- a/lib/TwilioTextMessenger.rb +++ b/lib/TwilioTextMessenger.rb @@ -11,7 +11,7 @@ def initialize client.messages.create( from: 'my twilio number', to: 'my phone number', - body: "Thank you! Your order was placed and will be delivered before 18:52" + body: "Thank you! Your order was placed and will be delivered before #{Time.now + 1*60*60}" ) end end \ No newline at end of file