Skip to content

Instantly share code, notes, and snippets.

@juliandunn
Created November 10, 2014 01:35
Show Gist options
  • Save juliandunn/5056688a6dc55994a3d8 to your computer and use it in GitHub Desktop.
Save juliandunn/5056688a6dc55994a3d8 to your computer and use it in GitHub Desktop.
-module(christmas_handler).
-behaviour(cowboy_http_handler).
-export([init/3]).
-export([handle/2]).
-export([terminate/3]).
-record(state, {}).
init(_, Req, _Opts) ->
{ok, Req, #state{}}.
handle(Req, State=#state{}) ->
{Date, Req2} = cowboy_req:header(<<"date">>,
Req,
<<"Thu, 01 Jan 1970 00:00:00 GMT">>),
ReqDate = httpd_util:convert_request_date(binary_to_list(Date)),
{DaysTillXmas, _} = calendar:time_difference(ReqDate,
{{2014,12,25},{0,0,0}}),
Message = if DaysTillXmas > 0 -> "Not Christmas yet!";
DaysTillXmas == 0 -> "Merry Christmas!";
DaysTillXmas < 0 -> "Oh no, you missed Christmas!"
end,
{ok, Req3} = cowboy_req:reply(200,
[{<<"content-type">>, <<"text/plain">>}],
Message,
Req2),
{ok, Req3, State}.
terminate(_Reason, _Req, _State) ->
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment