Source: src/models/ItemListResponse.js

  1. 'use strict';
  2. /**
  3. * Represents a list of items returned from GW2 Spidy's full item list.
  4. *
  5. * @param {string} json A string of JSON that represents a Spidy result list
  6. * @returns {ItemListResponse}
  7. * @constructor
  8. */
  9. function ItemListResponse(json) {
  10. if (! (this instanceof ItemListResponse)) {
  11. return new ItemListResponse();
  12. }
  13. var self = Object.getPrototypeOf(this);
  14. if (json) {
  15. var parsedJson = JSON.parse(json);
  16. Object.keys(parsedJson).forEach(function mapItemJson(val) {
  17. var prop = (val === 'last_page') ? 'lastPage' : val;
  18. this[prop] = parsedJson[val];
  19. }, self);
  20. }
  21. }
  22. ItemListResponse.prototype = {
  23. /**
  24. * The number of results in the current page of results if the API
  25. * returned such information. Otherwise, it is -1.
  26. *
  27. * @returns {number}
  28. */
  29. get count() {
  30. return (this._count) ? this._count : -1;
  31. },
  32. set count(count) {
  33. this._count = (count) ? count : -1;
  34. },
  35. /**
  36. * The current page number of the results if the API returned such
  37. * information. Otherwise it is -1.
  38. *
  39. * @returns {number}
  40. */
  41. get page() {
  42. return (this._page) ? this._page : -1;
  43. },
  44. set page(page) {
  45. this._page = (page) ? page : -1;
  46. },
  47. /**
  48. * The number of the last available page of results if the API returned such
  49. * information. Otherwise, it is -1.
  50. *
  51. * @returns {number}
  52. */
  53. get lastPage() {
  54. return (this._lastPage) ? this._lastPage : -1;
  55. },
  56. set lastPage(lastPage) {
  57. this._lastPage = (lastPage) ? lastPage : -1;
  58. },
  59. /**
  60. * The total number of results if the API returned such information.
  61. * Otherwise, it is -1.
  62. *
  63. * @returns {number}
  64. */
  65. get total() {
  66. return (this._total) ? this._total : -1;
  67. },
  68. set total(total) {
  69. this._total = (total) ? total : -1;
  70. },
  71. /**
  72. * Retrieve the list of {@link Item} objects as returned by the REST API.
  73. *
  74. * @returns {array}
  75. */
  76. get results() {
  77. return (this._results) ? this._results : [];
  78. },
  79. set results(results) {
  80. this._results = (results) ? results : [];
  81. }
  82. };
  83. exports = module.exports = function() {
  84. return ItemListResponse;
  85. };
  86. exports['@singleton'] = false;