Created
February 29, 2024 09:36
-
-
Save tjmw/51fa1399d841f1fec15497b188779ea1 to your computer and use it in GitHub Desktop.
Tiny rack app to fake a JSON API
This file contains 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 'json' | |
class MyApp | |
def call(env) | |
request = Rack::Request.new(env) | |
headers = { | |
'Content-Type' => 'application/json', | |
'Access-Control-Allow-Origin' => '*', | |
} | |
case request.path | |
when '/medal-table.json' | |
json = { | |
table: [ | |
{ countryName: "United States of America", gold: 39, silver: 41, bronze: 33 }, | |
{ countryName: "People's Republic of China", gold: 38, silver: 32, bronze: 19 }, | |
{ countryName: "ROC", gold: 20, silver: 28, bronze: 23 } | |
] | |
}.to_json | |
[200, headers, [json]] | |
else | |
[404, headers, [{ error: 404 }.to_json]] | |
end | |
end | |
end | |
run MyApp.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with
rackup
/puma
etc.