-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
8 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.