-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Financial-Times/matth/tag
Add tag method to fetch a single tag
- Loading branch information
Showing
7 changed files
with
222 additions
and
9 deletions.
There are no files selected for viewing
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
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
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') | ||
}; |
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
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; |
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
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" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} |
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
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": [] | ||
} | ||
} |
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
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
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'); | ||
}) | ||
)); | ||
}); | ||
}); |