Skip to content

Instantly share code, notes, and snippets.

@stevenschobert
Last active November 10, 2015 22:46
Show Gist options
  • Save stevenschobert/b3580e2866a283a4da16 to your computer and use it in GitHub Desktop.
Save stevenschobert/b3580e2866a283a4da16 to your computer and use it in GitHub Desktop.
Rack powered counter JSON API. For testing purposes.
require 'rack'
require 'json'
COUNTERS = {}
app = Proc.new do |env|
req = Rack::Request.new(env)
COUNTERS[req.path] ||= 0
if type = req.params["action"]
case type
when "increment"
COUNTERS[req.path] += 1
when "decrement"
COUNTERS[req.path] -= 1
end
end
[200, { "Content-Type" => "application/json" }, [ { counter: COUNTERS[req.path] }.to_json ]]
end
run app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment