Home Reference Source Test Repository

src/model/rating.js

  1. /**
  2. * Container class for all metadata pertaining to an action that a user has taken against a recommendation.
  3. */
  4. export default class Rating
  5. {
  6. /**
  7. * @param {Object} json The decoded JSON object that should be used as the basis for this model
  8. * @param {string} json.mediaId The media id as given by the media object
  9. * @param {string} json.origin How the recommendation was generated
  10. * @param {string} json.rating String representing the rating (action)
  11. * @param {number} json.elapsed Number of seconds since the start of playback for this media item, as an integer
  12. * @param {number} json.duration Number of seconds this audio piece is expected to last
  13. * @param {string} json.timestamp ISO-8601 formatted date/time; typically replaced by the client with the actual rating time
  14. * @param {string} json.channel The channel this media item was pulled from
  15. * @param {string} json.cohort The primary cohort of the current logged-in user
  16. * @param {Array} json.affiliations An array of IDs & other data about collections or podcasts the user has ratings for; produced by the server and should be sent back as received; used for tracking program and podcast suggestions
  17. */
  18. constructor(json) {
  19. Object.assign(this, json);
  20. this._hasSent = false;
  21. this._recommendationUrl = '';
  22. this._actionUrl = '';
  23. }
  24.  
  25. /**
  26. * Only send the fields back that NPR has sent us
  27. *
  28. * @param {string} key
  29. * @param {boolean|string|number|Array|Object|Function} value
  30. * @returns {undefined|boolean|string|number|Array|Object|Function}
  31. */
  32. static privateMemberReplacer(key, value) {
  33. if (['_hasSent', '_recommendationUrl', '_actionUrl'].indexOf(key) >= 0) {
  34. return undefined;
  35. }
  36. return value;
  37. }
  38.  
  39. /**
  40. * A convenience function to cast this object back to a string, generally only used by the {@link Logger} class.
  41. *
  42. * @returns {string}
  43. */
  44. toString() {
  45. return `[RID=${this.mediaId}, R=${this.rating}]`;
  46. }
  47. }