feat: deprecate leanpub badges (#4479)

This commit is contained in:
Caleb Cartwright
2019-12-30 10:07:36 -06:00
committed by GitHub
parent e908a31f5e
commit e01712da76
2 changed files with 23 additions and 134 deletions

View File

@@ -1,79 +1,13 @@
'use strict'
const Joi = require('@hapi/joi')
const { BaseJsonService } = require('..')
const { deprecatedService } = require('..')
const bookSummarySchema = Joi.object({
id: Joi.number().required(),
page_count_published: Joi.number().required(),
total_copies_sold: Joi.number().required(),
}).required()
module.exports = class LeanpubBookSummaryService extends BaseJsonService {
static get category() {
return 'funding'
}
static get route() {
return {
base: 'leanpub/book',
pattern: ':metric(pages|sold)/:book',
}
}
static get examples() {
return [
{
title: 'Leanpub Book Page Count',
pattern: 'pages/:book',
namedParams: {
book: 'juice-shop',
},
staticPreview: this.render({ label: 'pages', message: 226 }),
},
{
title: 'Leanpub Book Total Copies Sold',
pattern: 'sold/:book',
namedParams: {
book: 'juice-shop',
},
staticPreview: this.render({ label: 'sold', message: 2691 }),
},
]
}
static get defaultBadgeData() {
return { color: 'blue', label: 'leanpub' }
}
static render({ label, message }) {
return {
label,
message,
}
}
async handle({ metric, book }) {
// LeanPub API Docs https://leanpub.com/help/api#getting-book-info
const url = `https://leanpub.com/${book}.json`
const options = {}
const errorMessages = {
404: 'book not found',
}
const json = await this._requestJson({
schema: bookSummarySchema,
url,
options,
errorMessages,
})
let value
if (metric === 'pages') {
value = json.page_count_published
} else {
value = json.total_copies_sold
}
return this.constructor.render({ label: metric, message: value })
}
}
module.exports = deprecatedService({
route: {
base: 'leanpub/book',
pattern: ':various+',
},
category: 'funding',
label: 'leanpub',
dateAdded: new Date('2019-12-30'),
})

View File

@@ -1,68 +1,23 @@
'use strict'
const Joi = require('@hapi/joi')
const t = (module.exports = require('../tester').createServiceTester())
const { ServiceTester } = require('../tester')
const knownValidBook = 'juice-shop'
const t = (module.exports = new ServiceTester({
id: 'LeanPub',
title: 'LeanPub',
pathPrefix: '/leanpub/book',
}))
t.create('known book pages')
.get(`/pages/${knownValidBook}.json`)
.expectBadge({
label: 'pages',
message: Joi.number(),
})
t.create('known book sold')
.get(`/sold/${knownValidBook}.json`)
.expectBadge({
label: 'sold',
message: Joi.number(),
})
t.create('unknown book')
.get(`/pages/234uFjAsDf234209.json`)
.expectBadge({ label: 'leanpub', message: 'book not found' })
t.create('404 book summary error response')
.get(`/pages/${knownValidBook}.json`)
.intercept(nock =>
nock('https://leanpub.com/')
.get(`/${knownValidBook}.json`)
.reply(404)
)
t.create('no longer available (previously book pages)')
.get('/pages/juice-shop.json')
.expectBadge({
label: 'leanpub',
message: 'book not found',
message: 'no longer available',
})
t.create('correct page count')
.get(`/pages/${knownValidBook}.json`)
.intercept(nock =>
nock('https://leanpub.com/')
.get(`/${knownValidBook}.json`)
.reply(200, {
id: 12,
page_count_published: 190,
total_copies_sold: 27,
})
)
t.create('no longer available (previously books sold)')
.get('/sold/juice-shop.json')
.expectBadge({
label: 'pages',
message: '190',
})
t.create('correct sold count')
.get(`/sold/${knownValidBook}.json`)
.intercept(nock =>
nock('https://leanpub.com/')
.get(`/${knownValidBook}.json`)
.reply(200, {
id: 7,
page_count_published: 351,
total_copies_sold: 82347,
})
)
.expectBadge({
label: 'sold',
message: '82347',
label: 'leanpub',
message: 'no longer available',
})