From 673733225116d1c7fceae63489a39816f3b9299c Mon Sep 17 00:00:00 2001 From: J Fern Date: Mon, 25 Nov 2019 03:50:59 +0000 Subject: [PATCH 1/2] Done. --- app/application.rb | 54 ++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/app/application.rb b/app/application.rb index 7e25e25c7..41aaa3878 100644 --- a/app/application.rb +++ b/app/application.rb @@ -1,30 +1,42 @@ -class Application - + class Application + @@items = ["Apples","Carrots","Pears"] + @@cart = [] - def call(env) - resp = Rack::Response.new - req = Rack::Request.new(env) - - if req.path.match(/items/) - @@items.each do |item| - resp.write "#{item}\n" + def call(env) + resp = Rack::Response.new + req = Rack::Request.new(env) + + + if req.path.match(/search/) + @@items.each do |item| + resp.write "#{item}\n" + + end + item = req.params["q"] + if @@items.include?(item) + resp.write "#{item} is one of our items" + else + resp.write "Couldn't find #{item}" end - elsif req.path.match(/search/) - search_term = req.params["q"] - resp.write handle_search(search_term) - else - resp.write "Path Not Found" - end - - resp.finish - end + else + resp.write "Path Not Found " + resp.write "Your cart is empty" + resp.write @@items << "Apples\nOranges" +end + resp.finish +end def handle_search(search_term) if @@items.include?(search_term) return "#{search_term} is one of our items" else return "Couldn't find #{search_term}" - end - end -end + end + end + end + + + + + \ No newline at end of file From c135600dbcc2071e5caec07a0ae9f949b51239dd Mon Sep 17 00:00:00 2001 From: Jonathan Fernandez Date: Thu, 28 Nov 2019 05:39:54 +0000 Subject: [PATCH 2/2] Done. --- app/application.rb | 73 +++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/app/application.rb b/app/application.rb index 41aaa3878..78af1ab72 100644 --- a/app/application.rb +++ b/app/application.rb @@ -1,40 +1,59 @@ - class Application - +class Application + @@items = ["Apples","Carrots","Pears"] - @@cart = [] + @@cart = [] - def call(env) - resp = Rack::Response.new - req = Rack::Request.new(env) - + def call(env) + resp = Rack::Response.new + req = Rack::Request.new(env) + +#-------------------------------- + + if req.path.match(/items/) + @@items.each do |item| + resp.write "#{item}\n" + end - if req.path.match(/search/) - @@items.each do |item| - resp.write "#{item}\n" - - end - item = req.params["q"] - if @@items.include?(item) - resp.write "#{item} is one of our items" +#-------------------------------- + elsif req.path.match(/search/) + search_term = req.params["q"] + resp.write handle_search(search_term) + elsif req.path.match(/cart/) + if @@cart.empty? + resp.write "Your cart is empty" else - resp.write "Couldn't find #{item}" + @@cart.each do |item| + resp.write "#{item}\n" + end end - else - resp.write "Path Not Found " - resp.write "Your cart is empty" - resp.write @@items << "Apples\nOranges" -end - resp.finish -end - + + #------------------------------- + elsif req.path.match(/add/) + item_to_add = req.params["item"] + if @@items.include? item_to_add + @@cart << item_to_add + resp.write "added #{item_to_add}" + else + resp.write "We don't have that item!" + end + else + resp.write "Path Not Found" + end + + #------------------------------- + resp.finish + end + #------------------------------- def handle_search(search_term) if @@items.include?(search_term) return "#{search_term} is one of our items" else return "Couldn't find #{search_term}" - end - end - end + end + end +end + +