Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Homebrew] Add homebrew downloads badge #6209

Merged
merged 2 commits into from
Feb 22, 2021
Merged
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
77 changes: 77 additions & 0 deletions services/homebrew/homebrew-downloads.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict'

const Joi = require('joi')
const { downloadCount } = require('../color-formatters')
const { metric } = require('../text-formatters')
const { BaseJsonService } = require('..')
const { nonNegativeInteger } = require('../validators')

function getSchema({ formula }) {
return Joi.object({
analytics: Joi.object({
install: Joi.object({
'30d': Joi.object({ [formula]: nonNegativeInteger }).required(),
'90d': Joi.object({ [formula]: nonNegativeInteger }).required(),
'365d': Joi.object({ [formula]: nonNegativeInteger }).required(),
}).required(),
}).required(),
}).required()
}

const periodMap = {
dm: {
api_field: '30d',
suffix: '/month',
},
dq: {
api_field: '90d',
suffix: '/quarter',
},
dy: {
api_field: '365d',
suffix: '/year',
},
}

module.exports = class HomebrewDownloads extends BaseJsonService {
static category = 'downloads'

static route = {
base: 'homebrew',
pattern: 'installs/:interval(dm|dq|dy)/:formula',
}

static examples = [
{
title: 'homebrew downloads',
namedParams: { interval: 'dm', formula: 'cake' },
staticPreview: this.render({ interval: 'dm', downloads: 93 }),
},
]

static defaultBadgeData = { label: 'downloads' }

static render({ interval, downloads }) {
return {
message: `${metric(downloads)}${periodMap[interval].suffix}`,
color: downloadCount(downloads),
}
}

async fetch({ formula }) {
const schema = getSchema({ formula })
return this._requestJson({
schema,
url: `https://formulae.brew.sh/api/formula/${formula}.json`,
errorMessages: { 404: 'formula not found' },
})
}

async handle({ interval, formula }) {
const data = await this.fetch({ formula })
return this.constructor.render({
interval,
downloads: data.analytics.install[periodMap[interval].api_field][formula],
})
}
}
28 changes: 28 additions & 0 deletions services/homebrew/homebrew-downloads.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const t = (module.exports = require('../tester').createServiceTester())
const { isMetricOverTimePeriod } = require('../test-validators')

t.create('daily downloads (valid)')
.get('/installs/dm/cake.json')
.expectBadge({ label: 'downloads', message: isMetricOverTimePeriod })

t.create('yearly downloads (valid)')
.get('/installs/dq/cake.json')
.expectBadge({ label: 'downloads', message: isMetricOverTimePeriod })

t.create('yearly downloads (valid)')
.get('/installs/dy/cake.json')
.expectBadge({ label: 'downloads', message: isMetricOverTimePeriod })

t.create('daily downloads (not found)')
.get('/installs/dm/not-a-package.json')
.expectBadge({ label: 'downloads', message: 'formula not found' })

t.create('yearly downloads (not found)')
.get('/installs/dq/not-a-package.json')
.expectBadge({ label: 'downloads', message: 'formula not found' })

t.create('yearly downloads (not found)')
.get('/installs/dy/not-a-package.json')
.expectBadge({ label: 'downloads', message: 'formula not found' })
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const schema = Joi.object({
}).required(),
}).required()

module.exports = class Homebrew extends BaseJsonService {
module.exports = class HomebrewVersion extends BaseJsonService {
static category = 'version'

static route = { base: 'homebrew/v', pattern: ':formula' }

static examples = [
{
title: 'homebrew',
title: 'homebrew version',
namedParams: { formula: 'cake' },
staticPreview: renderVersionBadge({ version: 'v0.32.0' }),
},
Expand Down
6 changes: 4 additions & 2 deletions services/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ const isMetricOverMetric = withRegex(
)

const isMetricOverTimePeriod = withRegex(
/^([1-9][0-9]*[kMGTPEZY]?|[1-9]\.[1-9][kMGTPEZY])\/(year|month|four weeks|week|day)$/
/^([1-9][0-9]*[kMGTPEZY]?|[1-9]\.[1-9][kMGTPEZY])\/(year|month|four weeks|quarter|week|day)$/
)

const isZeroOverTimePeriod = withRegex(/^0\/(year|month|four weeks|week|day)$/)
const isZeroOverTimePeriod = withRegex(
/^0\/(year|month|four weeks|quarter|week|day)$/
)

const isIntegerPercentage = withRegex(/^[1-9][0-9]?%|^100%|^0%$/)
const isDecimalPercentage = withRegex(/^[0-9]+\.[0-9]*%$/)
Expand Down