1
0
Fork 0
mirror of https://github.com/edgurgel/httparrot synced 2025-04-09 11:42:33 -04:00
httparrot/lib/httparrot/delayed_handler.ex
2014-01-05 20:32:14 -03:00

37 lines
815 B
Elixir

defmodule HTTParrot.DelayedHandler do
alias HTTParrot.GeneralRequestInfo
def init(_transport, _req, _opts) do
{:upgrade, :protocol, :cowboy_rest}
end
def allowed_methods(req, state) do
{["GET"], req, state}
end
def malformed_request(req, state) do
{n, req} = :cowboy_req.binding(:n, req)
try do
n = n |> binary_to_integer |> min(10) |> max(0)
{false, req, n}
rescue
ArgumentError -> {true, req, state}
end
end
def content_types_provided(req, state) do
{[{{"application", "json", []}, :get_json}], req, state}
end
def get_json(req, n) do
{info, req} = GeneralRequestInfo.retrieve(req)
:timer.sleep(n*1000)
{response(info), req, n}
end
defp response(info) do
info |> JSEX.encode!
end
def terminate(_, _, _), do: :ok
end