Added [bStats] badges (#2591)
* bStats badges * Remove inline tutorial comments * Split tests into seperate files * use shorthand for tester instantiation * use Joi.number() for validation * Misc. fixes * update to use native api
This commit is contained in:
committed by
Caleb Cartwright
parent
cd0ff105f6
commit
a8629fe8bd
70
services/bstats/bstats-players.service.js
Normal file
70
services/bstats/bstats-players.service.js
Normal file
@@ -0,0 +1,70 @@
|
||||
'use strict'
|
||||
|
||||
const BaseJsonService = require('../base-json')
|
||||
|
||||
const Joi = require('joi')
|
||||
const schema = Joi.array()
|
||||
.items(Joi.array().items([Joi.number().required(), Joi.number().required()]))
|
||||
.required()
|
||||
|
||||
module.exports = class BStatsPlayers extends BaseJsonService {
|
||||
static get route() {
|
||||
return {
|
||||
base: 'bstats/players',
|
||||
pattern: ':pluginid',
|
||||
}
|
||||
}
|
||||
|
||||
static get defaultBadgeData() {
|
||||
return {
|
||||
label: 'players',
|
||||
color: 'blue',
|
||||
}
|
||||
}
|
||||
|
||||
async handle({ pluginid }) {
|
||||
const json = await this.fetch({ pluginid })
|
||||
const { players } = this.transform({ json })
|
||||
return this.constructor.render({ players })
|
||||
}
|
||||
|
||||
transform({ json }) {
|
||||
const players = json[0][1]
|
||||
return { players }
|
||||
}
|
||||
|
||||
async fetch({ pluginid }) {
|
||||
const url = `https://bstats.org/api/v1/plugins/${pluginid}/charts/players/data`
|
||||
|
||||
return this._requestJson({
|
||||
schema,
|
||||
options: {
|
||||
qs: {
|
||||
maxElements: 1,
|
||||
},
|
||||
},
|
||||
url,
|
||||
})
|
||||
}
|
||||
|
||||
static render({ players }) {
|
||||
return {
|
||||
message: players,
|
||||
}
|
||||
}
|
||||
|
||||
static get category() {
|
||||
return 'other'
|
||||
}
|
||||
static get examples() {
|
||||
return [
|
||||
{
|
||||
title: 'bStats Players',
|
||||
namedParams: {
|
||||
pluginid: '1',
|
||||
},
|
||||
staticPreview: this.render({ players: 74299 }),
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
14
services/bstats/bstats-players.tester.js
Normal file
14
services/bstats/bstats-players.tester.js
Normal file
@@ -0,0 +1,14 @@
|
||||
'use strict'
|
||||
|
||||
const Joi = require('joi')
|
||||
|
||||
const t = (module.exports = require('../create-service-tester')())
|
||||
|
||||
t.create('Players')
|
||||
.get('/1.json')
|
||||
.expectJSONTypes(
|
||||
Joi.object().keys({
|
||||
name: 'players',
|
||||
value: Joi.number(),
|
||||
})
|
||||
)
|
||||
70
services/bstats/bstats-servers.service.js
Normal file
70
services/bstats/bstats-servers.service.js
Normal file
@@ -0,0 +1,70 @@
|
||||
'use strict'
|
||||
|
||||
const BaseJsonService = require('../base-json')
|
||||
|
||||
const Joi = require('joi')
|
||||
const schema = Joi.array()
|
||||
.items(Joi.array().items([Joi.number().required(), Joi.number().required()]))
|
||||
.required()
|
||||
|
||||
module.exports = class BStatsServers extends BaseJsonService {
|
||||
static get route() {
|
||||
return {
|
||||
base: 'bstats/servers',
|
||||
pattern: ':pluginid',
|
||||
}
|
||||
}
|
||||
|
||||
static get defaultBadgeData() {
|
||||
return {
|
||||
label: 'servers',
|
||||
color: 'blue',
|
||||
}
|
||||
}
|
||||
|
||||
async handle({ pluginid }) {
|
||||
const json = await this.fetch({ pluginid })
|
||||
const { servers } = this.transform({ json })
|
||||
return this.constructor.render({ servers })
|
||||
}
|
||||
|
||||
transform({ json }) {
|
||||
const servers = json[0][1]
|
||||
return { servers }
|
||||
}
|
||||
|
||||
async fetch({ pluginid }) {
|
||||
const url = `https://bstats.org/api/v1/plugins/${pluginid}/charts/servers/data`
|
||||
|
||||
return this._requestJson({
|
||||
schema,
|
||||
options: {
|
||||
qs: {
|
||||
maxElements: 1,
|
||||
},
|
||||
},
|
||||
url,
|
||||
})
|
||||
}
|
||||
|
||||
static render({ servers }) {
|
||||
return {
|
||||
message: servers,
|
||||
}
|
||||
}
|
||||
|
||||
static get category() {
|
||||
return 'other'
|
||||
}
|
||||
static get examples() {
|
||||
return [
|
||||
{
|
||||
title: 'bStats Servers',
|
||||
namedParams: {
|
||||
pluginid: '1',
|
||||
},
|
||||
staticPreview: this.render({ servers: 57479 }),
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
14
services/bstats/bstats-servers.tester.js
Normal file
14
services/bstats/bstats-servers.tester.js
Normal file
@@ -0,0 +1,14 @@
|
||||
'use strict'
|
||||
|
||||
const Joi = require('joi')
|
||||
|
||||
const t = (module.exports = require('../create-service-tester')())
|
||||
|
||||
t.create('Servers')
|
||||
.get('/1.json')
|
||||
.expectJSONTypes(
|
||||
Joi.object().keys({
|
||||
name: 'servers',
|
||||
value: Joi.number(),
|
||||
})
|
||||
)
|
||||
Reference in New Issue
Block a user