1
0
mirror of https://github.com/badges/shields.git synced 2025-04-18 19:44:04 +03:00

add [homebrew] cask download badge (#10595)

* add homebrew cask download badge

* add homebrew cask download badge

* fix: updates test cases

* fix: updates test cases

* tidy up homebrew filenames and docs site titles

---------

Co-authored-by: chris48s <git@chris-shaw.dev>
This commit is contained in:
Tapaj Kumar Das 2024-10-12 17:32:23 +05:30 committed by GitHub
parent f767fabf43
commit 8ed3dc8db4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 113 additions and 3 deletions

View File

@ -0,0 +1,82 @@
import Joi from 'joi'
import { renderDownloadsBadge } from '../downloads.js'
import { BaseJsonService, pathParams } from '../index.js'
import { nonNegativeInteger } from '../validators.js'
function getSchema({ cask }) {
return Joi.object({
analytics: Joi.object({
install: Joi.object({
'30d': Joi.object({ [cask]: nonNegativeInteger }).required(),
'90d': Joi.object({ [cask]: nonNegativeInteger }).required(),
'365d': Joi.object({ [cask]: nonNegativeInteger }).required(),
}).required(),
}).required(),
}).required()
}
const periodMap = {
dm: {
api_field: '30d',
interval: 'month',
},
dq: {
api_field: '90d',
interval: 'quarter',
},
dy: {
api_field: '365d',
interval: 'year',
},
}
export default class HomebrewCaskDownloads extends BaseJsonService {
static category = 'downloads'
static route = {
base: 'homebrew/cask/installs',
pattern: ':interval(dm|dq|dy)/:cask',
}
static openApi = {
'/homebrew/cask/installs/{interval}/{cask}': {
get: {
summary: 'Homebrew Cask Downloads',
parameters: pathParams(
{
name: 'interval',
example: 'dm',
schema: { type: 'string', enum: this.getEnum('interval') },
description: 'Monthly, Quarterly or Yearly downloads',
},
{
name: 'cask',
example: 'freetube',
},
),
},
},
}
static defaultBadgeData = { label: 'downloads' }
async fetch({ cask }) {
const schema = getSchema({ cask })
return this._requestJson({
schema,
url: `https://formulae.brew.sh/api/cask/${cask}.json`,
httpErrors: { 404: 'cask not found' },
})
}
async handle({ interval, cask }) {
const {
analytics: { install },
} = await this.fetch({ cask })
return renderDownloadsBadge({
downloads: install[periodMap[interval].api_field][cask],
interval: periodMap[interval].interval,
})
}
}

View File

@ -0,0 +1,28 @@
import { createServiceTester } from '../tester.js'
import { isMetricOverTimePeriod } from '../test-validators.js'
export const t = await createServiceTester()
t.create('daily downloads (valid)')
.get('/dm/freetube.json')
.expectBadge({ label: 'downloads', message: isMetricOverTimePeriod })
t.create('yearly downloads (valid)')
.get('/dq/freetube.json')
.expectBadge({ label: 'downloads', message: isMetricOverTimePeriod })
t.create('yearly downloads (valid)')
.get('/dy/freetube.json')
.expectBadge({ label: 'downloads', message: isMetricOverTimePeriod })
t.create('daily downloads (not found)')
.get('/dm/not-a-package.json')
.expectBadge({ label: 'downloads', message: 'cask not found' })
t.create('yearly downloads (not found)')
.get('/dq/not-a-package.json')
.expectBadge({ label: 'downloads', message: 'cask not found' })
t.create('yearly downloads (not found)')
.get('/dy/not-a-package.json')
.expectBadge({ label: 'downloads', message: 'cask not found' })

View File

@ -13,7 +13,7 @@ export default class HomebrewCask extends BaseJsonService {
static openApi = {
'/homebrew/cask/v/{cask}': {
get: {
summary: 'homebrew cask',
summary: 'Homebrew Cask Version',
parameters: pathParams({
name: 'cask',
example: 'iterm2',

View File

@ -41,7 +41,7 @@ export default class HomebrewDownloads extends BaseJsonService {
static openApi = {
'/homebrew/installs/{interval}/{formula}': {
get: {
summary: 'homebrew downloads',
summary: 'Homebrew Formula Downloads',
parameters: pathParams(
{
name: 'interval',

View File

@ -16,7 +16,7 @@ export default class HomebrewVersion extends BaseJsonService {
static openApi = {
'/homebrew/v/{formula}': {
get: {
summary: 'homebrew version',
summary: 'Homebrew Formula Version',
parameters: pathParams({
name: 'formula',
example: 'cake',