<commit_before>%% -------------------------------------------------------------------
%%
%% Copyright (c) 2015 Basho Technologies, Inc.  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.
%%
%% -------------------------------------------------------------------

-module(re_wm).

-export([resources/0,
         routes/0,
         dispatch/0,
         base_route/0]).

-export([rd_maybe_text/2,
         rd_maybe_text/3,
         rd_accepts/2,
         add_content/2,
         add_error/2,
         rd_content/2,
         rd_cluster_exists/1,
         rd_cluster/1,
         rd_node_exists/1,
         rd_node/1,
         maybe_atomize/1,
         maybe_to_list/1,
         url_decode/1
        ]).

-export([init/1,
         service_available/2,
         allowed_methods/2,
         content_types_provided/2,
         content_types_accepted/2,
         resource_exists/2,
         provide_content/2,
         delete_resource/2,
         process_post/2,
         provide_text_content/2,
         provide_static_content/2,
         accept_content/2,
         post_is_create/2,
         create_path/2,
         last_modified/2]).

-include_lib("webmachine/include/webmachine.hrl").
-include("re_wm.hrl").

-record(ctx, {route :: route()}).

%%%===================================================================
%%% API
%%%===================================================================

%%% Routing

-spec rd_maybe_text(term(), #wm_reqdata{}) -> {term(), #wm_reqdata{}}.
rd_maybe_text(Result, ReqData) ->
    rd_maybe_text(lines, Result, ReqData).

-spec rd_maybe_text(atom(), term(), #wm_reqdata{}) -> {term(), #wm_reqdata{}}.
rd_maybe_text(_, {error, Reason}, ReqData) ->
    rd_content({error, Reason}, ReqData);
rd_maybe_text(Key, Result, ReqData) ->
    case rd_accepts("plain/text", ReqData) of
        true ->
            Lines = [binary_to_list(L) || L <- proplists:get_value(Key, Result, [])],
            {string:join(Lines, io_lib:nl()), ReqData};
        _ ->
            rd_content(Result, ReqData)
    end.

-spec rd_accepts(string(), #wm_reqdata{}) -> boolean().
rd_accepts(CT, ReqData) ->
    case wrq:get_req_header("Accept", ReqData) of
        undefined ->
            true;
        Accept ->
            string:str(Accept,CT) > 0
    end.

-spec resources() -> [module()].
resources() ->
    [
     re_wm_explore,
     re_wm_control,
     re_wm_proxy,
     re_wm_static
    ].

-spec routes() -> [route()].
routes() ->
    routes(resources(), []).

-spec routes([module()], [route()]) -> [route()].
routes([], Routes) ->
    Routes;
routes([Resource|Rest], Routes) ->
    routes(Rest, Routes ++ Resource:routes()).

-spec dispatch() -> [{[string() | atom], module(), [term()]}].
dispatch() ->
    build_wm_routes(base_route(), routes(), []).

-spec base_route() -> string().
base_route() ->
    case riak_explorer:is_riak() of
        false -> [];
        true -> ["admin"]
    end.

%%% Utility

-spec add_content(term(), #wm_reqdata{}) -> {boolean(), #wm_reqdata{}}.
add_content({error, not_found}, ReqData) ->
    {{halt, 404}, ReqData};
add_content({error, Reason}, ReqData) ->
    {{halt, 500}, add_error(Reason, ReqData)};
add_content(ok, ReqData) ->
    {true, ReqData};
add_content(Content, ReqData) ->
    {true, wrq:append_to_response_body(mochijson2:encode(Content), ReqData)}.

-spec add_error(term(), #wm_reqdata{}) -> #wm_reqdata{}.
add_error(Error, ReqData) ->
    wrq:append_to_response_body(mochijson2:encode([{error, list_to_binary(io_lib:format("~p", [Error]))}]), ReqData).

-spec rd_content(term(), #wm_reqdata{}) -> 
                        {[{binary(), term()}], #wm_reqdata{}}.
rd_content({error, not_found}, ReqData) ->
    {{halt, 404}, ReqData};
rd_content({error, Reason}, ReqData) ->
    {{halt, 500}, add_error(Reason, ReqData)};
rd_content(Content, ReqData) -><commit_msg>fixing query output to be wrapped in query obj<commit_after>%% -------------------------------------------------------------------
%%
%% Copyright (c) 2015 Basho Technologies, Inc.  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.
%%
%% -------------------------------------------------------------------

-module(re_wm).

-export([resources/0,
         routes/0,
         dispatch/0,
         base_route/0]).

-export([rd_maybe_text/2,
         rd_maybe_text/3,
         rd_accepts/2,
         add_content/2,
         add_error/2,
         rd_content/2,
         rd_cluster_exists/1,
         rd_cluster/1,
         rd_node_exists/1,
         rd_node/1,
         maybe_atomize/1,
         maybe_to_list/1,
         url_decode/1
        ]).

-export([init/1,
         service_available/2,
         allowed_methods/2,
         content_types_provided/2,
         content_types_accepted/2,
         resource_exists/2,
         provide_content/2,
         delete_resource/2,
         process_post/2,
         provide_text_content/2,
         provide_static_content/2,
         accept_content/2,
         post_is_create/2,
         create_path/2,
         last_modified/2]).

-include_lib("webmachine/include/webmachine.hrl").
-include("re_wm.hrl").

-record(ctx, {route :: route()}).

%%%===================================================================
%%% API
%%%===================================================================

%%% Routing

-spec rd_maybe_text(term(), #wm_reqdata{}) -> {term(), #wm_reqdata{}}.
rd_maybe_text(Result, ReqData) ->
    rd_maybe_text(lines, Result, ReqData).

-spec rd_maybe_text(atom(), term(), #wm_reqdata{}) -> {term(), #wm_reqdata{}}.
rd_maybe_text(_, {error, Reason}, ReqData) ->
    rd_content({error, Reason}, ReqData);
rd_maybe_text(Key, Result, ReqData) ->
    case rd_accepts("plain/text", ReqData) of
        true ->
            Lines = [binary_to_list(L) || L <- proplists:get_value(Key, Result, [])],
            {string:join(Lines, io_lib:nl()), ReqData};
        _ ->
            rd_content(Result, ReqData)
    end.

-spec rd_accepts(string(), #wm_reqdata{}) -> boolean().
rd_accepts(CT, ReqData) ->
    case wrq:get_req_header("Accept", ReqData) of
        undefined ->
            true;
        Accept ->
            string:str(Accept,CT) > 0
    end.

-spec resources() -> [module()].
resources() ->
    [
     re_wm_explore,
     re_wm_control,
     re_wm_proxy,
     re_wm_static
    ].

-spec routes() -> [route()].
routes() ->
    routes(resources(), []).

-spec routes([module()], [route()]) -> [route()].
routes([], Routes) ->
    Routes;
routes([Resource|Rest], Routes) ->
    routes(Rest, Routes ++ Resource:routes()).

-spec dispatch() -> [{[string() | atom], module(), [term()]}].
dispatch() ->
    build_wm_routes(base_route(), routes(), []).

-spec base_route() -> string().
base_route() ->
    case riak_explorer:is_riak() of
        false -> [];
        true -> ["admin"]
    end.

%%% Utility

-spec add_content(term(), #wm_reqdata{}) -> {boolean(), #wm_reqdata{}}.
add_content({error, not_found}, ReqData) ->
    {{halt, 404}, ReqData};
add_content({error, Reason}, ReqData) ->
    {{halt, 500}, add_error(Reason, ReqData)};
add_content(ok, ReqData) ->
    {true, ReqData};
add_content(Content, ReqData) ->
    Tokens = string:tokens(wrq:path(ReqData), "/"),
    Last = lists:nth(length(Tokens), Tokens),
    {true, wrq:append_to_response_body(mochijson2:encode([{list_to_binary(Last), Content}]), ReqData)}.

-spec add_error(term(), #wm_reqdata{}) -> #wm_reqdata{}.
add_error(Error, ReqData) ->
    wrq:append_to_response_body(mochijson2:encode([{error, list_to_binary(io_lib:format("~p", [Error]))}]), ReqData).

-spec rd_content(term(), #wm_reqdata{}) -> 
                        {[{binary(), term()}], #wm_reqdata{}}.
rd_content({error, not_found}, ReqData) ->
    {{halt, 404}, ReqData};
rd_content({error, Reason}, ReqData) ->
    {{halt, 500}, add_error(Reason, ReqData)};
rd_content(Content, ReqData) ->
{'fraction_non_alphanumeric': 0.21704991587212563, 'fraction_numerical': 0.012338754907459339, 'mean_word_length': 2.7763659466327826, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, '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': '3339162', 'n_tokens_mistral': 2975, 'n_tokens_neox': 2883, 'n_words': 604}