Skip to content

Commit

Permalink
Create Resource class
Browse files Browse the repository at this point in the history
  • Loading branch information
martimpassos committed Sep 21, 2023
1 parent a2aea06 commit 92a828f
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/resource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const contexts = {
"@context": [{
"sc": "http://iiif.io/api/presentation/2#",
"iiif": "http://iiif.io/api/image/2#",
"exif": "http://www.w3.org/2003/12/exif/ns#",
"oa": "http://www.w3.org/ns/oa#",
"cnt": "http://www.w3.org/2011/content#",
"dc": "http://purl.org/dc/elements/1.1/",
"dcterms": "http://purl.org/dc/terms/",
"dctypes": "http://purl.org/dc/dcmitype/",
"doap": "http://usefulinc.com/ns/doap#",
"foaf": "http://xmlns.com/foaf/0.1/",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"svcs": "http://rdfs.org/sioc/services#",
"as": "http://www.w3.org/ns/activitystreams#",
}]
}
const ns = (prefix) => (name = '') => `${prefix}${name}`;
const alias = (ctx, name) => ns((ctx['@context'][0] || ctx['@context'])[name]);

// Create aliases for the namespaces
const dcterms = alias(contexts, 'dcterms');
const exif = alias(contexts, 'exif');

function extractValue(data, property) {
return data[property]?.[0]['@value'];
}

class Resource {
constructor(data = {}) {
this.data = data
this.identifier = extractValue(data, dcterms('identifier'));
this.title = extractValue(data, dcterms('title'));
this.description = extractValue(data, dcterms('description'));
this.source = extractValue(data, dcterms('source'));
this.rights = extractValue(data, dcterms('rights'));
this.latitude = extractValue(data, exif('gpsLatitude'));
this.longitude = extractValue(data, exif('gpsLongitude'));
}
}

module.exports = { Resource }

0 comments on commit 92a828f

Please sign in to comment.