// Abstract ------------------------------------------------------------------------------------------------------------------------------

This sample fetch data from api.fixer.io (as JSON) and import it as knowledge.

// Examples ------------------------------------------------------------------------------------------------------------------------------

$ ./fizz.x64 ./etc/samples/fixer-pull.fizz -c api.key=\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\"
fizz 0.3.N-X (20180505.1956) [x64|8|w|l]

load : loading ./etc/samples/fixer-pull.fizz ...
load : loaded ./etc/samples/fixer-pull.fizz in 0.007s
load : loading completed in 0.007s
conversion(1525588507, EUR, AED, 4.394742)
conversion(1525588507, EUR, AFN, 84.252313)
...
conversion(1525588507, EUR, ZMW, 12.014256)
conversion(1525588507, EUR, ZWL, 385.728644)
 
// Code ----------------------------------------------------------------------------------------------------------------------------------

// an HTTP service puller elemental, fetching data every 60 seconds
web.conv.puller {

    class           = FZZCWebAPIPuller,
    tick            = 60.0,
    pull.on.attach  = true,
    url.host        = "http://data.fixer.io",
    url.path        = "/api/latest",
    url.query       = { access_key = $api.key }

} {

}

web.conv.input {

    // handle erroneous status coming back from the server
    () :-   @web.conv.puller(:t,:s?[neq(200)],_,:f),
            console.puts("pulling error: ",:s," ",:f);

    // handle success status
    () :-   @web.conv.puller(:t,200,_,:f),                        // trigger
            frm.fetch(:f,base,:base),                             // fetch the base
            frm.fetch(:f,rates,:r), #web.conv.proc(:t,:base,:r);  // fetch the rate and pass it to a secondary elemental for processing

}

web.conv.proc { // process each rate and assert a conversion statement for each

    (:t,:base,:f) :- frm.fetch(:f,:l?[is.symbol],:v?[is.number]), #assert(conversion(:t,:base,:l,:v));

}

// wrap the primitive 'assert' so that what is being asserted during execution can be more easily seen (in the console)
assert {

    (:f) :- console.puts(:f), assert(:f);

}

// ---------------------------------------------------------------------------------------------------------------------------------------

[Home] [Email] [Twitter] [LinkedIn]