Skip to content

Instantly share code, notes, and snippets.

@tjmw
Created February 29, 2024 09:36
Show Gist options
  • Save tjmw/51fa1399d841f1fec15497b188779ea1 to your computer and use it in GitHub Desktop.
Save tjmw/51fa1399d841f1fec15497b188779ea1 to your computer and use it in GitHub Desktop.
Tiny rack app to fake a JSON API
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
@tjmw
Copy link
Author

tjmw commented Feb 29, 2024

Run with rackup/puma etc.

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