<commit_before>%%
%% Copyright (c) 2013 <NAME>  All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%%% -------------------------------------------------------------------
%%% Author  : Ulf <EMAIL>
%%% Description :
%%%
%%% Created : 
%%% -------------------------------------------------------------------
-module(funrunner_new_resource).

%% --------------------------------------------------------------------
%% External exports
%% --------------------------------------------------------------------
-export([init/1, to_html/2, to_json/2, content_types_provided/2, allowed_methods/2, resource_exists/2]).
-export([post_is_create/2, process_post/2, finish_request/2]).
-export([is_authorized/2]).
%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------
-include_lib("webmachine/include/webmachine.hrl").
%% --------------------------------------------------------------------
%% record definitions
%% --------------------------------------------------------------------
-record(context, {}).
%% --------------------------------------------------------------------
%%% Internal functions
%% --------------------------------------------------------------------
init(Config) -> 
    %%{{trace, "/tmp"}, Config}.
    {ok, #context{}}.
%
% Returning non-true values will result in 404 Not Found.
% 
resource_exists(ReqData, Context) ->
    {true, ReqData, Context}.

%
% true, if the service is available
%
service_available(ReqData, Context) ->
    {true, ReqData, Context}.

%
% If this returns anything other than true, the response will be 401 Unauthorized.
% The AuthHead return value will be used as the value in the WWW-Authenticate header.
%
is_authorized(ReqData, Context) ->
    login:login(ReqData, Context). 

forbidden(ReqData, Context) ->
    {false, ReqData, Context}.

%
% If the resource accepts POST requests to nonexistent resources, 
% then this should return true.
%
allow_missing_post(ReqData, Context) ->
    {false, ReqData, Context}.

malformed_request(ReqData, Context) ->
    {false, ReqData, Context}.

uri_too_long(ReqData, Context) ->
    {false, ReqData, Context}.

known_content_type(ReqData, Context) ->
    {true, ReqData, Context}.

valid_entity_length(ReqData, Context) ->
    {true, ReqData, Context}.
%
% If the OPTIONS method is supported and is used, the return value of this 
% function is expected to be a list of pairs representing header names and 
% values that should appear in the response.
%
options(ReqData, Context) ->
    {[], ReqData, Context}.
valid_content_headers(ReqData, Context) ->
    {true, ReqData, Context}.
%
% If a Method not in this list is requested, then a 405 Method Not Allowed
% will be sent. Note that these are all-caps and are atoms. (single-quoted)
%
allowed_methods(ReqData, Context) ->
    {['GET', 'POST'], ReqData, Context}.
%
% This is called when a DELETE request should be enacted 
% and should return true if the deletion succeeded.
%
delete_resource(ReqData, Context) ->
    {false, ReqData, Context}.
%
% This is only called after a successful delete_resource call, and should 
% return false if the deletion was accepted but cannot yet be guaranteed to have finished.
%
delete_completed(ReqData, Context) ->
    {true, ReqData, Context}.
%
% If POST requests should be treated as a request to put content into a 
% (potentially new) resource as opposed to being a generic submission for 
% processing, then this function should return true. If it does return true, 
% then create_path will be called and the rest of the request will be treated 
% much like a PUT to the Path entry returned by that call.
post_is_create(ReqData, Context) ->
    {false, ReqData, Context}.
%
% This will be called on a POST request if post_is_create returns true. 
% It is an error for this function to not produce a Path if post_is_create returns true. 
% The Path returned should be a valid URI part following the dispatcher prefix. 
% That Path will replace the previous one in the return value of wrq:disp_path(ReqData) 
% for all subsequent resource function calls in the course of this request.
%
create_path(ReqData, Context) ->
    {undefined, ReqData, Context}.
%
% If post_is_create returns false, then this will be called to process any POST requests. 
% If it succeeds, it should return true.
%
process_post(ReqData, Context) ->
    Body = mochiweb_util:parse_qs(wrq:req_body(ReqData)),
    lager:info("body : ~p", [Body]),
    Button = get_value("button", Body),
    handle_post(Button, Body),
    {true, ReqData, Context}.

handle_post("cancel", Body) ->
    ok;
handle_post("save", Body) ->    
    {"fname", Name} = lists:keyfind("fname",1, Body),
    {"fnode", Node} = lists:keyfind("fnode",1, Body),
    {"fdriver", Driver} = lists:keyfind("fdriver",1, Body),
    {"fid", Id} = lists:keyfind("fid",1, Body),
    {"ffun", Fun} = lists:keyfind("ffun",1, Body),
    {"fcomment", Comment} = lists:keyfind("fcomment",1, Body),    
    Msg = save(Name, Node, Driver, Id, Fun, Comment),
    sender_util:send_message(Msg).
<commit_msg>refactoring<commit_after>%%
%% Copyright (c) 2013 <NAME>  All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%%% -------------------------------------------------------------------
%%% Author  : Ulf <EMAIL>
%%% Description :
%%%
%%% Created : 
%%% -------------------------------------------------------------------
-module(funrunner_new_resource).

%% --------------------------------------------------------------------
%% External exports
%% --------------------------------------------------------------------
-export([init/1, to_html/2, to_json/2, content_types_provided/2, allowed_methods/2, resource_exists/2]).
-export([post_is_create/2, process_post/2, finish_request/2]).
-export([is_authorized/2]).
%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------
-include_lib("webmachine/include/webmachine.hrl").
%% --------------------------------------------------------------------
%% record definitions
%% --------------------------------------------------------------------
-record(context, {}).
%% --------------------------------------------------------------------
%%% Internal functions
%% --------------------------------------------------------------------
init(Config) -> 
    %%{{trace, "/tmp"}, Config}.
    {ok, #context{}}.
%
% Returning non-true values will result in 404 Not Found.
% 
resource_exists(ReqData, Context) ->
    {true, ReqData, Context}.

%
% true, if the service is available
%
service_available(ReqData, Context) ->
    {true, ReqData, Context}.

%
% If this returns anything other than true, the response will be 401 Unauthorized.
% The AuthHead return value will be used as the value in the WWW-Authenticate header.
%
is_authorized(ReqData, Context) ->
    login:login(ReqData, Context). 

forbidden(ReqData, Context) ->
    {false, ReqData, Context}.

%
% If the resource accepts POST requests to nonexistent resources, 
% then this should return true.
%
allow_missing_post(ReqData, Context) ->
    {false, ReqData, Context}.

malformed_request(ReqData, Context) ->
    {false, ReqData, Context}.

uri_too_long(ReqData, Context) ->
    {false, ReqData, Context}.

known_content_type(ReqData, Context) ->
    {true, ReqData, Context}.

valid_entity_length(ReqData, Context) ->
    {true, ReqData, Context}.
%
% If the OPTIONS method is supported and is used, the return value of this 
% function is expected to be a list of pairs representing header names and 
% values that should appear in the response.
%
options(ReqData, Context) ->
    {[], ReqData, Context}.
valid_content_headers(ReqData, Context) ->
    {true, ReqData, Context}.
%
% If a Method not in this list is requested, then a 405 Method Not Allowed
% will be sent. Note that these are all-caps and are atoms. (single-quoted)
%
allowed_methods(ReqData, Context) ->
    {['GET', 'POST'], ReqData, Context}.
%
% This is called when a DELETE request should be enacted 
% and should return true if the deletion succeeded.
%
delete_resource(ReqData, Context) ->
    {false, ReqData, Context}.
%
% This is only called after a successful delete_resource call, and should 
% return false if the deletion was accepted but cannot yet be guaranteed to have finished.
%
delete_completed(ReqData, Context) ->
    {true, ReqData, Context}.
%
% If POST requests should be treated as a request to put content into a 
% (potentially new) resource as opposed to being a generic submission for 
% processing, then this function should return true. If it does return true, 
% then create_path will be called and the rest of the request will be treated 
% much like a PUT to the Path entry returned by that call.
post_is_create(ReqData, Context) ->
    {false, ReqData, Context}.
%
% This will be called on a POST request if post_is_create returns true. 
% It is an error for this function to not produce a Path if post_is_create returns true. 
% The Path returned should be a valid URI part following the dispatcher prefix. 
% That Path will replace the previous one in the return value of wrq:disp_path(ReqData) 
% for all subsequent resource function calls in the course of this request.
%
create_path(ReqData, Context) ->
    {undefined, ReqData, Context}.
%
% If post_is_create returns false, then this will be called to process any POST requests. 
% If it succeeds, it should return true.
%
process_post(ReqData, Context) ->
    Body = mochiweb_util:parse_qs(wrq:req_body(ReqData)),
    %lager:info("body : ~p", [Body]),
    Button = get_value("button", Body),
    handle_post(Button, Body),
    {true, ReqData, Context}.

handle_post("cancel", Body) ->
    ok;
handle_post("save", Body) ->    
    {"fname", Name} = lists:keyfind("fname",1, Body),
    {"fnode", Node} = lists:keyfind("fnode",1, Body),
    {"fdriver", Driver} = lists:keyfind("fdriver",1, Body),
    {"fid", Id} = lists:keyfind("fid",1, Body),
    {"ffun", Fun} = lists:keyfind("ffun",1, Body),
    {"fcomment", Comment} = lists:keyfind("fcomment",1, Body),    
    Msg = save(Name, Node, Driver, Id, Fun, Comment),
    sender_util:send_message(Msg).

{'fraction_non_alphanumeric': 0.22528877524027863, 'fraction_numerical': 0.005819592628516004, 'mean_word_length': 2.4568729046022555, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7235840', 'n_tokens_mistral': 3112, 'n_tokens_neox': 3051, 'n_words': 1132}