PageRenderTime 77ms CodeModel.GetById 44ms RepoModel.GetById 1ms app.codeStats 0ms

/share/spice/expatistan/expatistan.js

http://github.com/duckduckgo/zeroclickinfo-spice
JavaScript | 50 lines | 40 code | 6 blank | 4 comment | 6 complexity | 494966f6927910073b19b2a7bfc82074 MD5 | raw file
Possible License(s): Apache-2.0
  1. (function(env) {
  2. env.ddg_spice_expatistan = function(api_result) {
  3. "use strict";
  4. if(!api_result || api_result.status !== 'OK') {
  5. return Spice.failed('expatistan');
  6. }
  7. Spice.add({
  8. id: "expatistan",
  9. name: "Answer",
  10. data: api_result,
  11. meta: {
  12. sourceUrl: api_result.source_url,
  13. sourceName: 'Expatistan'
  14. },
  15. normalize: function(item) {
  16. // Vars
  17. var city, cost, subtitle, title;
  18. // Guard if abstract or source_url don't exist
  19. if (!api_result.abstract || !api_result.source_url) {
  20. return Spice.failed('expatistan');
  21. }
  22. // We have a comparison result return the full abstract
  23. if(api_result.source_url.match(/comparison/)) {
  24. title = api_result.abstract;
  25. } else {
  26. // We have a single result extract the index and create a subtitle
  27. city = api_result.abstract.match(/<b>(.*?)<\/b>/g)[0];
  28. title = api_result.abstract.match(/\s[0-9]+\s/)[0];
  29. subtitle = city + " - Cost of living index";
  30. }
  31. return {
  32. title: DDG.strip_html(title),
  33. subtitle: DDG.strip_html(subtitle)
  34. };
  35. },
  36. templates: {
  37. group: 'text',
  38. options: {
  39. moreAt: true
  40. }
  41. }
  42. });
  43. }
  44. }(this));