Commits

orbitz committed fd98b13

tryerlang support added

Comments (0)

Files changed (4)

Binary file added.
+{lib_dirs, ["deps"]}.
+
+{deps, [{ibrowse, "2.2.0", {git, "https://github.com/cmullaparthi/ibrowse.git", {branch, "master"}}},
+        {jsx, "0.10.0", {git, "https://github.com/talentdeficit/jsx.git", {branch, "master"}}}]}.
+
+
+

src/tryerlang.app.src

+{application, tryerlang,
+ [
+  {description, "Interface for tryerlang.org"},
+  {vsn, "0.0.1"},
+  {registered, []},
+  {applications, [
+                  kernel,
+                  stdlib,
+		  jsx,
+		  ibrowse
+                 ]},
+  {modules, [tryerlang]},
+  {env, []}
+ ]}.

src/tryerlang.erl

+%%%-------------------------------------------------------------------
+%%% @author orbitz <orbitz@osx.local>
+%%% @copyright (C) 2011, orbitz
+%%% @doc
+%%% Provides a library interface for executing Erlang expressions through
+%%% tryerlang.org's REST api
+%%% @end
+%%% Created : 17 Oct 2011 by orbitz <orbitz@osx.local>
+%%%-------------------------------------------------------------------
+-module(tryerlang).
+
+-define(EVALUATE_URL, "http://www.tryerlang.org/api/evaluate").
+
+%% API
+-export([evaluate/1]).
+
+
+%%%===================================================================
+%%% API
+%%%===================================================================
+
+%%--------------------------------------------------------------------
+%% @doc
+%% Evaluates an expression through tryerlang.org's rest interface.
+%% Returns {ok, Response}.
+%% @spec
+%% @end
+%%--------------------------------------------------------------------
+evaluate(Expr) ->
+    Url_expr = ibrowse_lib:url_encode(Expr),
+    {ok, "200", _Headers, Body_json} = ibrowse:send_req(?EVALUATE_URL, [], post, "expression=" ++ Url_expr),
+    Body = jsx:json_to_term(list_to_binary(Body_json)),
+    {<<"result">>, Result} = lists:keyfind(<<"result">>, 1, Body),
+    {ok, Result}.
+
+%%%===================================================================
+%%% Internal functions
+%%%===================================================================