Skip to content

Instantly share code, notes, and snippets.

@harsh183
Created July 25, 2019 07:00
Show Gist options
  • Save harsh183/8c5b6ca8e63dc693a4a763bb0f44d327 to your computer and use it in GitHub Desktop.
Save harsh183/8c5b6ca8e63dc693a4a763bb0f44d327 to your computer and use it in GitHub Desktop.
require 'sinatra'
# Ex. http://localhost:4567/calc/add?x=1&y=2
get '/calc/:operation' do
operations = { 'add' => ->(x, y) {x + y},
'sub' => ->(x, y) {x - y},
'mul' => ->(x, y) {x * y},
'div' => ->(x, y) {x / y},
'mod' => ->(x, y) {x % y} }
# Yes I could DRY that further but I don't trust user input
operation = params['operation']
x, y = params['x'].to_f, params['y'].to_f
result = operations[operation].call(x, y)
result.to_s
end
@harsh183
Copy link
Author

harsh183 commented May 24, 2020

MIT License

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment