Lungo.Service.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * External Data & Services Manager
  3. *
  4. * @namespace Lungo
  5. * @class Service
  6. * @requires QuoJS
  7. *
  8. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  9. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  10. */
  11. Lungo.Service = (function(lng, $$, undefined) {
  12. var URL_CACHE_INDEX_KEY = 'lungojs_service_cache';
  13. var DATE_PATTERN = {
  14. MINUTE: 'minute',
  15. HOUR: 'hour',
  16. DAY: 'day'
  17. };
  18. /**
  19. * Load data from the server using a HTTP GET request.
  20. *
  21. * @method get
  22. *
  23. * @param {string} Containing the URL to which the request is sent
  24. * @param {object} A map or string that is sent to the server with the request
  25. * @param {Function} Callback function after the request [OPTIONAL]
  26. * @param {string} Mime-Type: json, xml, html, or text [OPTIONAL]
  27. */
  28. var get = function(url, data, success, dataType) {
  29. return $$.get(url, data, success, dataType);
  30. };
  31. /**
  32. * Load data from the server using a HTTP POST request.
  33. *
  34. * @method post
  35. *
  36. * @param {string} Containing the URL to which the request is sent
  37. * @param {object} A map or string that is sent to the server with the request
  38. * @param {Function} Callback function after the request [OPTIONAL]
  39. * @param {string} Mime-Type: json, xml, html, or text [OPTIONAL]
  40. */
  41. var post = function(url, data, success, dataType) {
  42. return $$.post(url, data, success, dataType);
  43. };
  44. /**
  45. * Load data from the server using a HTTP GET request.
  46. *
  47. * @method json
  48. *
  49. * @param {string} Containing the URL to which the request is sent
  50. * @param {object} A map or string that is sent to the server with the request
  51. * @param {Function} [OPTIONAL] Callback function after the request
  52. */
  53. var json = function(url, data, success) {
  54. return $$.json(url, data, success);
  55. };
  56. /**
  57. * Auto-caching system with date pattern.
  58. *
  59. * @method cache
  60. *
  61. * @param {string} Containing the URL to which the request is sent
  62. * @param {object} A map or string that is sent to the server with the request
  63. * @param {string} Date pattern (example: 15 minutes, 1 hour, 3 days)
  64. * @param {Function} [OPTIONAL] Callback function after the request
  65. * @param {string} Mime-Type: json, xml, html, or text [OPTIONAL]
  66. */
  67. var cache = function(url, data, date_pattern, callback, dataType) {
  68. var url_key = url + $$.serializeParameters(data);
  69. if (_urlCached(url_key, date_pattern)) {
  70. var value = lng.Data.Storage.persistent(url_key);
  71. if (value) {
  72. return callback.call(callback, value);
  73. }
  74. } else {
  75. return $$.get(url, data, function(result) {
  76. _saveServiceInCache(url_key, result);
  77. callback.call(callback, result);
  78. }, dataType);
  79. }
  80. };
  81. var _urlCached = function(url, date_pattern) {
  82. var in_cache = false;
  83. var url_cache_index = lng.Data.Storage.persistent(URL_CACHE_INDEX_KEY);
  84. if (url_cache_index) {
  85. var time_between = _calculateTimeSpent(url_cache_index[url]);
  86. in_cache = _checkIsValidPattern(time_between, date_pattern);
  87. }
  88. return in_cache;
  89. };
  90. var _calculateTimeSpent = function(url_last_access) {
  91. var now = new Date().getTime();
  92. var service_last_access = new Date(url_last_access).getTime();
  93. return now - service_last_access;
  94. };
  95. var _checkIsValidPattern = function(time_between, date_pattern) {
  96. var pattern = date_pattern.split(' ');
  97. var diference_time = _calculateDiferenceTime(pattern[1], time_between);
  98. return (diference_time < pattern[0]) ? true : false;
  99. };
  100. var _calculateDiferenceTime = function(pattern_name, time_between) {
  101. var diference = (time_between / 1000) / 60;
  102. if (pattern_name.indexOf(DATE_PATTERN.HOUR) >= 0) {
  103. diference = diference / 60;
  104. } else if (pattern_name.indexOf(DATE_PATTERN.DAY) >= 0) {
  105. diference = (diference / 60) / 24;
  106. }
  107. return diference;
  108. };
  109. var _saveServiceInCache = function(url, result) {
  110. var service_cache_index = lng.Data.Storage.persistent(URL_CACHE_INDEX_KEY) || {};
  111. service_cache_index[url] = new Date();
  112. lng.Data.Storage.persistent(URL_CACHE_INDEX_KEY, service_cache_index);
  113. lng.Data.Storage.persistent(url, result);
  114. };
  115. return {
  116. get: get,
  117. post: post,
  118. json: json,
  119. cache: cache,
  120. Settings: $$.ajaxSettings
  121. };
  122. })(Lungo, Quo);