Skip to content

Commit

Permalink
support curies
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed Jun 27, 2014
1 parent db5bdf2 commit 3477b61
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 19 deletions.
63 changes: 54 additions & 9 deletions lib/resource.js
Expand Up @@ -3,23 +3,60 @@
function Resource(links, curies, embedded, validationIssues) {
var self = this;
this._links = links || {};
initCuries(this, curies);
this._initCuries(curies);
this._embedded = embedded || {};
this._validation = validationIssues || [];
}

function initCuries(self, curies) {
self._curiesMap = {};
Resource.prototype._initCuries = function(curies) {
this._curiesMap = {};
if (!curies) {
self._curies = [];
this._curies = [];
} else {
self._curies = curies;
for (var i = 0; i < self._curies.length; i++) {
var curie = self._curies[i];
self._curiesMap[curie.name] = curie;
this._curies = curies;
for (var i = 0; i < this._curies.length; i++) {
var curie = this._curies[i];
this._curiesMap[curie.name] = curie;
}
}
}
this._preResolveCuries();
};

// TODO resolving of CURIEs should be a package of its own
// TODO resolving templated CURIES should use a small uri template lib, not
// coded here ad hoc

Resource.prototype._preResolveCuries = function() {
this._resolvedCuriesMap = {};
for (var i = 0; i < this._curies.length; i++) {
var curie = this._curies[i];
if (!curie.name) {
continue;
}
for (var rel in this._links) {
if (rel !== 'curies') {
this._preResolveCurie(curie, rel);
}
}
}
};

Resource.prototype._preResolveCurie = function(curie, rel) {
var link = this._links[rel];
var prefixAndReference = rel.split(/:(.+)/);
var candidate = prefixAndReference[0];
if (curie.name === candidate) {
if (curie.templated && prefixAndReference.length >= 1) {
// TODO resolving templated CURIES should use a small uri template
// lib, not coded here ad hoc
var href = curie.href.replace(/(.*){(.*)}(.*)/, '$1' +
prefixAndReference[1] + '$3');
this._resolvedCuriesMap[href] = rel;
} else {
this._resolvedCuriesMap[curie.href] = rel;
}
}
};

Resource.prototype.allLinkArrays = function() {
return this._links;
Expand All @@ -33,6 +70,10 @@ Resource.prototype.link = function(key, index) {
return elementOfPropertyArray(this._links, key, index);
};

Resource.prototype.hasCuries = function(key) {
return this._curies.length > 0;
};

Resource.prototype.curieArray = function(key) {
return this._curies;
};
Expand All @@ -41,6 +82,10 @@ Resource.prototype.curie = function(name) {
return this._curiesMap[name];
};

Resource.prototype.reverseResolveCurie = function(fullUrl) {
return this._resolvedCuriesMap[fullUrl];
};

Resource.prototype.allEmbeddedResourceArrays = function () {
return this._embedded;
};
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/curies.js
Expand Up @@ -12,8 +12,9 @@ exports.get = function() {
name: 'curie2',
href: 'http://docs.example.com/relations/curie2',
}],
'curie1:value': { href: '/link' },
'curie2:value': { href: '/link' },
'curie1:value': { href: '/curie/1' },
'curie1:value:dangling': { href: '/curie/1/1' },
'curie2': { href: '/curie/2' },
}
};
};
62 changes: 54 additions & 8 deletions test/parser.js
Expand Up @@ -189,15 +189,61 @@ describe('Parsing HAL', function () {
it('should parse curies', function () {
var unparsed = fixtures.curies.get();
var resource = halfred.parse(unparsed);
expect(resource.hasCuries()).to.be.true;
expect(resource.curieArray()).to.exist;
var curie1 = resource.curie('curie1');
expect(curie1.name).to.equal('curie1');
expect(curie1.templated).to.equal.true;
expect(curie1.href).to.equal('http://docs.example.com/relations/{rel}');
var curie2 = resource.curie('curie2');
expect(curie2.name).to.equal('curie2');
expect(curie2.templated).to.be.false;
expect(curie2.href).to.equal('http://docs.example.com/relations/curie2');
expect(resource.curieArray()).to.be.an('array');
expect(resource.curieArray().length).to.equal(2);
var fullUrl1 = resource.curie('curie1');
expect(fullUrl1.name).to.equal('curie1');
expect(fullUrl1.templated).to.equal.true;
expect(fullUrl1.href).to.equal('http://docs.example.com/relations/{rel}');
var fullUrl2 = resource.curie('curie2');
expect(fullUrl2.name).to.equal('curie2');
expect(fullUrl2.templated).to.be.false;
expect(fullUrl2.href).to.equal('http://docs.example.com/relations/curie2');
});

it('should recognize that there are no curies', function () {
var unparsed = fixtures.shop.get();
var resource = halfred.parse(unparsed);
expect(resource.hasCuries()).to.be.false;
expect(resource.curieArray()).to.be.an('array');
expect(resource.curieArray().length).to.equal(0);
expect(resource.curie('whatever')).to.not.exist;
});

it('should reverse resolve non-templated curies', function () {
var unparsed = fixtures.curies.get();
var resource = halfred.parse(unparsed);
var curie = resource.reverseResolveCurie(
'http://docs.example.com/relations/curie2');
expect(curie).to.equal('curie2');
});

it('should reverse resolve templated curies', function () {
var unparsed = fixtures.curies.get();
var resource = halfred.parse(unparsed);
var curie = resource.reverseResolveCurie(
'http://docs.example.com/relations/value');
expect(curie).to.equal('curie1:value');
});

it('should reverse resolve to null/undefined if curie url does not ' +
'exist', function () {
var unparsed = fixtures.curies.get();
var resource = halfred.parse(unparsed);
var curie = resource.reverseResolveCurie(
'http://docs.example.com/does/not/exist');
expect(curie).to.not.exist;
});

it('should reverse resolve to null/undefined if there is no templated ' +
' match', function () {
var unparsed = fixtures.curies.get();
var resource = halfred.parse(unparsed);
var curie = resource.reverseResolveCurie(
'http://docs.example.com/relations/doesnotexist');
expect(curie).to.not.exist;
});

it('should parse a resource without links', function () {
Expand Down

0 comments on commit 3477b61

Please sign in to comment.