Skip to content

Commit

Permalink
Merge pull request #8 from Financial-Times/matth/tag
Browse files Browse the repository at this point in the history
Add tag method to fetch a single tag
  • Loading branch information
i-like-robots authored Apr 5, 2017
2 parents cc5af37 + a32d032 commit d0bf076
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ es.search({
});
```

### `.tag(uuid[, timeout])`

Get a single TME tag by UUID. Returns an object or `undefined` if no matches were found.

[1]: https://github.com/matthew-andrews/signed-aws-es-fetch
[2]: https://www.npmjs.com/package/http-errors
[3]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
search: require('./lib/search'),
mget: require('./lib/mget'),
get: require('./lib/get')
get: require('./lib/get'),
tag: require('./lib/tag')
};
39 changes: 39 additions & 0 deletions lib/tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const search = require('./search');

const KEYS = [ 'idV1', 'prefLabel', 'taxonomy', 'attributes', 'url' ];

function pluck (rawTag) {
return KEYS.reduce((newTag, key) => {
newTag[key] = rawTag[key];
return newTag;
}, {});
}

function handleData (data, uuid) {
if (data.length) {
const tag = data[0].metadata.find((tag) => tag.idV1 === uuid);
return pluck(tag);
}
}

function tag (uuid, timeout = 3000) {
const params = {
query: {
// avoid scoring to increase caching
constant_score: {
filter: {
term: {
'metadata.idV1': uuid
}
}
}
},
size: 1,
_source: [ 'metadata' ]
};

return search(params, timeout)
.then((data) => handleData(data, uuid));
}

module.exports = tag;
77 changes: 77 additions & 0 deletions test/fixtures/tag-found.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 26082,
"max_score": 1,
"hits": [
{
"_index": "v3_api_v2-2016-12-20",
"_type": "item",
"_id": "07004678-4661-3417-bd5f-3b5f1e673773",
"_score": 1,
"_source": {
"metadata": [
{
"idV1": "NTM=-U2VjdGlvbnM=",
"prefLabel": "Technology",
"attributes": [],
"primaryTag": true,
"taxonomy": "sections",
"teaserTag": true,
"url": "https://www.ft.com/companies/technology",
"primary": "section"
},
{
"idV1": "OA==-R2VucmVz",
"prefLabel": "Comment",
"attributes": [],
"taxonomy": "genre",
"url": "https://www.ft.com/stream/genreId/OA==-R2VucmVz"
},
{
"idV1": "MDEwMzFmMmUtYThkNy00ZWJmLWEzOTYtOWM2OWQ3YzA0ZGE1-T04=",
"prefLabel": "Financial Times",
"attributes": [
{
"value": "true",
"key": "is_company"
}
],
"taxonomy": "organisations",
"url": "https://www.ft.com/stream/organisationsId/MDEwMzFmMmUtYThkNy00ZWJmLWEzOTYtOWM2OWQ3YzA0ZGE1-T04="
},
{
"idV1": "TnN0ZWluX09OX0FGVE1fT05fMzI5M19NU0w=-T04=",
"prefLabel": "Lehman Brothers Holdings",
"attributes": [
{
"value": "us:LEHMQ",
"key": "wsod_key"
},
{
"value": "true",
"key": "is_company"
}
],
"taxonomy": "organisations",
"url": "https://www.ft.com/topics/organisations/Lehman_Brothers_Holdings_Inc"
},
{
"idV1": "Mjk=-U2VjdGlvbnM=",
"prefLabel": "Companies",
"attributes": [],
"taxonomy": "sections",
"url": "https://www.ft.com/companies"
}
]
}
}
]
}
}
14 changes: 14 additions & 0 deletions test/fixtures/tag-not-found.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
16 changes: 8 additions & 8 deletions test/spec/search-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ describe('Search', () => {
expect(result.total).to.equal(fixtureWithResults.hits.total);
})
));

it('returns the document source', () => (
subject().then((result) => {
result.forEach((doc) => {
expect(doc).to.include.keys('id', 'title');
});
})
));
});

context('Response - no results', () => {
Expand All @@ -79,14 +87,6 @@ describe('Search', () => {
expect(result.total).to.equal(0);
})
));

it('returns the document source', () => (
subject().then((result) => {
result.forEach((doc) => {
expect(doc).to.include.keys('id', 'title');
});
})
));
});

context('Response - error', () => {
Expand Down
78 changes: 78 additions & 0 deletions test/spec/tag-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const { expect } = require('chai');
const nock = require('nock');

const subject = require('../../lib/tag');

const fixtureFound = require('../fixtures/tag-found.json');
const fixtureNotFound = require('../fixtures/tag-not-found.json');

describe('Tag', () => {
afterEach(() => {
nock.isDone();
nock.cleanAll();
});

context('Response - found', () => {
const id = 'NTM=-U2VjdGlvbnM=';

beforeEach(() => {
nock('https://next-elastic.ft.com')
.post('/v3_api_v2/item/_search')
.reply(200, fixtureFound);
});

it('returns an object', () => (
subject(id).then((result) => {
expect(result).to.be.an('object');
})
));

it('finds the requested tag', () => {
subject(id).then((result) => {
expect(result.idV1).to.equal(id);
})
});

it('plucks out the necessary keys', () => (
subject(id).then((result) => {
expect(result).to.include.keys('idV1', 'prefLabel', 'taxonomy', 'attributes', 'url');
expect(result).to.not.include.keys('primaryTag', 'teaserTag', 'primary');
})
));
});

context('Response - not found', () => {
const id = 'No=-Exist=';

beforeEach(() => {
nock('https://next-elastic.ft.com')
.post('/v3_api_v2/item/_search')
.reply(200, fixtureNotFound);
});

it('returns nothing', () => (
subject(id).then((result) => {
expect(result).to.be.undefined;
})
));
});

context('Response - error', () => {
beforeEach(() => {
nock('https://next-elastic.ft.com')
.post('/v3_api_v2/item/_search')
.reply(500);
});

it('throws an HTTP error', () => (
subject()
.then((result) => {
expect(result).to.equal('This should never run');
})
.catch((error) => {
expect(error).to.be.an('error');
expect(error.name).to.equal('InternalServerError');
})
));
});
});

0 comments on commit d0bf076

Please sign in to comment.