Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions services/thunderbird/thunderbird-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Joi from 'joi'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService } from '../index.js'

const description = '[addons.thunderbird.net](https://addons.thunderbird.net)'
const schema = Joi.object({
average_daily_users: nonNegativeInteger,
current_version: Joi.object({
version: Joi.string().required(),
}).required(),
ratings: Joi.object({
average: Joi.number().required(),
}).required(),
weekly_downloads: nonNegativeInteger,
}).required()

class BaseThunderbirdService extends BaseJsonService {
static defaultBadgeData = { label: 'thunderbird add-on' }
async fetch({ addonId }) {
return this._requestJson({
schema,
url: `https://addons.thunderbird.net/api/v4/addons/addon/${addonId}/`,
})
}
}

export { BaseThunderbirdService, description }
45 changes: 45 additions & 0 deletions services/thunderbird/thunderbird-downloads.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { renderDownloadsBadge } from '../downloads.js'
import { pathParams } from '../index.js'
import {
BaseThunderbirdService,
description as baseDescription,
} from './thunderbird-base.js'

const description = `${baseDescription}

Previously \`thunderbird/d\` provided a “total downloads” badge. However,
[updates to the v3 API](https://github.com/badges/shields/issues/3079)
only give us weekly downloads. The route \`thunderbird/d\` redirects to \`thunderbird/dw\`.
`

class ThunderbirdWeeklyDownloads extends BaseThunderbirdService {
static category = 'downloads'
static route = { base: 'thunderbird/dw', pattern: ':addonId' }

static openApi = {
'/thunderbird/dw/{addonId}': {
get: {
summary: 'Thunderbird Add-on Downloads',
description,
parameters: pathParams({ name: 'addonId', example: 'dustman' }),
},
},
}

static _cacheLength = 21600

static defaultBadgeData = { label: 'downloads' }

static render({ downloads }) {
return renderDownloadsBadge({ downloads, interval: 'week' })
}

async handle({ addonId }) {
const data = await this.fetch({ addonId })
return this.constructor.render({
downloads: data.weekly_downloads,
})
}
}

export { ThunderbirdWeeklyDownloads }
15 changes: 15 additions & 0 deletions services/thunderbird/thunderbird-downloads.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ServiceTester } from '../tester.js'
import { isMetricOverTimePeriod } from '../test-validators.js'
export const t = new ServiceTester({
id: 'ThunderbirdDownloads',
title: 'ThunderbirdDownloads',
pathPrefix: '/thunderbird',
})

t.create('Weekly Downloads')
.get('/dw/dustman.json')
.expectBadge({ label: 'downloads', message: isMetricOverTimePeriod })

t.create('Weekly Downloads (not found)')
.get('/dw/not-a-real-plugin.json')
.expectBadge({ label: 'downloads', message: 'not found' })
47 changes: 47 additions & 0 deletions services/thunderbird/thunderbird-rating.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { starRating } from '../text-formatters.js'
import { floorCount as floorCountColor } from '../color-formatters.js'
import { pathParams } from '../index.js'
import { BaseThunderbirdService, description } from './thunderbird-base.js'

export default class ThunderbirdRating extends BaseThunderbirdService {
static category = 'rating'
static route = {
base: 'thunderbird',
pattern: ':format(stars|rating)/:addonId',
}

static openApi = {
'/thunderbird/rating/{addonId}': {
get: {
summary: 'Thunderbird Add-on Rating',
description,
parameters: pathParams({ name: 'addonId', example: 'dustman' }),
},
},
'/thunderbird/stars/{addonId}': {
get: {
summary: 'Thunderbird Add-on Stars',
description,
parameters: pathParams({ name: 'addonId', example: 'dustman' }),
},
},
}

static _cacheLength = 7200

static render({ format, rating }) {
return {
label: format,
message:
format === 'stars'
? starRating(rating)
: `${Math.round(rating * 10) / 10}/5`,
color: floorCountColor(rating, 2, 3, 4),
}
}

async handle({ format, addonId }) {
const data = await this.fetch({ addonId })
return this.constructor.render({ format, rating: data.ratings.average })
}
}
19 changes: 19 additions & 0 deletions services/thunderbird/thunderbird-rating.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Joi from 'joi'
import { isStarRating } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('Rating')
.get('/rating/dustman.json')
.expectBadge({
label: 'rating',
message: Joi.string().regex(/^\d(\.\d)?\/\d$/),
})

t.create('Stars')
.get('/stars/dustman.json')
.expectBadge({ label: 'stars', message: isStarRating })

t.create('Rating (not found)')
.get('/rating/not-a-real-plugin.json')
.expectBadge({ label: 'thunderbird add-on', message: 'not found' })
Loading