/share/spice/rx_info/rx_info.js

https://github.com/pardocz/zeroclickinfo-spice · JavaScript · 73 lines · 58 code · 10 blank · 5 comment · 10 complexity · b69f665d35d588e294bd874183429d14 MD5 · raw file

  1. (function (env) {
  2. "use strict";
  3. // Concatenate Ingredients
  4. function concatIngredients(ingredients) {
  5. var concatted = '';
  6. // Protect against the chance ingredients aren't part of the API result
  7. if (ingredients !== null && (typeof ingredients !== 'undefined')) {
  8. for (var i = 0; i < ingredients.length; i++) {
  9. concatted += ingredients[i] + ", ";
  10. }
  11. if (concatted.length > 0) {
  12. concatted = concatted.substring(0, concatted.length - 2);
  13. }
  14. }
  15. return concatted;
  16. }
  17. env.ddg_spice_rx_info = function(api_result){
  18. if (!api_result || api_result.error || !api_result.replyStatus || !api_result.replyStatus.totalImageCount || api_result.replyStatus.totalImageCount < 1) {
  19. return Spice.failed('rx_info');
  20. }
  21. var sourceName = "More at DailyMed",
  22. sourceUrl = "http://dailymed.nlm.nih.gov/";
  23. Spice.add({
  24. id: "rx_info",
  25. name: "RxInfo",
  26. data: api_result.nlmRxImages,
  27. meta: {
  28. sourceName: sourceName,
  29. sourceUrl: sourceUrl
  30. },
  31. templates: {
  32. group: 'products_simple',
  33. detail: Spice.rx_info.rx_info,
  34. item_detail: Spice.rx_info.rx_info,
  35. options: {
  36. brand: false,
  37. rating: false
  38. }
  39. },
  40. normalize: function(item) {
  41. var heading = item.ndc11,
  42. active = concatIngredients(item.ingredients.active),
  43. inactive = concatIngredients(item.ingredients.inactive);
  44. // Not all items will have a name (drug name),
  45. // so when it's available use it, otherwise the
  46. // ndc-11 will suffice.
  47. if (item.name) {
  48. heading = item.name;
  49. }
  50. return {
  51. image: item.imageUrl,
  52. abstract: item.ndc11,
  53. heading: heading,
  54. title: heading,
  55. ratingText: item.ndc11,
  56. active: active,
  57. inactive: inactive,
  58. proxyImageUrl: "https://images.duckduckgo.com/iu/?u=" + encodeURIComponent(item.imageUrl) + "&f=1"
  59. }
  60. }
  61. });
  62. };
  63. }(this));