Lungo.Service.coffee 3.7 KB

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