-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonurls.erl
42 lines (35 loc) · 966 Bytes
/
jsonurls.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
%%
%% Read json data, extract urls from it and return a List
%%
%% DynVars used :
%% - jsonurls
%%
%% [ {"url": "/foobar/index?page=0"},
%% {"url": "/foobar/index?page=1"} ]
%% will return an erlang List as :
%% ["/foobar/index?page=0","/foobar/index?page=1"]
%%
-module(jsonurls).
-include_lib("eunit/include/eunit.hrl").
-export([get_urls/1]).
-author({author, "Rodolphe Quiedeville", "<[email protected]>"}).
get_urls({_Pid, DynVars})->
fillurls(decode(DynVars)).
decode(DynVars)->
case ts_dynvars:lookup(jsonurls, DynVars) of
{ok, Val} -> mochijson2:decode(Val);
false -> []
end.
fillurls(List) ->
fillurls([],List).
fillurls(Urls,[])->
Urls;
fillurls(Urls,[H|T])->
lists:merge([url(H)], fillurls(Urls,T)).
url(Elmt) ->
{struct, JsonData} = Elmt,
Value = proplists:get_value(<<"url">>, JsonData),
case is_binary(Value) of
true -> binary_to_list(Value);
false -> Value
end.