PageRenderTime 886ms CodeModel.GetById 50ms RepoModel.GetById 10ms app.codeStats 0ms

/example/spice.js

https://github.com/mikeedwards83/zeroclickinfo-spice
JavaScript | 61 lines | 15 code | 10 blank | 36 comment | 3 complexity | 214fb9b0e3bcf146bd3364ba80e88b85 MD5 | raw file
  1. /*
  2. This is the function you define.
  3. To see a live example of what this function produces, check out:
  4. http://duckduckgo.com/?q=is+it+raining+in+philadelphia
  5. Here is the flow (as laid out in the ../example.html file
  6. 1) The external API is called through our servers, e.g.
  7. http://duckduckgo.com/ir/Phialadelphia
  8. 2) The API call returns JSON wrapped in a callback function, i.e. JSONP.
  9. 3) That callback function is defined below. It's name corresponds
  10. to the internal API endpoint, i.e. /ir/ becomes /nrir/.
  11. Your project will get its own unique endpoint, and thus
  12. function name.
  13. 4) The callback function receives the JSON object as its argument.
  14. There is no need to parse the string returned by the API; it is
  15. already a JavaScript object -- that's the beauty of JSONP.
  16. You can check out what it looks like by using Firebug or
  17. a browser extention like JSONView
  18. 5) The function should do some validity checks to make sure
  19. what it got back is appropriate for display.
  20. 6) If so, then you create the return object and send it to nra(),
  21. which displays it on the page.
  22. */
  23. function nrir(ir) {
  24. var snippet = '';
  25. // For debugging.
  26. // console.log(ir);
  27. // Validity check.
  28. if (ir['answer']) {
  29. // Snippet that gets shown in the 0-click box.
  30. snippet = '<b>' + ir['answer'] + '</b>';
  31. if (ir['forecast']) snippet += '; ' + ir['forecast'];
  32. if (ir['location']) snippet += ' (' + ir['location'] + ')';
  33. items = new Array();
  34. items[0] = new Array();
  35. items[0]['a'] = snippet;
  36. items[0]['h'] = '';
  37. // Source name and url for the More at X link.
  38. items[0]['s'] = 'GoingToRain';
  39. items[0]['u'] = 'http://goingtorain.com/';
  40. nra(items);
  41. }
  42. }