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