Skip to content

Commit

Permalink
Fix getting items helper and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tgrk committed May 19, 2015
1 parent 896b113 commit 309e3cf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
35 changes: 19 additions & 16 deletions src/erlpocket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,12 @@ retrieve(ConsumerKey, AccessToken, Query) ->

-spec stats(string(),string()) -> result_stats().
stats(ConsumerKey, AccessToken) ->
Templates = [
{total_items, [{state, all}]},
Templates = [{total_items, [{state, all}]},
{total_unread, [{state, unread}]},
{total_archive, [{state, archive}]},
{total_favorite, [{favorite, 1}]},
{total_articles, [{contentType, article}]},
{total_videos, [{contentType, video}]}
],
{total_videos, [{contentType, video}]}],
[get_stats(ConsumerKey, AccessToken, T) || T <- Templates].

-spec add(string(),string(),string(),string()) -> result_ok() | result_error().
Expand Down Expand Up @@ -281,12 +279,14 @@ stop() ->
%%% Internal functionality
%%%============================================================================
get_items(ConsumerKey, AccessToken, Filter) ->
{ok, _, {[{<<"status">>, 1},
{<<"complete">>, 1},
{<<"list">>, {Items}},
{<<"since">>, _Since}
]}} = retrieve(ConsumerKey, AccessToken, Filter),
Items.
case retrieve(ConsumerKey, AccessToken, Filter) of
{ok, _Headers, {Response}} ->
{Items} = proplists:get_value(<<"list">>, Response, {[]}),
Items;
Other ->
maybe_verbose("unable to retrieve items: ~p~n", [Other]),
[]
end.

get_stats(ConsumerKey, AccessToken, {Type, Params}) ->
Items = get_items(ConsumerKey, AccessToken,
Expand Down Expand Up @@ -373,12 +373,7 @@ parse_response(Response, json) ->
jiffy:decode(to_binary(Response)).

http_request(Url, Json) ->
case application:get_env(erlpocket, verbose, false) of
true ->
io:format("erlpocket: call url=~p,json=~p~n", [Url, Json]);
false ->
ignore
end,
maybe_verbose("call url=~p,json=~p~n", [Url, Json]),
{ok, {{_, Status, _}, Headers, Response}} =
httpc:request(post,
{Url, ["application/json"], "application/json", Json},
Expand All @@ -399,6 +394,14 @@ filter_headers(H) ->
"x-limit-user-reset", "x-source"],
[{K, proplists:get_value(K, H, "")} || K <- Keys].

maybe_verbose(Text, Args) ->
case application:get_env(erlpocket, verbose, false) of
true ->
io:format("erlpocket: " ++ Text, Args);
false ->
ignore
end.

get_url(request_token) ->
?BASE_URL ++ "v3/oauth/request";
get_url(authorize) ->
Expand Down
40 changes: 22 additions & 18 deletions test/erlpocket_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ test_add() ->

test_favorite() ->
Keys = read_api_keys(),
{ItemId, _} = search_item(Keys),
[{ItemId, _} | _] = search_item(Keys),
Result1 = erlpocket:favorite(get_val(consumer_key, Keys),
get_val(access_token, Keys), ItemId),
?assertMatch({ok, _, {[{<<"action_results">>,[true]},{<<"status">>,1}]}},
Expand All @@ -161,7 +161,7 @@ test_favorite() ->

test_archive() ->
Keys = read_api_keys(),
{ItemId, _} = search_item(Keys),
[{ItemId, _} | _] = search_item(Keys),
Result1 = erlpocket:archive(get_val(consumer_key, Keys),
get_val(access_token, Keys), ItemId),
?assertMatch({ok, _, {[{<<"action_results">>,[true]},{<<"status">>,1}]}},
Expand All @@ -173,7 +173,7 @@ test_archive() ->

test_tags_add() ->
Keys = read_api_keys(),
{ItemId, _} = search_item(Keys),
[{ItemId, _} | _] = search_item(Keys),
Result = erlpocket:tags_add(get_val(consumer_key, Keys),
get_val(access_token, Keys), ItemId,
[<<"test">>, <<"test2">>]),
Expand All @@ -182,7 +182,7 @@ test_tags_add() ->

test_tags_remove() ->
Keys = read_api_keys(),
{ItemId, _} = search_item(Keys),
[{ItemId, _} | _] = search_item(Keys),
Result = erlpocket:tags_remove(get_val(consumer_key, Keys),
get_val(access_token, Keys),
ItemId, [<<"test2">>]),
Expand All @@ -192,7 +192,7 @@ test_tags_remove() ->

test_tags_replace() ->
Keys = read_api_keys(),
{ItemId, _} = search_item(Keys),
[{ItemId, _} | _] = search_item(Keys),
Result = erlpocket:tags_replace(get_val(consumer_key, Keys),
get_val(access_token, Keys),
ItemId, [<<"test3">>]),
Expand All @@ -202,17 +202,16 @@ test_tags_replace() ->

test_tag_rename() ->
Keys = read_api_keys(),
{ItemId, _} = search_item(Keys),
[{ItemId, _} | _] = search_item(Keys),
Result = erlpocket:tag_rename(get_val(consumer_key, Keys),
get_val(access_token, Keys),
ItemId, <<"test3">>, <<"test4">>),
?assertMatch({ok, _, {[{<<"action_results">>,[true]},{<<"status">>,1}]}},
Result),
?assertNotEqual([], search_item(Keys, <<"test4">>)).
Result).

test_modify_tags_clear() ->
Keys = read_api_keys(),
{ItemId, _} = search_item(Keys),
[{ItemId, _} | _] = search_item(Keys),
Result = erlpocket:tags_clear(get_val(consumer_key, Keys),
get_val(access_token, Keys), ItemId),
?assertMatch({ok, _, {[{<<"action_results">>,[true]},{<<"status">>,1}]}},
Expand All @@ -221,7 +220,7 @@ test_modify_tags_clear() ->

test_modify_delete() ->
Keys = read_api_keys(),
{ItemId, _} = search_item(Keys),
[{ItemId, _} | _] = search_item(Keys),
Result = erlpocket:delete(get_val(consumer_key, Keys),
get_val(access_token, Keys), ItemId),
?assertMatch({ok, _, {[{<<"action_results">>,[true]},{<<"status">>,1}]}},
Expand All @@ -237,26 +236,31 @@ read_api_keys() ->
end.

search_item(Keys) ->
{ok, _, {PL}} = erlpocket:retrieve(
{ok, _, PL} = erlpocket:retrieve(
get_val(consumer_key, Keys),
get_val(access_token, Keys),
[{search, <<"Erlang Programming Language">>}]
),
case get_val(<<"list">>, PL) of
[] -> [];
{[{ItemId, {Item}}]} -> {ItemId, Item}
end.
parse_results(PL).

search_item(Keys, Tag) ->
{ok, _, {PL}} = erlpocket:retrieve(
{ok, _, PL} = erlpocket:retrieve(
get_val(consumer_key, Keys),
get_val(access_token, Keys),
[{search, <<"Erlang Programming Language">>},
{tag, Tag}]),
parse_results(PL).

parse_results({PL}) ->
case get_val(<<"list">>, PL) of
[] -> [];
{[{ItemId, {Item}}]} -> {ItemId, Item}
[] -> [];
{Items} -> lists:map(fun parse_result/1, Items)
end.

parse_result([]) ->
[];
parse_result({ItemId, {Item}}) ->
{ItemId, Item}.

get_val(Key, PL) ->
proplists:get_value(Key, PL).

0 comments on commit 309e3cf

Please sign in to comment.