Files
shields/services/wordpress/wordpress-rating.service.js
Paul Melnikow 02ec19fd22 BaseService terminology: Rename url to route (#2278)
The term “url” is overloaded in services, to refer to the Shields route and also the API URL. Calling the Shields URL a “route” is on the whole more descriptive, and makes it clearer and more obvious which one of these we’re talking about. It’s a small thing, though seems like an improvement.

We have a few functions called `buildUrl`. I’ve renamed them to `buildRoute` when they refer to routes, and left them as `buildUrl` when they refer to API URLs.

I included a minor style tweak and some formatting cleanup in `TUTORIAL.md`.
2018-11-09 15:11:03 -05:00

117 lines
2.7 KiB
JavaScript

'use strict'
const BaseWordpress = require('./wordpress-base')
const { starRating, metric } = require('../../lib/text-formatters')
const { floorCount } = require('../../lib/color-formatters')
const extensionData = {
plugin: {
capt: 'Plugin',
exampleSlug: 'bbpress',
},
theme: {
capt: 'Theme',
exampleSlug: 'twentyseventeen',
},
}
class WordpressRatingBase extends BaseWordpress {
static render({ response }) {
const total = response.num_ratings
const rating = ((response.rating / 100) * 5).toFixed(1)
return {
message: `${rating}/5 (${metric(total)})`,
color: floorCount(rating, 2, 3, 4),
}
}
static get category() {
return 'rating'
}
static get defaultBadgeData() {
return { label: 'rating' }
}
}
function RatingForExtensionType(extensionType) {
const { capt, exampleSlug } = extensionData[extensionType]
return class WordpressRating extends WordpressRatingBase {
static get route() {
return {
base: `wordpress/${extensionType}/rating`,
pattern: ':slug',
}
}
static get extensionType() {
return extensionType
}
static get examples() {
return [
{
title: `Wordpress ${capt} Rating`,
exampleUrl: exampleSlug,
urlPattern: ':slug',
staticExample: this.render({
response: {
rating: 80,
num_ratings: 100,
},
}),
keywords: ['wordpress'],
},
]
}
}
}
function StarsForExtensionType(extensionType) {
const { capt, exampleSlug } = extensionData[extensionType]
return class WordpressStars extends WordpressRatingBase {
static render({ response }) {
const rating = (response.rating / 100) * 5
return { message: starRating(rating), color: floorCount(rating, 2, 3, 4) }
}
static get route() {
return {
base: `wordpress/${extensionType}`,
format: '(?:stars|r)/(.+)',
capture: ['slug'],
}
}
static get extensionType() {
return extensionType
}
static get examples() {
return [
{
title: `Wordpress ${capt} Rating`,
exampleUrl: `stars/${exampleSlug}`,
urlPattern: 'stars/:slug',
staticExample: this.render({
response: {
rating: 80,
num_ratings: 100,
},
}),
keywords: ['wordpress'],
documentation: 'There is an alias <code>/r/:slug.svg</code> aswell',
},
]
}
}
}
const ratingsServices = ['plugin', 'theme'].map(RatingForExtensionType)
const starsServices = ['plugin', 'theme'].map(StarsForExtensionType)
const modules = [...ratingsServices, ...starsServices]
module.exports = modules