From b20244e2e5289f62d176e90acc5d8d897be0e37d Mon Sep 17 00:00:00 2001 From: Yessiest Date: Thu, 27 Apr 2023 22:27:53 +0400 Subject: [PATCH] testing this little express js task one of my friends got --- test_calculator.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 test_calculator.rb diff --git a/test_calculator.rb b/test_calculator.rb new file mode 100644 index 0000000..f3f555f --- /dev/null +++ b/test_calculator.rb @@ -0,0 +1,32 @@ +require_relative "hyde" +server = Hyde::Server.new Port: 8000 do + {"add" => -> (a,b) { a + b }, + "sub" => -> (a,b) { a - b }, + "mul" => -> (a,b) { a * b }, + "div" => -> (a,b) { + begin + return a/b + rescue ZeroDivisionError + return "Divided by zero" + end + } + }.each_pair do |k,v| + serve k do |ctx| + req,res = ctx.request,ctx.response + a,b = req.query["a"],req.query["b"] + result = (a and b) ? v.call(a.to_f,b.to_f) : "Invalid parameters" + res.body = " + + + + Calculator API test + + + Result: #{result} + +" + res['Content-Type'] = "text/html" + end + end +end +server.start