all files / api/toJSON/ embed.js

100% Statements 18/18
50% Branches 1/2
100% Functions 0/0
100% Lines 14/14
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                                                                                       
'use strict';
const R = require('ramda');
 
/*
 
    Embedded content type
 
    {
        "data": {
          "type": "embed",
          "id":12345,
          "attributes": {
            "subType": "poll",
            "label": "Poll"
          },
          "links":{
            "url":"https://www.mirror.co.uk/.../embedded-view/mirror-12345"
          }
        }
      }
*/
 
const capitalize = str => str.replace(/^./, R.toUpper);
const dehyphenize = R.compose(R.join(' '), R.split('-'));
const censored = R.flip(R.reduce((str, [match,rep]) => str.replace(new RegExp(match,'gi'), rep)));
const objToMap = obj => new Map(R.toPairs(obj));
const createEmbedUrl = (url, pubName, filename, id) => `${url}${filename}/embedded-webview/${pubName}-${id}`;
 
const wordObj = {
    'interactive HTML':'Interactive Content',
    'html':'Interactive Content',
    'jotform': 'Form',
    'facebook':'Facebook'
};
const words = objToMap(wordObj);
const humanReadableLabel = R.compose(dehyphenize, capitalize, censored(words));
 
module.exports = (homeSection, filename, item) => {
    const attributes = {
        "subType": item.sub_type,
        "label": humanReadableLabel(item.sub_type)
    };
    const links = {
        "url": createEmbedUrl(
                homeSection.url,
                R.path(['publication', 'name'], homeSection) ? homeSection.publication.name : '',
                filename,
                item.escenic_id
            )
    };
    const data = {
        "type":"embed",
        "id": item.escenic_id,
        attributes,
        links
    };
    return data;
};