diff --git a/services/greasyfork/greasyfork-rating.service.js b/services/greasyfork/greasyfork-rating.service.js new file mode 100644 index 0000000000..830d48645d --- /dev/null +++ b/services/greasyfork/greasyfork-rating.service.js @@ -0,0 +1,40 @@ +import { floorCount as floorCountColor } from '../color-formatters.js' +import { metric } from '../text-formatters.js' +import BaseGreasyForkService from './greasyfork-base.js' + +export default class GreasyForkRatingCount extends BaseGreasyForkService { + static category = 'rating' + static route = { base: 'greasyfork', pattern: 'rating-count/:scriptId' } + + static examples = [ + { + title: 'Greasy Fork', + namedParams: { scriptId: '407466' }, + staticPreview: this.render({ good: 17, ok: 2, bad: 3 }), + }, + ] + + static defaultBadgeData = { label: 'rating' } + + static render({ good, ok, bad }) { + let color = 'lightgrey' + const total = good + bad + ok + if (total > 0) { + const score = (good * 3 + ok * 2 + bad * 1) / total - 1 + color = floorCountColor(score, 1, 1.5, 2) + } + return { + message: `${metric(good)} good, ${metric(ok)} ok, ${metric(bad)} bad`, + color, + } + } + + async handle({ scriptId }) { + const data = await this.fetch({ scriptId }) + return this.constructor.render({ + good: data.good_ratings, + ok: data.ok_ratings, + bad: data.bad_ratings, + }) + } +} diff --git a/services/greasyfork/greasyfork-rating.spec.js b/services/greasyfork/greasyfork-rating.spec.js new file mode 100644 index 0000000000..459cb72982 --- /dev/null +++ b/services/greasyfork/greasyfork-rating.spec.js @@ -0,0 +1,31 @@ +import { test, given } from 'sazerac' +import GreasyForkRatingCount from './greasyfork-rating.service.js' + +describe('GreasyForkRatingCount', function () { + test(GreasyForkRatingCount.render, () => { + given({ good: 0, ok: 0, bad: 30 }).expect({ + message: '0 good, 0 ok, 30 bad', + color: 'red', + }) + given({ good: 10, ok: 20, bad: 30 }).expect({ + message: '10 good, 20 ok, 30 bad', + color: 'yellow', + }) + given({ good: 10, ok: 20, bad: 10 }).expect({ + message: '10 good, 20 ok, 10 bad', + color: 'yellowgreen', + }) + given({ good: 20, ok: 10, bad: 0 }).expect({ + message: '20 good, 10 ok, 0 bad', + color: 'green', + }) + given({ good: 30, ok: 0, bad: 0 }).expect({ + message: '30 good, 0 ok, 0 bad', + color: 'brightgreen', + }) + given({ good: 0, ok: 0, bad: 0 }).expect({ + message: '0 good, 0 ok, 0 bad', + color: 'lightgrey', + }) + }) +}) diff --git a/services/greasyfork/greasyfork-rating.tester.js b/services/greasyfork/greasyfork-rating.tester.js new file mode 100644 index 0000000000..1fc2cdedca --- /dev/null +++ b/services/greasyfork/greasyfork-rating.tester.js @@ -0,0 +1,14 @@ +import Joi from 'joi' +import { createServiceTester } from '../tester.js' +export const t = await createServiceTester() + +t.create('Rating Count') + .get('/rating-count/407466.json') + .expectBadge({ + label: 'rating', + message: Joi.string().regex(/^\d+ good, \d+ ok, \d+ bad$/), + }) + +t.create('Rating Count (not found)') + .get('/rating-count/000000.json') + .expectBadge({ label: 'rating', message: 'not found' })