fix [gem] rank badge (#2659)

The ranking endpoints return a value for every day. This is rare, but it looks like sometimes this can be `null` (for example if you call http://bestgems.org/api/v1/gems/rack/daily_ranking.json the rank was `null` on `2013-07-02` ) and if the rank has _ever_ been `null` for a package in the past, the schema will fail validating the response.

This modifies the schema to allow a `null` value and adds a case to handle if the rank is `null` today.

closes #2647
This commit is contained in:
chris48s
2019-01-08 05:36:40 +00:00
committed by Paul Melnikow
parent 84a6d28250
commit 7d1a6dd627
2 changed files with 27 additions and 4 deletions

View File

@@ -5,14 +5,17 @@ const Joi = require('joi')
const BaseJsonService = require('../base-json')
const { floorCount: floorCountColor } = require('../../lib/color-formatters')
const { ordinalNumber } = require('../../lib/text-formatters')
const { nonNegativeInteger } = require('../validators')
const { InvalidResponse } = require('../errors')
const keywords = ['ruby']
const totalSchema = Joi.array()
.items(
Joi.object({
total_ranking: nonNegativeInteger,
total_ranking: Joi.number()
.integer()
.min(0)
.allow(null),
})
)
.min(1)
@@ -20,7 +23,10 @@ const totalSchema = Joi.array()
const dailySchema = Joi.array()
.items(
Joi.object({
daily_ranking: nonNegativeInteger,
daily_ranking: Joi.number()
.integer()
.min(0)
.allow(null),
})
)
.min(1)
@@ -56,6 +62,9 @@ module.exports = class GemRank extends BaseJsonService {
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 })
}

View File

@@ -19,7 +19,7 @@ t.create('total rank (valid)')
)
t.create('daily rank (valid)')
.get('/rd/rspec-puppet-facts.json')
.get('/rd/rails.json')
.expectJSONTypes(
Joi.object().keys({
name: 'rank',
@@ -30,3 +30,17 @@ t.create('daily rank (valid)')
t.create('rank (not found)')
.get('/rt/not-a-package.json')
.expectJSON({ name: 'rank', value: 'not found' })
t.create('rank is null')
.get('/rd/rails.json')
.intercept(nock =>
nock('http://bestgems.org')
.get('/api/v1/gems/rails/daily_ranking.json')
.reply(200, [
{
date: '2019-01-06',
daily_ranking: null,
},
])
)
.expectJSON({ name: 'rank', value: 'invalid rank' })