Because I can. Requires JSON for Erlang 

-module(htest).
-include("rfc4627.hrl").
-export([start/0]).

start() ->
application:start(inets),
application:start(ssl),
Apikey = "API TOKEN GOES HERE",
Name = "mattb"
Url = "https://www.dopplr.com/api/traveller_info/" ++ Name ++ "?format=js&token=" ++ Apikey,
handle(http:request(Url)).

handle({ok, {{_Version, 200, _ReasonPhrase}, _Headers, Body}}) ->
case rfc4627:decode(Body) of
{ok, Json, _S} ->
json(Json);
{error, _Reason} ->
io:format("JSON couldn't be parsed.~n")
end;
handle({ok, {{_Version, _Status, ReasonPhrase}, _Headers, _Body}}) ->
io:format("HTTP failed: ~s~n",[ReasonPhrase]).

json({obj, Json}) ->
traveller(get("traveller",Json)).

traveller(Traveller) ->
HomeCity = get("home_city",Traveller),
io:format("~s ~s~n", [get("name",Traveller), get("status",Traveller)]),
io:format("~s's home city is ~s~n", [get("name",Traveller), get("name",HomeCity)]).

get(Key,Alist) ->
case dict:fetch(Key,dict:from_list(Alist)) of
{obj, Value} -> Value;
Value -> Value
end. 


Page Information

  • 10 months ago [history]
  • View page source
  • You're not logged in
  • No tags yet learn more

Wiki Information

Recent PBwiki Blog Posts