-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Validate response json in [apm appveyor cdnjs clojars gem] also affects [npm] #1883
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
07adf6a
explicitly declare fetch() as async
chris48s 8121a90
split gem service into multiple files
chris48s 23890b3
add validation to apm, appveyor, cdnjs, clojars & gem services
chris48s 99fc836
fix the apm examples
chris48s cf04796
DRY up
chris48s ea0bac1
extract a commonly used validator
chris48s 00f4176
give validator more accurate name
chris48s 01981e5
pass the right schema for the right endpoint
chris48s 410b329
make array() required
chris48s 796e6e7
add clarifying comments
chris48s 3cdf4a5
use 2 schemas instead of Joi.alternatives().try() for gem rank
chris48s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
'use strict' | ||
|
||
const semver = require('semver') | ||
const Joi = require('joi') | ||
|
||
const { BaseJsonService } = require('../base') | ||
const { InvalidResponse } = require('../errors') | ||
const { | ||
downloadCount: downloadCountColor, | ||
} = require('../../lib/color-formatters') | ||
const { metric } = require('../../lib/text-formatters') | ||
const { latest: latestVersion } = require('../../lib/version') | ||
const { nonNegativeInteger } = require('../validators.js') | ||
|
||
const gemsSchema = Joi.object({ | ||
downloads: nonNegativeInteger, | ||
version_downloads: nonNegativeInteger, | ||
}).required() | ||
|
||
const versionsSchema = Joi.array() | ||
.items( | ||
Joi.object({ | ||
prerelease: Joi.boolean().required(), | ||
number: Joi.string().required(), | ||
downloads_count: nonNegativeInteger, | ||
}) | ||
) | ||
.min(1) | ||
.required() | ||
|
||
module.exports = class GemDownloads extends BaseJsonService { | ||
async fetch(repo, info) { | ||
const endpoint = info === 'dv' ? 'versions/' : 'gems/' | ||
const schema = info === 'dv' ? versionsSchema : gemsSchema | ||
const url = `https://rubygems.org/api/v1/${endpoint}${repo}.json` | ||
return this._requestJson({ | ||
url, | ||
schema, | ||
}) | ||
} | ||
|
||
_getLabel(version, info) { | ||
if (version) { | ||
return 'downloads@' + version | ||
} else { | ||
if (info === 'dtv') { | ||
return 'downloads@latest' | ||
} else { | ||
return 'downloads' | ||
} | ||
} | ||
} | ||
|
||
async handle({ info, rubygem }) { | ||
const splitRubygem = rubygem.split('/') | ||
const repo = splitRubygem[0] | ||
let version = | ||
splitRubygem.length > 1 ? splitRubygem[splitRubygem.length - 1] : null | ||
version = version === 'stable' ? version : semver.valid(version) | ||
const label = this._getLabel(version, info) | ||
const json = await this.fetch(repo, info) | ||
|
||
let downloads | ||
if (info === 'dt') { | ||
downloads = metric(json.downloads) | ||
} else if (info === 'dtv') { | ||
downloads = metric(json.version_downloads) | ||
} else if (info === 'dv') { | ||
let versionData | ||
if (version !== null && version === 'stable') { | ||
const versions = json | ||
.filter(function(ver) { | ||
return ver.prerelease === false | ||
}) | ||
.map(function(ver) { | ||
return ver.number | ||
}) | ||
// Found latest stable version. | ||
const stableVersion = latestVersion(versions) | ||
versionData = json.filter(function(ver) { | ||
return ver.number === stableVersion | ||
})[0] | ||
downloads = metric(versionData.downloads_count) | ||
} else if (version !== null) { | ||
versionData = json.filter(function(ver) { | ||
return ver.number === version | ||
})[0] | ||
|
||
downloads = metric(versionData.downloads_count) | ||
} else { | ||
throw new InvalidResponse({ | ||
underlyingError: new Error('version is null'), | ||
}) | ||
} | ||
} else { | ||
throw new InvalidResponse({ | ||
underlyingError: new Error('info is invalid'), | ||
}) | ||
} | ||
|
||
return { | ||
label: label, | ||
message: downloads, | ||
color: downloadCountColor(downloads), | ||
} | ||
} | ||
|
||
// Metadata | ||
static get defaultBadgeData() { | ||
return { label: 'downloads' } | ||
} | ||
|
||
static get category() { | ||
return 'downloads' | ||
} | ||
|
||
static get url() { | ||
return { | ||
base: 'gem', | ||
format: '(dt|dtv|dv)/(.+)', | ||
capture: ['info', 'rubygem'], | ||
} | ||
} | ||
|
||
static get examples() { | ||
return [ | ||
{ | ||
title: 'Gem', | ||
previewUrl: 'dv/rails/stable', | ||
keywords: ['ruby'], | ||
}, | ||
{ | ||
title: 'Gem', | ||
previewUrl: 'dv/rails/4.1.0', | ||
keywords: ['ruby'], | ||
}, | ||
{ | ||
title: 'Gem', | ||
previewUrl: 'dtv/rails', | ||
keywords: ['ruby'], | ||
}, | ||
{ | ||
title: 'Gem', | ||
previewUrl: 'dt/rails', | ||
keywords: ['ruby'], | ||
}, | ||
] | ||
} | ||
} |
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,51 @@ | ||
'use strict' | ||
|
||
const Joi = require('joi') | ||
|
||
const { BaseJsonService } = require('../base') | ||
const { floorCount: floorCountColor } = require('../../lib/color-formatters') | ||
|
||
const ownerSchema = Joi.array().required() | ||
|
||
module.exports = class GemOwner extends BaseJsonService { | ||
async handle({ user }) { | ||
const url = `https://rubygems.org/api/v1/owners/${user}/gems.json` | ||
const json = await this._requestJson({ | ||
url, | ||
schema: ownerSchema, | ||
}) | ||
const count = json.length | ||
|
||
return { | ||
message: count, | ||
color: floorCountColor(count, 10, 50, 100), | ||
} | ||
} | ||
|
||
// Metadata | ||
static get defaultBadgeData() { | ||
return { label: 'gems' } | ||
} | ||
|
||
static get category() { | ||
return 'other' | ||
} | ||
|
||
static get url() { | ||
return { | ||
base: 'gem/u', | ||
format: '(.+)', | ||
capture: ['user'], | ||
} | ||
} | ||
|
||
static get examples() { | ||
return [ | ||
{ | ||
title: 'Gems', | ||
previewUrl: 'raphink', | ||
keywords: ['ruby'], | ||
}, | ||
] | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️