<commit_before>%%%-------------------------------------------------------------------
%%% @author <NAME> (<EMAIL>)
%%% @copyright 2011, Hypernumbers Ltd
%%% @doc Utilities for processing Twilio requests
%%%
%%% @end
%%%-------------------------------------------------------------------
-module(twilio_web_util).

-export([
         get_recording/1,
         process_query/1,
         process_body/1,
         get_type/1,
         process_proplist/1,
         country_code_to_prefix/1,
         country_code_to_country/1,
         prefix_to_country/1,
         prefix_to_country_code/1,
         pretty_print/1,
         add_prefix/2
        ]).

-include("twilio_web.hrl").

-define(UQ, mochiweb_util:unquote).

add_prefix("0" ++ Number, Prefix) -> Prefix ++ Number.

country_code_to_prefix(CC) ->
    CC2 = string:to_upper(CC),
    {_Country, CC2, Prefix} = lists:keyfind(CC2, 2, ?CCLOOKUP),
    Prefix.

country_code_to_country(CC) ->
    CC2 = string:to_upper(CC),
    {Country, CC2, _Prefix} = lists:keyfind(CC2, 2, ?CCLOOKUP),
    Country.

prefix_to_country("00" ++ Prefix) ->
    prefix_to_country(Prefix);
prefix_to_country("+" ++ Prefix) ->
    prefix_to_country(Prefix);
prefix_to_country(Prefix) when is_list(Prefix) ->
    prefix_to_country(list_to_integer(Prefix));
prefix_to_country(Prefix) ->
    {Country, _CC, Prefix} = lists:keyfind(Prefix, 3, ?CCLOOKUP),
    Country.

prefix_to_country_code("00" ++ Prefix) ->
    prefix_to_country_code(Prefix);
prefix_to_country_code("+" ++ Prefix) ->
    prefix_to_country_code(Prefix);
prefix_to_country_code(Prefix) when is_list(Prefix) ->
    prefix_to_country_code(list_to_integer(Prefix));
prefix_to_country_code(Prefix) ->
    {_Country, CC, Prefix} = lists:keyfind(Prefix, 3, ?CCLOOKUP),
    CC.

get_recording(#twilio{recording = R}) -> R#twilio_recording.recording_url.

get_type(#twilio{called = null, caller = null, from = null,
                 to = null, call_duration = null}) ->
    "start outbound";
get_type(#twilio{direction = "inbound", call_status = "ringing",
                 called = C, caller = Cr, from = Fr, to = To,
                 call_duration = null, inprogress = null, recording = null})
  when C =/= null andalso Cr =/= null andalso Fr =/= null andalso To =/= null ->
    "start inbound";
get_type(#twilio{direction = "inbound", call_status = "in-progress",
                 called = C, caller = Cr, from = Fr, to = To,
                call_duration = null, inprogress = Ip, recording = Rc})
  when C =/= null andalso Cr =/= null andalso Fr =/= null andalso To =/= null<commit_msg>strong typing for starting outbound calls - and handling the case when a named client makes them<commit_after>%%%-------------------------------------------------------------------
%%% @author <NAME> (<EMAIL>)
%%% @copyright 2011, Hypernumbers Ltd
%%% @doc Utilities for processing Twilio requests
%%%
%%% @end
%%%-------------------------------------------------------------------
-module(twilio_web_util).

-export([
         get_recording/1,
         process_query/1,
         process_body/1,
         get_type/1,
         process_proplist/1,
         country_code_to_prefix/1,
         country_code_to_country/1,
         prefix_to_country/1,
         prefix_to_country_code/1,
         pretty_print/1,
         add_prefix/2
        ]).

-include("twilio_web.hrl").

-define(UQ, mochiweb_util:unquote).

add_prefix("0" ++ Number, Prefix) -> Prefix ++ Number.

country_code_to_prefix(CC) ->
    CC2 = string:to_upper(CC),
    {_Country, CC2, Prefix} = lists:keyfind(CC2, 2, ?CCLOOKUP),
    Prefix.

country_code_to_country(CC) ->
    CC2 = string:to_upper(CC),
    {Country, CC2, _Prefix} = lists:keyfind(CC2, 2, ?CCLOOKUP),
    Country.

prefix_to_country("00" ++ Prefix) ->
    prefix_to_country(Prefix);
prefix_to_country("+" ++ Prefix) ->
    prefix_to_country(Prefix);
prefix_to_country(Prefix) when is_list(Prefix) ->
    prefix_to_country(list_to_integer(Prefix));
prefix_to_country(Prefix) ->
    {Country, _CC, Prefix} = lists:keyfind(Prefix, 3, ?CCLOOKUP),
    Country.

prefix_to_country_code("00" ++ Prefix) ->
    prefix_to_country_code(Prefix);
prefix_to_country_code("+" ++ Prefix) ->
    prefix_to_country_code(Prefix);
prefix_to_country_code(Prefix) when is_list(Prefix) ->
    prefix_to_country_code(list_to_integer(Prefix));
prefix_to_country_code(Prefix) ->
    {_Country, CC, Prefix} = lists:keyfind(Prefix, 3, ?CCLOOKUP),
    CC.

get_recording(#twilio{recording = R}) -> R#twilio_recording.recording_url.

% this is the start of an outbound call from a client with outgoing only
get_type(#twilio{direction = "inbound", call_status = "ringing",
                 called = null, caller = null, from = null,
                 to = null, call_duration = null}) ->
    "start outbound";
% this is the start of an outbound call from a client with both
% outgoing and incoming capabilities
get_type(#twilio{direction = "inbound", call_status = "ringing",
                 called = null, caller = Cr, from = Fr, to = null,
                 call_duration = null, inprogress = null, recording = null})
  when Cr =/= null andalso Fr =/= null ->
    "start outbound";
get_type(#twilio{direction = "inbound", call_status = "ringing",
                 called = C, caller = Cr, from = Fr, to = To,
                 call_duration = null, inprogress = null, recording = null})
  when C =/= null andalso Cr =/= null andalso Fr =/= null andalso To =/= null ->
    "start inbound";
get_type(#twilio{direction = "inbound", call_status = "in-progress",
                 called = C, caller = Cr, from = Fr, to = To,
                call_duration = null, inprogress = Ip, recording = Rc})
  when C =/= null andalso Cr =/= null andalso Fr =/= null andalso To =/= null
{'fraction_non_alphanumeric': 0.17692173609900644, 'fraction_numerical': 0.010458427749694962, 'mean_word_length': 2.716321243523316, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '736757', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1864, 'n_words': 413}