Skip to content

Instantly share code, notes, and snippets.

@msantos
Created November 15, 2010 01:53
Show Gist options
  • Select an option

  • Save msantos/676318 to your computer and use it in GitHub Desktop.

Select an option

Save msantos/676318 to your computer and use it in GitHub Desktop.
Erlang API for websequencediagrams.com
-module(wsd).
-export([start/0, render/0, render/1]).
-export([body/1, request/1, fetch/1]).
-define(STYLE, "napkin").
-define(MESSAGE, "Alice->Bob: Authentication Request\n"
"note right of Bob: Bob thinks about it\n"
"Bob->Alice: Authentication Response\n").
-define(URI, "http://www.websequencediagrams.com/").
-define(CONTENT_TYPE, "application/x-www-form-urlencoded").
start() ->
inets:start().
render() ->
render([]).
render(Opt) when is_list(Opt) ->
Body = body(Opt),
Image = request(Body),
fetch(Image).
body(Opt) when is_list(Opt) ->
Style = edoc_lib:escape_uri(proplists:get_value(style, Opt, ?STYLE)),
Message = edoc_lib:escape_uri(proplists:get_value(message, Opt, ?MESSAGE)),
% edoc encodes "\n" as "%a", app expects "%0a"
"style=" ++ Style ++ "&message=" ++
re:replace(Message, "%a", "%0a", [global, {return, list}]).
request(Body) ->
{ok, {{_,200,_}, _Headers, JSON}} = httpc:request(post,
{?URI ++ "index.php", [], ?CONTENT_TYPE, Body}, [], []),
{match, [Image]} = re:run(JSON, "img:\s+\"\\?img=([\\d\\w]+)",
[{capture, [1], list}]),
Image.
fetch(Image) ->
File = Image ++ ".png",
{ok,saved_to_file} = httpc:request(get,
{?URI ++ "?img=" ++ Image, []}, [], [{stream, File}]),
{ok, File}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment