PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/share/spice/uv/uv.js

http://github.com/duckduckgo/zeroclickinfo-spice
JavaScript | 71 lines | 60 code | 6 blank | 5 comment | 11 complexity | 58a18840580d362ebc3d36f8dfa6d653 MD5 | raw file
Possible License(s): Apache-2.0
  1. (function (env) {
  2. "use strict";
  3. env.ddg_spice_uv = function(api_result){
  4. // Validate the response (customize for your Spice)
  5. if (!api_result || api_result.error || !api_result.length || api_result.length !== 1) {
  6. return Spice.failed('uv');
  7. }
  8. //extract city and date from first result
  9. var result = api_result[0];
  10. var city = DDG.capitalizeWords(result.CITY.toLowerCase()),
  11. state = result.STATE,
  12. uvIndex = parseInt(result.UV_INDEX,10),
  13. risk = "extreme",
  14. riskTitle = "Extreme Risk",
  15. protection = "Take all precautions: Wear sunglasses and use SPF 30+ sunscreen, "
  16. +"cover the body with a long-sleeve shirt and trousers, wear a very broad hat, "
  17. +"and avoid the sun from three hours before until three hours after solar noon.";
  18. //uv index risks and descriptions taken from http://en.wikipedia.org/wiki/Ultraviolet_index#How_to_use_the_index
  19. if(uvIndex < 3){
  20. risk = "low";
  21. riskTitle = "Low Danger";
  22. protection = "Wear sunglasses on bright days; use sunscreen if there is snow on the ground, "
  23. +"which reflects UV radiation, or if you have particularly fair skin.";
  24. } else if(uvIndex < 6){
  25. risk = "moderate";
  26. riskTitle = "Moderate Risk";
  27. protection = "Take precautions, such as covering up, if you will be outside. Stay in shade near midday when the sun is strongest.";
  28. } else if (uvIndex < 8){
  29. risk = "high";
  30. riskTitle = "High Risk";
  31. protection = "Cover the body with sun protective clothing, use SPF 30+ sunscreen, wear a wide-brim hat, "
  32. +"reduce time in the sun within three hours of solar noon, and wear sunglasses.";
  33. } else if (uvIndex < 11){
  34. risk = "veryhigh";
  35. riskTitle = "Very High Risk";
  36. protection = "Take all precautions: Wear sunglasses and use SPF 30+ sunscreen, cover the body with a long-sleeve shirt and trousers, "
  37. +"wear a very broad hat, and avoid the sun from three hours before until three hours after solar noon.";
  38. }
  39. // Render the response
  40. Spice.add({
  41. id: "uv",
  42. // Customize these properties
  43. name: "UV Index",
  44. data: {
  45. city: city,
  46. state: state,
  47. uvIndexName: result.UV_INDEX,
  48. risk: risk,
  49. riskTitle: riskTitle,
  50. protection: protection
  51. },
  52. signal: 'high',
  53. meta: {
  54. sourceName: "EPA",
  55. sourceUrl: 'http://www.epa.gov/enviro/facts/uv/uv_descriptions.html'
  56. },
  57. templates: {
  58. group: 'base',
  59. options: {
  60. content: Spice.uv.uv
  61. }
  62. }
  63. });
  64. };
  65. }(this));