Skip to content

Commit

Permalink
fix: use nouns for weblate badge routes
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed Jul 9, 2021
1 parent db1a796 commit d1df97c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ const queryParamSchema = Joi.object({
server: optionalUrl,
}).required()

module.exports = class WeblateEntityCount extends BaseJsonService {
export default class WeblateEntities extends BaseJsonService {
static category = 'other'
static route = {
base: 'weblate/count',
base: 'weblate',
pattern: ':type(components|projects|users|languages)',
queryParamSchema,
}

static examples = [
{
title: `Weblate entity count`,
title: `Weblate entities`,
namedParams: { type: 'projects' },
queryParams: { server: 'https://hosted.weblate.org' },
staticPreview: this.render({ type: 'projects', count: 533 }),
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ const queryParamSchema = Joi.object({
export default class WeblateUserStatistic extends BaseJsonService {
static category = 'other'
static route = {
base: 'weblate/user',
base: 'weblate',
pattern:
':user/:statistic(translated|suggested|languages|uploaded|commented)',
':statistic(translations|suggestions|languages|uploads|comments)/:user',
queryParamSchema,
}

static examples = [
{
title: `Weblate user statistic`,
namedParams: { user: 'nijel', statistic: 'translated' },
namedParams: { statistic: 'translations', user: 'nijel' },
queryParams: { server: 'https://hosted.weblate.org' },
staticPreview: this.render({ type: 'translated', count: 30585 }),
staticPreview: this.render({ statistic: 'translations', count: 30585 }),
keywords: ['i18n', 'internationalization'],
},
]

static defaultBadgeData = { color: 'informational' }

static render({ type, count }) {
return { label: type, message: metric(count) }
static render({ statistic, count }) {
return { label: statistic, message: metric(count) }
}

async fetch({ user, server = 'https://hosted.weblate.org' }) {
Expand All @@ -53,7 +53,29 @@ export default class WeblateUserStatistic extends BaseJsonService {
}

async handle({ user, statistic }, { server }) {
let upstreamStatisticName

switch (statistic) {
case 'translations':
upstreamStatisticName = 'translated'
break
case 'suggestions':
upstreamStatisticName = 'suggested'
break
case 'uploads':
upstreamStatisticName = 'uploaded'
break
case 'comments':
upstreamStatisticName = 'commented'
break
default:
upstreamStatisticName = statistic
}

const data = await this.fetch({ user, server })
return this.constructor.render({ type: statistic, count: data[statistic] })
return this.constructor.render({
statistic,
count: data[upstreamStatisticName],
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { isMetric } from '../test-validators.js'
export const t = await createServiceTester()

t.create('Translations')
.get('/nijel/translations.json?server=https://hosted.weblate.org')
.get('/translations/nijel.json?server=https://hosted.weblate.org')
.expectBadge({ label: 'translations', message: isMetric })

t.create('Suggestions')
.get('/nijel/suggestions.json?server=https://hosted.weblate.org')
.get('/suggestions/nijel.json?server=https://hosted.weblate.org')
.expectBadge({ label: 'suggestions', message: isMetric })

t.create('Languages')
.get('/nijel/languages.json?server=https://hosted.weblate.org')
.get('/languages/nijel.json?server=https://hosted.weblate.org')
.expectBadge({ label: 'languages', message: isMetric })

0 comments on commit d1df97c

Please sign in to comment.