Files
shields/services/wheelmap/wheelmap.service.js
Paul Melnikow 226fa67a02 Create shortcut for BaseService-related imports (#2809)
Continue to implement #2698:

- Add `core/base-service/index.js` (but hold off on moving the things it imports)
- Add shortcuts in `services/index.js` for Base*Service, errors, and deprecatedService. This file will be streamlined later to avoid cluttering it with rarely used bits.
- Apply consistent ordering of imports and use of `module.exports` in testers.
- Remove some renaming of imports.
- Remove obsolete tests here and there.
2019-01-21 15:41:24 -05:00

78 lines
1.6 KiB
JavaScript

'use strict'
const Joi = require('joi')
const serverSecrets = require('../../lib/server-secrets')
const { BaseJsonService } = require('..')
const wheelmapSchema = Joi.object({
node: Joi.object({
wheelchair: Joi.string().required(),
}).required(),
}).required()
module.exports = class Wheelmap extends BaseJsonService {
async fetch({ nodeId }) {
let options
if (serverSecrets.wheelmap_token) {
options = {
qs: {
api_key: `${serverSecrets.wheelmap_token}`,
},
}
}
return this._requestJson({
schema: wheelmapSchema,
url: `https://wheelmap.org/api/nodes/${nodeId}`,
options,
errorMessages: {
401: 'invalid token',
404: 'node not found',
},
})
}
static render({ accessibility }) {
let color
if (accessibility === 'yes') {
color = 'brightgreen'
} else if (accessibility === 'limited') {
color = 'yellow'
} else if (accessibility === 'no') {
color = 'red'
}
return { message: accessibility, color }
}
async handle({ nodeId }) {
const json = await this.fetch({ nodeId })
const accessibility = json.node.wheelchair
return this.constructor.render({ accessibility })
}
static get defaultBadgeData() {
return { label: 'accessibility' }
}
static get category() {
return 'other'
}
static get route() {
return {
base: 'wheelmap/a',
pattern: ':nodeId(-?[0-9]+)',
}
}
static get examples() {
return [
{
title: 'Wheelmap',
namedParams: { nodeId: '26699541' },
staticPreview: this.render({ accessibility: 'yes' }),
},
]
}
}