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

Refactor [GemDownloads] for readability; improve error messages #2870

Merged
merged 2 commits into from
Jan 28, 2019
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
138 changes: 74 additions & 64 deletions services/gem/gem-downloads.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ const Joi = require('joi')
const { downloadCount } = require('../../lib/color-formatters')
const { metric } = require('../../lib/text-formatters')
const { latest: latestVersion } = require('../../lib/version')
const { BaseJsonService, InvalidResponse } = require('..')
const { BaseJsonService, InvalidParameter, InvalidResponse } = require('..')
const { nonNegativeInteger } = require('../validators')

const keywords = ['ruby']

const gemsSchema = Joi.object({
const gemSchema = Joi.object({
downloads: nonNegativeInteger,
version_downloads: nonNegativeInteger,
}).required()

const versionsSchema = Joi.array()
const versionSchema = Joi.array()
.items(
Joi.object({
prerelease: Joi.boolean().required(),
Expand All @@ -27,76 +27,85 @@ const versionsSchema = Joi.array()
.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,
async fetchDownloadCountForVersion({ gem, version }) {
const json = await this._requestJson({
url: `https://rubygems.org/api/v1/versions/${gem}.json`,
schema: versionSchema,
errorMessages: {
404: 'gem not found',
},
})

let wantedVersion
if (version === 'stable') {
wantedVersion = latestVersion(
json.filter(({ prerelease }) => !prerelease).map(({ number }) => number)
)
} else {
wantedVersion = version
}

const versionData = json.find(({ number }) => number === wantedVersion)
if (versionData) {
return versionData.downloads_count
} else {
throw new InvalidResponse({
prettyMessage: 'version not found',
})
}
}

async fetchDownloadCountForGem({ gem }) {
const {
downloads: totalDownloads,
version_downloads: versionDownloads,
} = await this._requestJson({
url: `https://rubygems.org/api/v1/gems/${gem}.json`,
schema: gemSchema,
errorMessages: {
404: 'gem not found',
},
})
return { totalDownloads, versionDownloads }
}

static render({ label, downloads }) {
static render({ which, version, downloads }) {
let label
if (version) {
label = `downloads@${version}`
} else if (which === 'dtv') {
label = 'downloads@latest'
}

return {
label,
message: metric(downloads),
color: downloadCount(downloads),
}
}

static _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.constructor._getLabel(version, info)
const json = await this.fetch({ repo, info })

async handle({ which, gem, version }) {
let downloads
if (info === 'dt') {
downloads = json.downloads
} else if (info === 'dtv') {
downloads = json.version_downloads
} else if (info === 'dv') {
let versionData
if (version !== null && version === 'stable') {
const versions = json
.filter(ver => ver.prerelease === false)
.map(ver => ver.number)
// Found latest stable version.
const stableVersion = latestVersion(versions)
versionData = json.filter(ver => ver.number === stableVersion)[0]
downloads = versionData.downloads_count
} else if (version !== null) {
versionData = json.filter(ver => ver.number === version)[0]

downloads = versionData.downloads_count
} else {
throw new InvalidResponse({
underlyingError: new Error('version is null'),
if (which === 'dv') {
if (!version) {
throw new InvalidParameter({
prettyMessage: 'version downloads requires a version',
})
}
if (version !== 'stable' && !semver.valid(version)) {
throw new InvalidParameter({
prettyMessage: 'version should be "stable" or valid semver',
})
}
downloads = await this.fetchDownloadCountForVersion({ gem, version })
} else {
throw new InvalidResponse({
underlyingError: new Error('info is invalid'),
})
const {
totalDownloads,
versionDownloads,
} = await this.fetchDownloadCountForGem({ gem, which })
downloads = which === 'dtv' ? versionDownloads : totalDownloads
}

return this.constructor.render({ label, downloads })
return this.constructor.render({ which, version, downloads })
}

// Metadata
Expand All @@ -111,8 +120,7 @@ module.exports = class GemDownloads extends BaseJsonService {
static get route() {
return {
base: 'gem',
format: '(dt|dtv|dv)/(.+)',
capture: ['info', 'rubygem'],
pattern: ':which(dt|dtv|dv)/:gem/:version?',
}
}

Expand All @@ -126,7 +134,8 @@ module.exports = class GemDownloads extends BaseJsonService {
version: 'stable',
},
staticPreview: this.render({
label: this._getLabel('stable', 'dv'),
which: 'dv',
version: 'stable',
downloads: 70000,
}),
keywords,
Expand All @@ -139,7 +148,8 @@ module.exports = class GemDownloads extends BaseJsonService {
version: '4.1.0',
},
staticPreview: this.render({
label: this._getLabel('4.1.0', 'dv'),
which: 'dv',
version: '4.1.0',
downloads: 50000,
}),
keywords,
Expand All @@ -149,7 +159,7 @@ module.exports = class GemDownloads extends BaseJsonService {
pattern: 'dtv/:gem',
namedParams: { gem: 'rails' },
staticPreview: this.render({
label: this._getLabel(undefined, 'dtv'),
which: 'dtv',
downloads: 70000,
}),
keywords,
Expand All @@ -159,7 +169,7 @@ module.exports = class GemDownloads extends BaseJsonService {
pattern: 'dt/:gem',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add some title descriptors/differentiators? May just be me since I've never done much with Ruby but it wasn't immediately clear to me which example was which. I know those weren't changed in this PR, just a general thought

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, I just tried to do this and I'm actually not sure what the differences are. dt is total downloads, and I think dtv is downloads for latest version. But then I'm not sure what the difference is between that and dv with stable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chris48s Do you know off hand what these three routes represent?

Either way, let's tackle this in a follow-on.

namedParams: { gem: 'rails' },
staticPreview: this.render({
label: this._getLabel(undefined, 'dt'),
which: 'dt',
downloads: 900000,
}),
keywords,
Expand Down
26 changes: 18 additions & 8 deletions services/gem/gem-downloads.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { isMetric } = require('../test-validators')

const t = (module.exports = require('..').createServiceTester())

// total downloads
t.create('total downloads (valid)')
.get('/dt/rails.json')
.expectJSONTypes(
Expand All @@ -17,9 +16,8 @@ t.create('total downloads (valid)')

t.create('total downloads (not found)')
.get('/dt/not-a-package.json')
.expectJSON({ name: 'downloads', value: 'not found' })
.expectJSON({ name: 'downloads', value: 'gem not found' })

// version downloads
t.create('version downloads (valid, stable version)')
.get('/dv/rails/stable.json')
.expectJSONTypes(
Expand All @@ -40,17 +38,29 @@ t.create('version downloads (valid, specific version)')

t.create('version downloads (package not found)')
.get('/dv/not-a-package/4.1.0.json')
.expectJSON({ name: 'downloads', value: 'not found' })
.expectJSON({ name: 'downloads', value: 'gem not found' })

t.create('version downloads (valid package, invalid version)')
.get('/dv/rails/not-a-version.json')
.expectJSON({ name: 'downloads', value: 'invalid' })
.expectJSON({
name: 'downloads',
value: 'version should be "stable" or valid semver',
})

t.create('version downloads (valid package, version not found)')
.get('/dv/rails/8.8.8.json')
.expectJSON({
name: 'downloads',
value: 'version not found',
})

t.create('version downloads (valid package, version not specified)')
.get('/dv/rails.json')
.expectJSON({ name: 'downloads', value: 'invalid' })
.expectJSON({
name: 'downloads',
value: 'version downloads requires a version',
})

// latest version downloads
t.create('latest version downloads (valid)')
.get('/dtv/rails.json')
.expectJSONTypes(
Expand All @@ -62,4 +72,4 @@ t.create('latest version downloads (valid)')

t.create('latest version downloads (not found)')
.get('/dtv/not-a-package.json')
.expectJSON({ name: 'downloads', value: 'not found' })
.expectJSON({ name: 'downloads', value: 'gem not found' })