-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
250 additions
and
3 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
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,52 @@ | ||
'use strict' | ||
const fp = require('fastify-plugin') | ||
|
||
async function apiLayer (fastify, options) { | ||
const apiKey = options.apilayer.key | ||
|
||
fastify.decorate('apiLayer', { | ||
async post22 (content, url) { | ||
/* const myHeaders = new Headers() | ||
myHeaders.append('apikey', '6EwQsWNDR4AoSvFCHAsFUhmxkuALN13F') | ||
*/ | ||
const raw = 'You have done excellent work, and well done.' | ||
|
||
const requestOptions = { | ||
method: 'POST', | ||
redirect: 'follow', | ||
headers: { | ||
'content-type': 'text/plain', | ||
apikey: '6EwQsWNDR4AoSvFCHAsFUhmxkuALN13F' | ||
}, | ||
body: raw | ||
} | ||
const response = await fetch(url, requestOptions) | ||
if (!response.ok) { | ||
const message = `An error has occured: ${response.status}` | ||
throw new Error(message) | ||
} | ||
return await response.json() | ||
}, | ||
async post (content, url) { | ||
const options = { | ||
method: 'POST', | ||
redirect: 'follow', | ||
headers: { | ||
'content-type': 'text/plain', | ||
apikey: apiKey | ||
}, | ||
body: content | ||
} | ||
|
||
const response = await fetch(url, options) | ||
if (!response.ok) { | ||
const message = `An error has occured: ${response.status}` | ||
throw new Error(message) | ||
} | ||
return await response.json() | ||
} | ||
|
||
}) | ||
} | ||
|
||
module.exports = fp(apiLayer) |
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,22 @@ | ||
const fp = require('fastify-plugin') | ||
const schema = require('./schema') | ||
|
||
async function comments (server, options, done) { | ||
const sentimentService = server.sentimentService | ||
|
||
server.route({ | ||
method: 'POST', | ||
path: options.prefix + 'sentiment/score', | ||
onRequest: [server.authenticate_optional], | ||
schema: schema.sentiment, | ||
handler: onSentimentScore | ||
}) | ||
async function onSentimentScore (req, reply) { | ||
const score = await sentimentService.getSentiment(req.body.content) | ||
return { score } | ||
} | ||
|
||
done() | ||
} | ||
|
||
module.exports = fp(comments) |
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,11 @@ | ||
const S = require('fluent-json-schema') | ||
|
||
const sentiment = { | ||
body: S.object() | ||
.prop('content', S.string().required()), | ||
response: { | ||
200: S.object().prop('score', S.string().required()) | ||
} | ||
} | ||
|
||
module.exports = { sentiment } |
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,61 @@ | ||
'use strict' | ||
const fp = require('fastify-plugin') | ||
|
||
async function sentimentService (fastify, options) { | ||
const apiLayer = fastify.apiLayer | ||
|
||
fastify.decorate('sentimentService', { | ||
async getSentiment (content) { | ||
const slices = this.splitString(content, 1000) | ||
|
||
let score = 0 | ||
for (const slice of slices) { | ||
score += await this.getSentimentApi(slice) | ||
} | ||
return score / slices.length | ||
}, | ||
|
||
async getSentimentApi (content) { | ||
try { | ||
const response = await apiLayer.post(content, | ||
'https://api.apilayer.com/sentiment/analysis') | ||
switch (response.sentiment) { | ||
case 'positive': | ||
return 1 | ||
case 'negative': | ||
return -1 | ||
default: | ||
return 0 | ||
} | ||
} catch (err) { | ||
console.log(err) | ||
return 0 | ||
} | ||
}, | ||
|
||
splitString (str, chunkSize = 1000) { | ||
const words = str.split(' ') | ||
const chunks = [] | ||
let currentChunk = '' | ||
|
||
for (let i = 0; i < words.length; i++) { | ||
const word = words[i] | ||
|
||
if ((currentChunk + ' ' + word).length <= chunkSize) { | ||
currentChunk += (currentChunk === '' ? '' : ' ') + word | ||
} else { | ||
chunks.push(currentChunk) | ||
currentChunk = word | ||
} | ||
} | ||
|
||
if (currentChunk !== '') { | ||
chunks.push(currentChunk) | ||
} | ||
return chunks | ||
} | ||
|
||
}) | ||
} | ||
|
||
module.exports = fp(sentimentService) |
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,20 @@ | ||
const test = require('tap').test | ||
const apiLayer = require('../../../lib/plugins/apilayer') | ||
const fetchMock = require('fetch-mock') | ||
|
||
test('post returns the expected response', async (t) => { | ||
let myservice | ||
const fastify = { | ||
decorate: | ||
(name, service) => { | ||
myservice = service | ||
} | ||
} | ||
await apiLayer(fastify, { apilayer: { key: '123' } }) | ||
|
||
fetchMock.mock('*', { tags: ['tag1', 'tag2'] }) | ||
const response = await myservice.post('Some content', 'host', 'url') | ||
|
||
t.ok(fetchMock.called(), 'fetch was called') | ||
t.equal(response.tags[0], 'tag1', 'should return the expected response') | ||
}) |
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,54 @@ | ||
const test = require('tap').test | ||
const sentimentService = require('../../lib/services/sentiment') | ||
|
||
test('getSentiment returns the expected score', async (t) => { | ||
let myservice | ||
const fastify = { | ||
decorate: | ||
(name, service) => { | ||
myservice = service | ||
}, | ||
apiLayer: { | ||
post: () => { | ||
return { sentiment: 'positive' } | ||
} | ||
} | ||
} | ||
await sentimentService(fastify, {}) | ||
|
||
const response = await myservice.getSentiment('Some content') | ||
t.equal(response, 1, 'should return the expected score') | ||
}) | ||
|
||
test('splitString returns correct chunks number with long text', async (t) => { | ||
let myservice | ||
const fastify = { | ||
decorate: | ||
(name, service) => { | ||
myservice = service | ||
} | ||
} | ||
await sentimentService(fastify, {}) | ||
|
||
const response = await myservice.splitString('Some content '.repeat(10), 100) | ||
t.equal(response.length, 2, 'should return the expected number of chunks') | ||
}) | ||
|
||
test('getSentimentApi returns 0 on apiLayer error', async (t) => { | ||
let myservice | ||
const fastify = { | ||
decorate: | ||
(name, service) => { | ||
myservice = service | ||
}, | ||
rapidApi: { | ||
post: () => { | ||
throw new Error('Error') | ||
} | ||
} | ||
} | ||
await sentimentService(fastify, {}) | ||
|
||
const response = await myservice.getSentimentApi('Some content') | ||
t.equal(response, 0, 'should return 0 on error') | ||
}) |