Skip to content

Commit

Permalink
add [homebrew] cask download badge (#10595)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
tapaj and chris48s authored Oct 12, 2024
1 parent f767fab commit 8ed3dc8
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 3 deletions.
82 changes: 82 additions & 0 deletions services/homebrew/homebrew-cask-downloads.service.js
Original file line number Diff line number Diff line change
@@ -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,
})
}
}
28 changes: 28 additions & 0 deletions services/homebrew/homebrew-cask-downloads.tester.js
Original file line number Diff line number Diff line change
@@ -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' })
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 8ed3dc8

Please sign in to comment.