Created
July 21, 2017 09:34
-
-
Save ZiiSolutions/8462c47a24d109be0157644abe6c8322 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const BodyHtmlConverter = require('../converters/body-html-converter'); | |
const decriptionConverter = require('../converters/description-html-converter'); | |
const objectArrayConverter = require('../converters/object-array-converter'); | |
const pubstatus = require('../constants/pub-statuses'); | |
const RenditionsConverter = require('../converters/renditions-converter'); | |
const subjectArrayConverter = require('../../lib/converters/subject-array-service-converter'); | |
const topicKeywordConverter = require('../../lib/converters/topic-keywords-converter'); | |
const types = require('../constants/types'); | |
const DEFAULT_BASE_URL = config.app.images.baseUrl; | |
const DEFAULT_PROFILE = 'asset'; | |
/** | |
* class for converting source json. | |
*/ | |
class NinjsConverter { | |
/** | |
* convert supplied json. | |
* | |
* @param json json to be converted | |
* @return object converted | |
*/ | |
convertToNinjs(json) { | |
return new Promise((resolve) => { | |
resolve(this._convertToNinjs(json, {})); | |
}); | |
} | |
_convertToNinjs(source, target) { | |
const ninjs = this._proxy(target); | |
let bodyHtmlConverter = new BodyHtmlConverter(DEFAULT_BASE_URL); | |
let renditionsConverter = new RenditionsConverter(DEFAULT_BASE_URL); | |
ninjs | |
.profile(DEFAULT_PROFILE) | |
.type(types.PICTURE) | |
.embargoed(source.embargoed) | |
.firstcreated(source.firstcreated) | |
.versioncreated(source.versioncreated) | |
.pubstatus(source.embargoed ? pubstatus.WITHELD : pubstatus.USABLE) | |
.byline(source.byline); | |
if (source.description_text) { | |
ninjs | |
.description_text(source.description_text) | |
.description_html(decriptionConverter.wrapHtml(source.description_text)); | |
} | |
ninjs | |
.body_html(bodyHtmlConverter.getBodyHtml(source)) | |
.subject(subjectArrayConverter.getSubjectArray(source) | |
.concat(topicKeywordConverter.getTopicsAndKeywords(source))) | |
.object(objectArrayConverter.getObjectArray(source)) | |
.renditions(renditionsConverter.getRenditions(source)); | |
return target; | |
} | |
_proxy(target) { | |
return new Proxy(target, { | |
get: function (_, property) { | |
if (typeof target[property] !== 'function') { | |
return function (value) { | |
if (value !== undefined) { | |
if (typeof value === 'function') { | |
target[property] = value(); | |
} else if (Array.isArray(value)) { | |
if (value.length !== 0) { | |
target[property] = value; | |
} | |
} else { | |
target[property] = value; | |
} | |
} | |
return this; | |
}; | |
} | |
return target[property]; | |
}, | |
set: function () { | |
return false; | |
} | |
}); | |
} | |
} | |
module.exports = new NinjsConverter(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment