-
-
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.
- Loading branch information
Showing
4 changed files
with
96 additions
and
5 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,71 @@ | ||
'use strict' | ||
|
||
const Joi = require('joi') | ||
const { downloadCount } = require('../color-formatters') | ||
const { metric } = require('../text-formatters') | ||
const { BaseJsonService } = require('..') | ||
const { nonNegativeInteger } = require('../validators') | ||
|
||
const schema = Joi.object({ | ||
analytics: Joi.object({ | ||
install: Joi.object({ | ||
'30d': Joi.object().required(), | ||
'90d': Joi.object().required(), | ||
'365d': Joi.object().required(), | ||
}).required(), | ||
}).required(), | ||
}).required() | ||
|
||
const periodMap = { | ||
dm: { | ||
api_field: '30d', | ||
suffix: '/month', | ||
}, | ||
dy: { | ||
api_field: '365d', | ||
suffix: '/year', | ||
}, | ||
} | ||
|
||
module.exports = class Homebrew extends BaseJsonService { | ||
static category = 'downloads' | ||
|
||
static route = { | ||
base: 'homebrew/downloads', | ||
pattern: ':period(dm|dy)/:formula', | ||
} | ||
|
||
static render({ period, downloads }) { | ||
return { | ||
message: `${metric(downloads)}${periodMap[period].suffix}`, | ||
color: downloadCount(downloads), | ||
} | ||
} | ||
|
||
static examples = [ | ||
{ | ||
title: 'homebrew downloads', | ||
namedParams: { period: 'dm', formula: 'cake' }, | ||
staticPreview: this.render({ period: 'dm', downloads: 93 }), | ||
}, | ||
] | ||
|
||
static defaultBadgeData = { label: 'downloads' } | ||
|
||
async fetch({ formula }) { | ||
return this._requestJson({ | ||
schema, | ||
url: `https://formulae.brew.sh/api/formula/${formula}.json`, | ||
errorMessages: { 404: 'package not found' }, | ||
}) | ||
} | ||
|
||
async handle({ period, formula }) { | ||
const data = await this.fetch({ formula }) | ||
return this.constructor.render({ | ||
period, | ||
downloads: | ||
data.analytics['install'][periodMap[period].api_field][formula], | ||
}) | ||
} | ||
} |
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,20 @@ | ||
'use strict' | ||
|
||
const t = (module.exports = require('../tester').createServiceTester()) | ||
const { isMetricOverTimePeriod } = require('../test-validators') | ||
|
||
t.create('daily downloads (valid)') | ||
.get('/dm/cake.json') | ||
.expectBadge({ label: 'downloads', message: isMetricOverTimePeriod }) | ||
|
||
t.create('yearly downloads (valid)') | ||
.get('/dy/cake.json') | ||
.expectBadge({ label: 'downloads', message: isMetricOverTimePeriod }) | ||
|
||
t.create('daily downloads (not found)') | ||
.get('/dm/not-a-package.json') | ||
.expectBadge({ label: 'downloads', message: 'package not found' }) | ||
|
||
t.create('yearly downloads (not found)') | ||
.get('/dy/not-a-package.json') | ||
.expectBadge({ label: 'downloads', message: 'package 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
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