Source: adapters/abstract-lookup-adapter.js

  1. 'use strict';
  2. /**
  3. * Adapter that, given a URL, looks up metadata that can identify a review
  4. * subject, such as a book's title or a restauraunt name.
  5. *
  6. * @abstract
  7. */
  8. class AbstractLookupAdapter {
  9. /**
  10. * Lookup adapters return a limited set of data that's displayed to the
  11. * user. Lookup results take the following form. Note that strings are stored
  12. * only in one language; the backend adapter performs the full lookup of all
  13. * available translations.
  14. *
  15. * @typedef {Object} LookupResult
  16. * @property {Object} data
  17. * data for this result
  18. * @property {String} data.label
  19. * name to be rendered for this result
  20. * @property {String} [data.subtitle]
  21. * subtitle to be shown below label
  22. * @property {String} [data.description]
  23. * short textual description
  24. * @property {Thing} [data.thing]
  25. * native object representing the review subject, used by native lookup adapter
  26. * @property {String} sourceID
  27. * canonical source that identifies this adapter
  28. */
  29. /**
  30. * @param {Function} updateCallback - `(optional)` callback to run after a
  31. * successful lookup
  32. */
  33. constructor(updateCallback) {
  34. // Replace w/ new.target after upgrading to Babel 7.0
  35. if (this.constructor.name === AbstractLookupAdapter.name)
  36. throw new TypeError('AbstractAdapter is an abstract class, please instantiate a derived class.');
  37. this.updateCallback = updateCallback || null;
  38. /**
  39. * Canonical identifier for this source. Lower-case string, no whitespace.
  40. *
  41. * @type {String}
  42. */
  43. this.sourceID = undefined;
  44. /**
  45. * RegExp for URLs this adapter can handle.
  46. *
  47. * @type {RegExp}
  48. */
  49. this.supportedPattern = undefined;
  50. }
  51. /**
  52. * Does this adapter support the given URL? By default, performs a simple
  53. * regex check.
  54. *
  55. * @param {String} url
  56. * the URL to test
  57. * @returns {Boolean}
  58. * true if supported
  59. */
  60. ask(url) {
  61. return this.supportedPattern.test(url);
  62. }
  63. /**
  64. * Perform a lookup for a given URL.
  65. *
  66. * @abstract
  67. * @param {String} _url
  68. * the URL to perform lookup for
  69. * @returns {Promise}
  70. * promise that resolves with a result object of the form
  71. * {@link LookupResult} on success, and rejects with an error on failure
  72. *
  73. */
  74. lookup(_url) {
  75. return Promise.reject(new Error('Not implemented.'));
  76. }
  77. /**
  78. * Return the canonical source identifier for this adapter
  79. *
  80. * @returns {String}
  81. */
  82. getSourceID() {
  83. return this.sourceID || 'no source ID defined';
  84. }
  85. }
  86. module.exports = AbstractLookupAdapter;