All files PublicationInformation.js

98.5% Statements 66/67
100% Branches 24/24
100% Functions 8/8
98.5% Lines 66/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 671x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x   5x 5x 1x 37x 37x 1x 6x 6x 6x 6x 6x 1x 1x 1x 4x 4x 6x 1x 1x 1x 6x 1x 10x 373x 373x 10x 10x 1x 21x 21x 21x 21x 21x 1043x 21x 21x 1x 5x 5x 5x 370x 5x 5x 1x 5x 5x 370x 5x 5x 1x 6x 6x 1x 1x 1x
const {readFileSync} = require('fs');
const path = require('path');
const R = require('ramda');
const fetch = require('node-fetch');
 
class PublicationInformation {
  constructor(fetchFn = fetch) {
    this._fetchFn = fetchFn;
    this._updatePublicationInformation(JSON.parse(readFileSync(path.resolve(__dirname, 'pubs.json'))));
    this._triggerPublicationMappingRedownload();
    setInterval(() => {
      this._triggerPublicationMappingRedownload();
    }, 1000 * 60 * 10);
  }
  get publicationInformation() {
    return this._publicationInformation;
  }
  async _triggerPublicationMappingRedownload() {
    let result;
    try {
      result = await this._fetchFn('https://reach-dataen-publication-mapping.s3.eu-west-1.amazonaws.com/pubs.json');
      if(!result.ok) throw JSON.stringify(result);
    } catch(err) {
      console.error(`Could not re-download publication information, ${err}`);
      return;
    }
    try {
      this._updatePublicationInformation(JSON.parse(await result.text()));
    } catch(err) {
      console.error(`Could not parse publication information redownload, ${err}`);
      return;
    }
  }
  _updatePublicationInformation(newPublicationInformation) {
    this._publicationInformation = newPublicationInformation.map(pubInfo => {
      pubInfo.url = pubInfo.urls[0];
      return pubInfo;
    });
  }
  findHostname(hostname) {
    if(!hostname) hostname = '';
    if(hostname.endsWith('.trem.media')) hostname = 'trem.media.' + hostname.replace('.trem.media', '');
    if(hostname.endsWith('.translate.goog')) hostname = 'translate.goog.' + hostname.replace('.translate.goog', '').replace(/-/g,'.');
    return R.clone(this.publicationInformation
      .filter(pubInfo => {
        return pubInfo.urls.some(url => hostname.endsWith(url));
      })[0]);
  }
  findName(name) {
    const normalizedName = name.toString().toLowerCase();
    return R.clone(this.publicationInformation
      .filter(pubInfo => {
        return pubInfo.names.some(pubName => pubName.toLocaleLowerCase() === normalizedName);
      })[0]);
  }
  findId(platform, publicationId) {
    return R.clone(this.publicationInformation
      .filter(pubInfo => {
        return pubInfo.platform === platform && pubInfo.publicationId === publicationId;
      })[0]);
  }
  clone() {
    return R.clone(this.publicationInformation);
  }
}
 
module.exports = PublicationInformation;