Pages

Thursday, November 5, 2009

Testing Syntax Highlighting with a Code Snippet

As a way of testing whether the syntax highlighting works, an Erlang example of an HTTP client for a RESTful service, adapted from the awesome RESTful Web Services.
-module(wr).
-include_lib("xmerl/include/xmerl.hrl").

-define(URI,
    "http://api.search.yahoo.com/"
    "WebSearchService/V1/webSearch"
    "?appid=restbook&query=").
-define(TERM, "jellyfish").
-export([r/0,r/1]).

r() ->
    r(?TERM).

r(Term) ->
    {ok, {_Response, _Headers, Body}} =
    http:request(get, {?URI ++ edoc_lib:escape_uri(Term), []}, [], []),
    parse(Body).

parse(Body) ->
    {Xml, _Rest} = xmerl_scan:string(Body),
    Titles = xmerl_xpath:string("/ResultSet/Result/Title/text()", Xml),
    Urls  = xmerl_xpath:string("/ResultSet/Result/ClickUrl/text()", Xml),
    [ [{title, M#xmlText.value}, {url, N#xmlText.value}] || {M,N} <- lists:zip(Titles, Urls) ].
inets needs to be started first.
Erlang R13B02 (erts-5.7.3) [source] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]

Eshell V5.7.3  (abort with ^G)
1> inets:start().
ok
2> wr:r("erlang").

No comments:

Post a Comment