Created
November 30, 2020 12:45
-
-
Save progapandist/bf84e3f35f9d9f675dbeeaea7c564f39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "sinatra" | |
require "sinatra/reloader" if development? | |
require "sinatra/json" | |
get "/" do | |
status 200 | |
body "Build an arithmetic API" | |
end | |
# Implement a /sum endpoint that can sum two integers provided by the user. | |
# Choose the verb and expected params yourself thinking about the best DX for API consumer. | |
# The API response should be a JSON containing both integers provided by the user and the result of summation. | |
# The API should respond with a correct HTTP code and informative response body if user did not provide integers, | |
# did not provide EXACTLY TWO of them, or provided a malformed request. | |
# ===================== POSSIBLE SOLUTION ====================== | |
get "/sum" do | |
ints = params.values.map { |v| v.to_i } | |
resp = {} | |
if ints.count != 2 | |
resp = {error: "you need to provide exactly two integers as params"} | |
status 400 | |
else | |
resp = {operands: ints, result: ints.sum} | |
status 200 | |
end | |
json resp | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment