Skip to content

Commit

Permalink
feat: add project lookup and project logos
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmacdonald committed Sep 28, 2018
1 parent 9bbcf90 commit fb7891a
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 2 deletions.
82 changes: 81 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,72 @@ function getSearchRoot() {
return searchRoot
}

let projectLogoRoot = ''
function setProjectLogoRoot(url) {
projectLogoRoot = url
}
function getProjectLogoRoot() {
return projectLogoRoot
}

let projectUrl = ''
async function setProjectLookupURI(url) {
projectUrl = url
return doProjectLookup()
}

let projects = {}
async function doProjectLookup() {
return fetch(projectUrl, {
method: 'get',
credentials: 'same-origin',
headers: {
'Accept': 'application/json'
}
}).then(async (response) => {
const data = await response.json()
for (let projectKey in data) {
const project = data[projectKey]

let logoFilename = undefined
const fieldLogo = project.field_logo
if (fieldLogo !== undefined) {
for (let key in fieldLogo) {
const entry = fieldLogo[key]
if (entry.length > 0) {
logoFilename = entry[0].filename
}
}
}

let projectId = undefined
const fieldTopLevel = project.field_top_level_collection
if (fieldTopLevel !== undefined && fieldTopLevel.und !== undefined) {
const und = fieldTopLevel.und
if (und.length > 0 && und[0].pid !== undefined) {
const pid = und[0].pid
const namespace = pid.substring(0, pid.indexOf(':'))
if (namespace !== '') {
projectId = namespace
}
}
}

if (logoFilename !== undefined && projectId !== undefined) {
if (projectId === 'islandora') {
projectId = 'cwrc'
}
if (projects[projectId] === undefined) {
projects[projectId] = logoFilename
}
}
}
return projects;
}, (reason) => {
console.warn('project lookup failed', reason)
})
}

/*
config is passed through to fetch, so could include things like:
{
Expand Down Expand Up @@ -81,7 +147,16 @@ function callCWRC(url, queryString, nameType) {
let id = record.PID
let name = record.object_label
let uri = entityRoot + '/'+ id
return {id, uri, uriForDisplay: uri, name, nameType, repository: 'CWRC', originalQueryString: queryString}

let data = {id, uri, uriForDisplay: uri, name, nameType, repository: 'CWRC', originalQueryString: queryString}

let namespace = id.substring(0, id.indexOf(':'))
let logo = projects[namespace]
if (logo !== undefined) {
data.logo = projectLogoRoot + '/' + logo
}

return data
}) : []
})

Expand All @@ -108,6 +183,11 @@ module.exports = {
getEntityRoot: getEntityRoot,
setSearchRoot: setSearchRoot,
getSearchRoot: getSearchRoot,

getProjectLogoRoot: getProjectLogoRoot,
setProjectLogoRoot: setProjectLogoRoot,
setProjectLookupURI: setProjectLookupURI,

findPerson: findPerson,
findPlace: findPlace,
findOrganization: findOrganization,
Expand Down
29 changes: 28 additions & 1 deletion test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const searchRoot = 'https://beta.cwrc.ca/';
const entityRoot = 'https://commons.cwrc.ca/';
const projectLogoRoot = 'https://beta.cwrc.ca/sites/default/files';

let cwrcLookup = require('../src/index.js');
cwrcLookup.setSearchRoot(searchRoot);
Expand All @@ -22,6 +23,9 @@ const expectedResultLength = 10;
const emptyResultFixture = JSON.stringify(require('./httpResponseMocks/noResults.json'));
const resultsFixture = JSON.stringify(require('./httpResponseMocks/results.json'));

const projectUrl = 'foobar';
const projectResultsFixture = JSON.stringify(require('./httpResponseMocks/projectResults.json'));

var clock;

// setup server mocks
Expand All @@ -42,6 +46,8 @@ fetchMock.get(uriBuilderFn(queryStringForTimeout), (url, opts)=> {
});
fetchMock.get(uriBuilderFn(queryStringForError), 500);

fetchMock.get(projectUrl, projectResultsFixture);


// babel-plugin-istanbul adds instrumentation to the browserified/babelified bundle, during babelification.
// When the tests are run on the browserified/babelified bundle, the instrumentation records test coverage and puts it in
Expand Down Expand Up @@ -73,6 +79,26 @@ test('get/set roots', (assert)=> {
assert.equal(cwrcLookup.getEntityRoot(), entityRoot, 'entityRoot should be the same');
});

test('project logo root', async (assert) => {
assert.plan(1);
cwrcLookup.setProjectLogoRoot(projectLogoRoot)
assert.equal(cwrcLookup.getProjectLogoRoot(), projectLogoRoot, 'project logo root should be the same')
});

test('project lookup', async (assert) => {
assert.plan(1);
let projects = await cwrcLookup.setProjectLookupURI(projectUrl, assert)
let projectKeys = []
for (let key in projects) {
projectKeys.push(key)
}
assert.ok(doObjectsHaveSameKeys(projects, {
cwrc: '',
reed: '',
orlando: ''
}), 'projects have been set')
});

test('findPerson', async function(assert){
let thisAssert = assert
// thisAssert.plan(21);
Expand All @@ -89,7 +115,8 @@ test('findPerson', async function(assert){
name: '',
nameType: '',
repository: '',
originalQueryString: ''
originalQueryString: '',
logo: ''
}), 'all results have correct keys')
thisAssert.equal(singleResult.originalQueryString, queryString, 'each result should return the original query string')
})
Expand Down
38 changes: 38 additions & 0 deletions test/httpResponseMocks/projectResults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"1": {
"field_logo": {
"en": [{
"filename": "cwrclogo-vert-fullcolour_cropped.png"
}]
},
"field_top_level_collection": {
"und": [{
"pid": "cwrc:cwrc"
}]
}
},
"2": {
"field_logo": {
"en": [{
"filename": "rl_kempe.png"
}]
},
"field_top_level_collection": {
"und": [{
"pid": "reed:reed"
}]
}
},
"3": {
"field_logo": {
"en": [{
"filename": "orlando_tree-_blue_transparent_0.gif"
}]
},
"field_top_level_collection": {
"und": [{
"pid": "orlando:orlando"
}]
}
}
}

0 comments on commit fb7891a

Please sign in to comment.