Manual Reference Source Test

src/services/AdvertisementService.js

import { deprecate } from 'core-decorators';
import Client from '../Client';
import Advertisement from '../helpers/Advertisement';
import {
  hasToken,
  isA,
  isValid,
  validate,
} from '../utilities/ValidationUtility';

/**
 * Service handling Advertisements (images).
 *
 * Administrators can configure your app's carousel content on your Clinical6 dashboard under
 * **CMS** > **Image Carousel**. These advertisements consist of a title, an image, an action to be
 * taken when the image is tapped, and detailed action information.
 *
 * Your advertisements can also be tagged to help organize your content.
 *
 * @deprecated
 */
class AdvertisementService {
  /**
   * Retrieves advertisement settings.
   *
   * @return {Promise} - Promise object that returns success or failure
   *
   * @example
   * import { advertisementService } from 'clinical6';
   * advertisementService.getSettings().then(settings => console.log(settings));
   */
  @deprecate
  getSettings() {
    validate('AdvertisementService.getSettings',
      hasToken());

    return new Promise((resolve, reject) => {
      Client.instance.fetch(`/api/advertisements/settings`).then(
        response => resolve(response),
        err => reject(err)
      );
    });
  }

  /**
   * Retrieves the list of images in a advertisement with their corresponding attributes
   * and actions given a advertisement
   *
   * Once your carousel images are ready, you can access them through the `get` method. We highly
   * recommend including device-optimized images. You can filter your images by device-specific
   * versions and tags.
   *
   * @throws {Clinical6Error}             - If missing token
   * @param  {String}   [imageVersions=]  - Versions of images that will be returned with ads
   * @param  {String[]} [tagNames]        - Tag names that will be used for filtering
   * @return {Promise<Advertisement>}     - Promise object that returns success or failure
   *
   * @example
   * import { advertisementService } from 'clinical6';
   *
   * let imageVersion = 'galaxy_s4';
   * let tags = ['government','capital','DC'];
   *
   * advertisementService.get(imageVersion, tags).then((data) => {
   *   // display carousel
   * });
   * advertisementService.get();
   */
  @deprecate
  get(imageVersions = '', tagNames = []) {
    validate('AdvertisementService.get',
      hasToken(),
      isA({ imageVersions }, 'string'),
      isValid(Array.isArray(tagNames), 'tagNames is not an array'));

    let httpQuery = '';
    if (imageVersions || tagNames.length > 0) {
      httpQuery = `?`;
      if (imageVersions) {
        httpQuery += `image_versions=${imageVersions}&`;
      }
      tagNames.forEach(tagname => (httpQuery += `tag_names%5B%5D=${tagname}&`));
      // Remove tailing ampersand
      httpQuery = httpQuery.substr(0, httpQuery.length - 1);
    }

    return new Promise((resolve, reject) => {
      Client.instance.fetch(`/api/v2/advertisements${httpQuery}`).then(
        response => resolve(response.json.advertisements.map(obj => new Advertisement(obj))),
        err => reject(err)
      );
    });
  }
}

export default AdvertisementService;