Skip to content

Commit

Permalink
implement some tips from tidifier
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelog committed Mar 17, 2012
1 parent 8e2c4b2 commit a48b944
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 214 deletions.
213 changes: 106 additions & 107 deletions src/erlagi.erl

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions src/erlagi_debug.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

-export( [ print_result/2, print_agicall/1 ] ).

print_result(Log, Result) when is_record(Result, agiresult) ->
print_result(Log, #agiresult{} = Result) ->
erlagi_log:log(Log, "----- AgiResult -----~n"),
erlagi_log:log(Log, "Command: ~s~n", [ erlagi_result:get_cmd(Result) ]),
erlagi_log:log(Log, "Raw: ~s~n", [ erlagi_result:get_raw(Result) ]),
Expand All @@ -26,15 +26,13 @@ print_result(Log, Result) when is_record(Result, agiresult) ->
erlagi_log:log(Log, "---------------------~n")
.

print_agicall(Call) when is_record(Call, agicall) ->
Log = Call#agicall.log,
print_variable(Log, {Key, Value}) ->
erlagi_log:log(Log, "Variable [ ~s = ~s ]~n", [ Key, Value ])
.

print_agicall(#agicall{environment = Env, log = Log}) ->
erlagi_log:log(Log, "----- AgiCall -----~n"),
lists:map(
fun({Key, Value}) ->
erlagi_log:log(Log, "Variable [ ~s = ~s ]~n", [ Key, Value ])
end,
Call#agicall.environment
),
[ print_variable(Log, V) || V <- Env],
erlagi_log:log(Log, "-------------------~n")
.

Expand Down
84 changes: 42 additions & 42 deletions src/erlagi_demo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,60 @@
-export( [ run/1 ]).

run_command(Module, Function, Arguments) ->
[ Call | _ ] = Arguments,
[Call | _] = Arguments,
Log = Call#agicall.log,
Result = erlang:apply(Module, Function, Arguments),
case is_record(Result, agiresult) of
true -> erlagi_debug:print_result(Log, erlang:apply(Module, Function, Arguments));
false -> erlagi_log:log(Log, "~s~n", [ Result ])
false -> erlagi_log:log(Log, "~s~n", [Result])
end
.

run(Call) when is_record(Call, agicall) ->
run_command(erlagi_debug,print_agicall, [ Call ]),
run_command(erlagi, answer, [ Call ]),
run_command(erlagi, enable_music, [ Call ]),
run_command(erlagi, disable_music, [ Call ]),
run_command(erlagi, set_callerid, [ Call, "Name", "123" ]),
run_command(erlagi, say_digits, [ Call, "123", "#" ]),
run_command(erlagi, say_number, [ Call, "123", "#" ]),
run_command(erlagi, wait_digit, [ Call ]),
run_command(erlagi, set_variable, [ Call, "Some", "Value" ]),
run_command(erlagi, log_notice, [ Call, erlagi:get_variable(Call, "Some") ]),
run_command(erlagi, log_notice, [ Call, erlagi:get_full_variable(Call, "Some") ]),
run_command(erlagi, set_variable, [ Call, "Some", "\"Value\"" ]),
run_command(erlagi, log_notice, [ Call, erlagi:get_variable(Call, "Some") ]),
run_command(erlagi, log_notice, [ Call, erlagi:get_full_variable(Call, "Some") ]),
run_command(erlagi, log_notice, [ Call, "Hello Joe" ]),
run_command(erlagi, log_debug, [ Call, "Hello \"Mike\"" ]),
run_command(erlagi, log_warn, [ Call, "Hi" ]),
run_command(erlagi, log_error, [ Call, "Hi" ]),
run_command(erlagi, log_verbose, [ Call, "Hi" ]),
run_command(erlagi, log_dtmf, [ Call, "Hi" ]),
run_command(erlagi, database_deltree, [ Call, "Blah" ]),
run_command(erlagi, database_deltree, [ Call, "Blah", "bleh" ]),
%run_command(erlagi, set_auto_hangup, [ Call, "3" ]),
run_command(erlagi, stream_file, [ Call, "welcome", "#" ]),
run_command(erlagi, stop_play_tones, [ Call ]),
run_command(erlagi, play_custom_tones, [ Call, [
run_command(erlagi_debug,print_agicall, [Call]),
run_command(erlagi, answer, [Call]),
run_command(erlagi, enable_music, [Call]),
run_command(erlagi, disable_music, [Call]),
run_command(erlagi, set_callerid, [Call, "Name", "123"]),
run_command(erlagi, say_digits, [Call, "123", "#"]),
run_command(erlagi, say_number, [Call, "123", "#"]),
run_command(erlagi, wait_digit, [Call ]),
run_command(erlagi, set_variable, [Call, "Some", "Value"]),
run_command(erlagi, log_notice, [Call, erlagi:get_variable(Call, "Some")]),
run_command(erlagi, log_notice, [Call, erlagi:get_full_variable(Call, "Some")]),
run_command(erlagi, set_variable, [Call, "Some", "\"Value\""]),
run_command(erlagi, log_notice, [Call, erlagi:get_variable(Call, "Some")]),
run_command(erlagi, log_notice, [Call, erlagi:get_full_variable(Call, "Some")]),
run_command(erlagi, log_notice, [Call, "Hello Joe"]),
run_command(erlagi, log_debug, [Call, "Hello \"Mike\""]),
run_command(erlagi, log_warn, [Call, "Hi"]),
run_command(erlagi, log_error, [Call, "Hi"]),
run_command(erlagi, log_verbose, [Call, "Hi"]),
run_command(erlagi, log_dtmf, [Call, "Hi"]),
run_command(erlagi, database_deltree, [Call, "Blah"]),
run_command(erlagi, database_deltree, [Call, "Blah", "bleh"]),
%run_command(erlagi, set_auto_hangup, [Call, "3"]),
run_command(erlagi, stream_file, [Call, "welcome", "#"]),
run_command(erlagi, stop_play_tones, [Call]),
run_command(erlagi, play_custom_tones, [Call, [
"!350+440/100","!0/100","!350+440/100","!0/100","!350+440/100","!0/100","350+440"
] ]),
]]),
timer:sleep(2000),
run_command(erlagi, stop_play_tones, [ Call ]),
run_command(erlagi, play_busy, [ Call ]),
run_command(erlagi, indicate_busy, [ Call, "2" ]),
run_command(erlagi, stop_play_tones, [Call]),
run_command(erlagi, play_busy, [Call]),
run_command(erlagi, indicate_busy, [Call, "2"]),
timer:sleep(2000),
run_command(erlagi, stop_play_tones, [ Call ]),
run_command(erlagi, play_congestion, [ Call ]),
run_command(erlagi, indicate_congestion, [ Call, "2" ]),
run_command(erlagi, stop_play_tones, [Call]),
run_command(erlagi, play_congestion, [Call]),
run_command(erlagi, indicate_congestion, [Call, "2"]),
timer:sleep(2000),
run_command(erlagi, stop_play_tones, [ Call ]),
run_command(erlagi, play_dial, [ Call ]),
run_command(erlagi, stop_play_tones, [Call]),
run_command(erlagi, play_dial, [Call]),
timer:sleep(2000),
run_command(erlagi, stop_play_tones, [ Call ]),
run_command(erlagi, record, [ Call, "file", "wav", "#", "3000" ]),
run_command(erlagi, dial, [ Call, "SIP/54115555555", "3", "hH" ]),
run_command(erlagi, hangup, [ Call ]),
run_command(erlagi, terminate, [ Call ])
run_command(erlagi, stop_play_tones, [Call]),
run_command(erlagi, record, [Call, "file", "wav", "#", "3000"]),
run_command(erlagi, dial, [Call, "SIP/54115555555", "3", "hH"]),
run_command(erlagi, hangup, [Call]),
run_command(erlagi, terminate, [Call])
.

18 changes: 9 additions & 9 deletions src/erlagi_io.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@

-export( [ close/1, agi_rw/2, agi_rw/3 ] ).

close(Call) when is_record(Call, agicall) ->
close(#agicall{} = Call) ->
F = Call#agicall.close,
F()
.

send(Call, Text) when is_record(Call, agicall) ->
send(#agicall{} = Call, Text) ->
F = Call#agicall.send,
F(Text)
.

recv(Call) when is_record(Call, agicall) ->
recv(#agicall{} = Call) ->
F = Call#agicall.read,
F()
.

quote_word(Text) ->
erlagi_misc:concat([ [ 34 ], Text, [ 34 ] ])
erlagi_misc:concat([[34], Text, [34]])
.

escape_quotes(Text) ->
lists:map(
fun(Char) ->
case Char of
34 -> [ 92, 34 ];
34 -> [92, 34];
_ -> Char
end
end,
Expand All @@ -50,20 +50,20 @@ quote_arguments(Arguments) when is_list(Arguments) ->
.

form_arguments(Arguments) when is_list(Arguments) ->
string:join(quote_arguments(Arguments), [ 32 ])
string:join(quote_arguments(Arguments), [32])
.

form_agi_cmd(Command, Arguments) when is_list(Arguments) ->
erlagi_misc:concat([ Command, [ 32 ], form_arguments(Arguments) ])
erlagi_misc:concat([ Command, [32], form_arguments(Arguments) ])
.

remove_eol(Text) ->
Text -- [ 10 ] % \n
Text -- [10] % \n
.

agi_rw(Call, Command, Arguments) when is_list(Arguments), is_record(Call, agicall) ->
Cmd = form_agi_cmd(Command, Arguments),
send(Call, erlagi_misc:concat( [ Cmd, [ 10 ] ])),
send(Call, erlagi_misc:concat([Cmd, [10]])),
Result = erlagi_result:parse_result(Cmd, remove_eol(recv(Call))),
Result
.
Expand Down
2 changes: 1 addition & 1 deletion src/erlagi_io_normal.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ get_close_fun() ->

get_send_fun() ->
fun(Text) ->
io:format("~s", [ Text ])
io:format("~s", [Text])
end
.

Expand Down
10 changes: 5 additions & 5 deletions src/erlagi_log.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
-export( [ open/1, log/2, log/3, close/1, get_logger/1 ] ).

open(Filename) ->
{ok, IoDevice} = file:open(Filename, [ append ]),
{ok, IoDevice} = file:open(Filename, [append]),
IoDevice
.

log(Log, Format, Data) when is_record(Log, agilog), is_list(Data) ->
log(#agilog{} = Log, Format, Data) when is_list(Data) ->
io:format(Log#agilog.iodevice, Format, Data)
.

log(Log, Format) when is_record(Log, agilog) ->
log(#agilog{} = Log, Format) ->
log(Log, Format, [])
.

close(Log) ->
ok = file:close(Log#agilog.iodevice)
close(#agilog{iodevice = Iodevice}) ->
ok = file:close(Iodevice)
.

get_logger(Filename) ->
Expand Down
6 changes: 3 additions & 3 deletions src/erlagi_misc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ concat([], Tail) ->
Tail
;

concat([ H | T ], Tail) ->
[ H | concat(T, Tail) ]
concat([H | T], Tail) ->
[H | concat(T, Tail)]
.

concat([]) ->
[]
;

concat([ H | T ]) ->
concat([H | T]) ->
concat(H, concat(T))
.

4 changes: 2 additions & 2 deletions src/erlagi_options.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
-export( [ get_option/2 ] ).

get_option(Key, Options) ->
Value = [ X || { CandidateKey, X } <- Options, Key =:= CandidateKey ],
Value = [X || { CandidateKey, X } <- Options, Key =:= CandidateKey],
case Value of
[] -> get_default_option(Key);
[ H | _ ] -> H
[H | _] -> H
end
.

Expand Down
11 changes: 4 additions & 7 deletions src/erlagi_read_env.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
-export( [ read/1 ] ).

split_lines(Text) ->
string:tokens(Text, [ 10 ])
string:tokens(Text, [10])
.

%% Returns a tuple, { Key, Value }
Expand All @@ -23,19 +23,16 @@ parse_variable(Text) ->
.

parse_variables(Text) ->
lists:map(fun(X) -> parse_variable(X) end, split_lines(Text))
[parse_variable(X) || X <- split_lines(Text)]
.

find_end_of_variables(Text) ->
string:str(Text, [ 10, 10 ])
string:str(Text, [10, 10])
.

has_end_of_variables(Text) ->
Index = find_end_of_variables(Text),
case Index of
0 -> false;
_ -> true
end
Index =/= 0
.

remove_end_of_variables(Text) ->
Expand Down
Loading

0 comments on commit a48b944

Please sign in to comment.