testing this little express js task one of my friends got

This commit is contained in:
Yessiest 2023-04-27 22:27:53 +04:00
parent 37d242cde1
commit b20244e2e5
1 changed files with 32 additions and 0 deletions

32
test_calculator.rb Normal file
View File

@ -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 = "
<!DOCTYPE html>
<html>
<head>
<title> Calculator API test </title>
</head>
<body>
<h> Result: #{result} </h>
</body>
</html>"
res['Content-Type'] = "text/html"
end
end
end
server.start