iderlone / src / ide1_evaluate.erl

%%%-------------------------------------------------------------------
%%% @author orbitz <orbitz@osx.local>
%%% @copyright (C) 2011, orbitz
%%% @doc
%%% API for working with ideone
%%% @end
%%% Created : 18 Oct 2011 by orbitz <orbitz@osx.local>
%%%-------------------------------------------------------------------
-module(ide1_evaluate).

-record(account, {user, pass, language_list}).

%% API
-export([create_account/2,
         submit/6,
         get_submission_status/2,
         get_submission_details/2,
         evaluate/3,
         get_language/2]).

%%%===================================================================
%%% API
%%%===================================================================
%%--------------------------------------------------------------------
%% @doc
%% @spec
%% @end
%%--------------------------------------------------------------------
create_account(User, Pass) ->
    Language_list = ide1_dirt:call("getLanguages", [{"xsd:string", User}, {"xsd:string", Pass}]),
    {"languages", Languages} = lists:keyfind("languages", 1, Language_list),
    #account{user=User,
             pass=Pass,
             language_list=Languages}.

%%--------------------------------------------------------------------
%% @doc
%% @spec
%% @end
%%--------------------------------------------------------------------
get_language(Account, Language_name) when is_list(Language_name) ->
    case lists:keyfind(Language_name, 2, Account#account.language_list) of
        {Id, Language_name} ->
            {ok, Id};
        false ->
            {error, language_not_found}
    end.

%%--------------------------------------------------------------------
%% @doc
%% @spec
%% @end
%%--------------------------------------------------------------------
submit(Account, Sourcecode, Language_id, Input, Run, Private) ->
    Submit = ide1_dirt:call("createSubmission",
                            [{"xsd:string", Account#account.user},
                             {"xsd:string", Account#account.pass},
                             {"xsd:string", Sourcecode},
                             {"xsd:int", Language_id},
                             {"xsd:string", Input},
                             {"xsd:boolean", atom_to_list(Run)},
                             {"xsd:boolean", atom_to_list(Private)}]),
    case lists:keyfind("error", 1, Submit) of
        {"error", "OK"} ->
            {"link", Link} = lists:keyfind("link", 1, Submit),
            {ok, Link};
        {"error", Anything} ->
            {error, Anything};
        Anything ->
            {error, Anything}
    end.
                             
%%--------------------------------------------------------------------
%% @doc
%% @spec
%% @end
%%--------------------------------------------------------------------
get_submission_status(Account, Link) ->
    Submit = ide1_dirt:call("getSubmissionStatus",
                            [{"xsd:string", Account#account.user},
                             {"xsd:string", Account#account.pass},
                             {"xsd:string", Link}]),
    case lists:keyfind("error", 1, Submit) of
        {"error", "OK"} ->
            {"status", Status} = lists:keyfind("status", 1, Submit),
            {"result", Result} = lists:keyfind("result", 1, Submit),
            {ok, Status, Result};
        {"error", Anything} ->
            {error, Anything};
        Anything ->
            {error, Anything}
    end.

%%--------------------------------------------------------------------
%% @doc
%% @spec
%% @end
%%--------------------------------------------------------------------
get_submission_details(Account, Link) ->
    Submit = ide1_dirt:call("getSubmissionDetails",
                            [{"xsd:string", Account#account.user},
                             {"xsd:string", Account#account.pass},
                             {"xsd:string", Link},
                             {"xsd:boolean", "false"},
                             {"xsd:boolean", "false"},
                             {"xsd:boolean", "true"},
                             {"xsd:boolean", "true"},
                             {"xsd:boolean", "false"}]),
    case lists:keyfind("error", 1, Submit) of
        {"error", "OK"} ->
            {ok, Submit};
        {"error", Anything} ->
            {error, Anything};
        Anything ->
            {error, Anything}
    end.    

%%--------------------------------------------------------------------
%% @doc
%% @spec
%% @end
%%--------------------------------------------------------------------
evaluate(Account, Sourcecode, erlang) ->
    [Language_id | _] = [Language_id || {Language_id, Language_name} <- Account#account.language_list,
                                        string:str(Language_name, "Erlang") /= 0],
    Expression = lists:flatten(io_lib:format("-module(prog).~n" ++
                                                 "-export([main/0]).~n" ++
                                                 "main() ->~n" ++
                                                 "io:format(\"~~p~~n\",[~s]).~n", [Sourcecode])),
    case submit(Account, Expression, Language_id, "", true, false) of
        {ok, Link} ->
            case wait_for_completion(Account, Link) of
                {ok, Details} ->
                    {"output", Output} = lists:keyfind("output", 1, Details),
                    {ok, Output};
                Anything ->
                    Anything
            end;
        Anything ->
            Anything
    end;
evaluate(_Account, _Sourcecode, _Language) ->
    {error, language_not_supported}.

%%%===================================================================
%%% Internal functions
%%%===================================================================
wait_for_completion(Account, Link) ->
    case get_submission_details(Account, Link) of
        {ok, Details} ->
            case lists:keyfind("status", 1, Details) of
                {"status", 0} ->
                    {ok, Details};
                {"status", _} ->
                    timer:sleep(2000),
                    wait_for_completion(Account, Link)
            end;
        {error, Anything} ->
            {error, Anything}
    end.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.