@@ -27,92 +27,6 @@ const versionSchema = Joi.array()
|
||||
.required()
|
||||
|
||||
module.exports = class GemDownloads extends BaseJsonService {
|
||||
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({ 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),
|
||||
}
|
||||
}
|
||||
|
||||
async handle({ which, gem, version }) {
|
||||
let downloads
|
||||
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 {
|
||||
const {
|
||||
totalDownloads,
|
||||
versionDownloads,
|
||||
} = await this.fetchDownloadCountForGem({ gem, which })
|
||||
downloads = which === 'dtv' ? versionDownloads : totalDownloads
|
||||
}
|
||||
return this.constructor.render({ which, version, downloads })
|
||||
}
|
||||
|
||||
// Metadata
|
||||
static get defaultBadgeData() {
|
||||
return { label: 'downloads' }
|
||||
}
|
||||
|
||||
static get category() {
|
||||
return 'downloads'
|
||||
}
|
||||
@@ -176,4 +90,89 @@ module.exports = class GemDownloads extends BaseJsonService {
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
static get defaultBadgeData() {
|
||||
return { 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),
|
||||
}
|
||||
}
|
||||
|
||||
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 }
|
||||
}
|
||||
|
||||
async handle({ which, gem, version }) {
|
||||
let downloads
|
||||
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 {
|
||||
const {
|
||||
totalDownloads,
|
||||
versionDownloads,
|
||||
} = await this.fetchDownloadCountForGem({ gem, which })
|
||||
downloads = which === 'dtv' ? versionDownloads : totalDownloads
|
||||
}
|
||||
return this.constructor.render({ which, version, downloads })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,31 +7,6 @@ const { floorCount: floorCountColor } = require('../color-formatters')
|
||||
const ownerSchema = Joi.array().required()
|
||||
|
||||
module.exports = class GemOwner extends BaseJsonService {
|
||||
async fetch({ user }) {
|
||||
const url = `https://rubygems.org/api/v1/owners/${user}/gems.json`
|
||||
return this._requestJson({
|
||||
url,
|
||||
schema: ownerSchema,
|
||||
})
|
||||
}
|
||||
|
||||
static render({ count }) {
|
||||
return {
|
||||
message: count,
|
||||
color: floorCountColor(count, 10, 50, 100),
|
||||
}
|
||||
}
|
||||
|
||||
async handle({ user }) {
|
||||
const json = await this.fetch({ user })
|
||||
return this.constructor.render({ count: json.length })
|
||||
}
|
||||
|
||||
// Metadata
|
||||
static get defaultBadgeData() {
|
||||
return { label: 'gems' }
|
||||
}
|
||||
|
||||
static get category() {
|
||||
return 'other'
|
||||
}
|
||||
@@ -53,4 +28,28 @@ module.exports = class GemOwner extends BaseJsonService {
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
static get defaultBadgeData() {
|
||||
return { label: 'gems' }
|
||||
}
|
||||
|
||||
static render({ count }) {
|
||||
return {
|
||||
message: count,
|
||||
color: floorCountColor(count, 10, 50, 100),
|
||||
}
|
||||
}
|
||||
|
||||
async fetch({ user }) {
|
||||
const url = `https://rubygems.org/api/v1/owners/${user}/gems.json`
|
||||
return this._requestJson({
|
||||
url,
|
||||
schema: ownerSchema,
|
||||
})
|
||||
}
|
||||
|
||||
async handle({ user }) {
|
||||
const json = await this.fetch({ user })
|
||||
return this.constructor.render({ count: json.length })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,46 +31,6 @@ const dailySchema = Joi.array()
|
||||
.required()
|
||||
|
||||
module.exports = class GemRank extends BaseJsonService {
|
||||
async fetch({ period, gem }) {
|
||||
let endpoint, schema
|
||||
if (period === 'rt') {
|
||||
endpoint = 'total_ranking.json'
|
||||
schema = totalSchema
|
||||
} else {
|
||||
endpoint = 'daily_ranking.json'
|
||||
schema = dailySchema
|
||||
}
|
||||
|
||||
return this._requestJson({
|
||||
url: `http://bestgems.org/api/v1/gems/${gem}/${endpoint}`,
|
||||
schema,
|
||||
})
|
||||
}
|
||||
|
||||
static render({ period, rank }) {
|
||||
const count = Math.floor(100000 / rank)
|
||||
let message = ordinalNumber(rank)
|
||||
message += period === 'rt' ? '' : ' daily'
|
||||
return {
|
||||
message,
|
||||
color: floorCount(count, 10, 50, 100),
|
||||
}
|
||||
}
|
||||
|
||||
async handle({ period, gem }) {
|
||||
const json = await this.fetch({ period, gem })
|
||||
const rank = period === 'rt' ? json[0].total_ranking : json[0].daily_ranking
|
||||
if (rank == null) {
|
||||
throw new InvalidResponse({ prettyMessage: 'invalid rank' })
|
||||
}
|
||||
return this.constructor.render({ period, rank })
|
||||
}
|
||||
|
||||
// Metadata
|
||||
static get defaultBadgeData() {
|
||||
return { label: 'rank' }
|
||||
}
|
||||
|
||||
static get category() {
|
||||
return 'downloads'
|
||||
}
|
||||
@@ -104,4 +64,43 @@ module.exports = class GemRank extends BaseJsonService {
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
static get defaultBadgeData() {
|
||||
return { label: 'rank' }
|
||||
}
|
||||
|
||||
static render({ period, rank }) {
|
||||
const count = Math.floor(100000 / rank)
|
||||
let message = ordinalNumber(rank)
|
||||
message += period === 'rt' ? '' : ' daily'
|
||||
return {
|
||||
message,
|
||||
color: floorCount(count, 10, 50, 100),
|
||||
}
|
||||
}
|
||||
|
||||
async fetch({ period, gem }) {
|
||||
let endpoint, schema
|
||||
if (period === 'rt') {
|
||||
endpoint = 'total_ranking.json'
|
||||
schema = totalSchema
|
||||
} else {
|
||||
endpoint = 'daily_ranking.json'
|
||||
schema = dailySchema
|
||||
}
|
||||
|
||||
return this._requestJson({
|
||||
url: `http://bestgems.org/api/v1/gems/${gem}/${endpoint}`,
|
||||
schema,
|
||||
})
|
||||
}
|
||||
|
||||
async handle({ period, gem }) {
|
||||
const json = await this.fetch({ period, gem })
|
||||
const rank = period === 'rt' ? json[0].total_ranking : json[0].daily_ranking
|
||||
if (rank == null) {
|
||||
throw new InvalidResponse({ prettyMessage: 'invalid rank' })
|
||||
}
|
||||
return this.constructor.render({ period, rank })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,27 +11,6 @@ const schema = Joi.object({
|
||||
}).required()
|
||||
|
||||
module.exports = class GemVersion extends BaseJsonService {
|
||||
async fetch({ gem }) {
|
||||
return this._requestJson({
|
||||
schema,
|
||||
url: `https://rubygems.org/api/v1/gems/${gem}.json`,
|
||||
})
|
||||
}
|
||||
|
||||
static render({ version }) {
|
||||
return renderVersionBadge({ version })
|
||||
}
|
||||
|
||||
async handle({ gem }) {
|
||||
const { version } = await this.fetch({ gem })
|
||||
return this.constructor.render({ version })
|
||||
}
|
||||
|
||||
// Metadata
|
||||
static get defaultBadgeData() {
|
||||
return { label: 'gem' }
|
||||
}
|
||||
|
||||
static get category() {
|
||||
return 'version'
|
||||
}
|
||||
@@ -53,4 +32,24 @@ module.exports = class GemVersion extends BaseJsonService {
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
static get defaultBadgeData() {
|
||||
return { label: 'gem' }
|
||||
}
|
||||
|
||||
static render({ version }) {
|
||||
return renderVersionBadge({ version })
|
||||
}
|
||||
|
||||
async fetch({ gem }) {
|
||||
return this._requestJson({
|
||||
schema,
|
||||
url: `https://rubygems.org/api/v1/gems/${gem}.json`,
|
||||
})
|
||||
}
|
||||
|
||||
async handle({ gem }) {
|
||||
const { version } = await this.fetch({ gem })
|
||||
return this.constructor.render({ version })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user