Files
shields/services/reddit/subreddit-subscribers.service.js
dependabot-preview[bot] 294aa1e1df Build(deps-dev): bump eslint-plugin-import from 2.17.3 to 2.18.0; autofixes (#3671)
* Build(deps-dev): bump eslint-plugin-import from 2.17.3 to 2.18.0

Bumps [eslint-plugin-import](https://github.com/benmosher/eslint-plugin-import) from 2.17.3 to 2.18.0.
- [Release notes](https://github.com/benmosher/eslint-plugin-import/releases)
- [Changelog](https://github.com/benmosher/eslint-plugin-import/blob/master/CHANGELOG.md)
- [Commits](https://github.com/benmosher/eslint-plugin-import/compare/v2.17.3...v2.18.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Autofixes
2019-07-08 12:13:46 -04:00

84 lines
1.8 KiB
JavaScript

'use strict'
const Joi = require('@hapi/joi')
const { optionalNonNegativeInteger } = require('../validators')
const { metric } = require('../text-formatters')
const { BaseJsonService, NotFound } = require('..')
const schema = Joi.object({
data: Joi.object({
subscribers: optionalNonNegativeInteger,
}).required(),
}).required()
module.exports = class SubredditSubscribers extends BaseJsonService {
static get category() {
return 'social'
}
static get route() {
return {
base: 'reddit/subreddit-subscribers',
pattern: ':subreddit',
}
}
static get examples() {
return [
{
title: 'Subreddit subscribers',
namedParams: { subreddit: 'drums' },
staticPreview: {
label: 'follow r/drums',
message: '77k',
color: 'red',
style: 'social',
},
},
]
}
static get defaultBadgeData() {
return {
label: 'reddit',
namedLogo: 'reddit',
}
}
static render({ subreddit, subscribers }) {
return {
label: `follow r/${subreddit}`,
message: metric(subscribers),
color: 'red',
link: [`https://www.reddit.com/r/${subreddit}`],
}
}
async fetch({ subreddit }) {
return this._requestJson({
schema,
url: `https://www.reddit.com/r/${subreddit}/about.json`,
errorMessages: {
404: 'subreddit not found',
},
})
}
transform(json) {
const subscribers = json.data.subscribers
if (subscribers === undefined) {
throw new NotFound({ prettyMessage: 'subreddit not found' })
}
return { subscribers }
}
async handle({ subreddit }) {
const json = await this.fetch({ subreddit })
const { subscribers } = this.transform(json)
return this.constructor.render({
subreddit,
subscribers,
})
}
}