Add [Subreddit] subscribers (#3443)
* Add subreddit subscribers service file. * Fix small detail in service and add the tests files. * Apply suggested changes by the reviewer. * Trying to make tests work. * Update services/reddit/subreddit-subscribers.tester.js Co-Authored-By: Caleb Cartwright <calebcartwright@users.noreply.github.com> * Update services/reddit/subreddit-subscribers.tester.js Co-Authored-By: Caleb Cartwright <calebcartwright@users.noreply.github.com> * Apply suggested changes. * Add different handler for non existing and invalid subreddits. * Update services/reddit/subreddit-subscribers.service.js Co-Authored-By: Caleb Cartwright <calebcartwright@users.noreply.github.com>
This commit is contained in:
committed by
Caleb Cartwright
parent
02ee34d2a5
commit
d685cde52b
83
services/reddit/subreddit-subscribers.service.js
Normal file
83
services/reddit/subreddit-subscribers.service.js
Normal file
@@ -0,0 +1,83 @@
|
||||
'use strict'
|
||||
|
||||
const { BaseJsonService, NotFound } = require('..')
|
||||
const Joi = require('joi')
|
||||
const { optionalNonNegativeInteger } = require('../validators')
|
||||
const { metric } = require('../text-formatters')
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
25
services/reddit/subreddit-subscribers.tester.js
Normal file
25
services/reddit/subreddit-subscribers.tester.js
Normal file
@@ -0,0 +1,25 @@
|
||||
'use strict'
|
||||
|
||||
const { isMetric } = require('../test-validators')
|
||||
const t = (module.exports = require('../tester').createServiceTester())
|
||||
|
||||
t.create('subreddit-subscribers (valid subreddit)')
|
||||
.get('/drums.json')
|
||||
.expectBadge({
|
||||
label: 'follow r/drums',
|
||||
message: isMetric,
|
||||
})
|
||||
|
||||
t.create('subreddit-subscribers (invalid subreddit)')
|
||||
.get('/badfbasdfadfadfadfadfasdf.json')
|
||||
.expectBadge({
|
||||
label: 'reddit',
|
||||
message: 'subreddit not found',
|
||||
})
|
||||
|
||||
t.create('subreddit-subscribers (not existing subreddit)')
|
||||
.get('/not-a-real-rubreddit.json')
|
||||
.expectBadge({
|
||||
label: 'reddit',
|
||||
message: 'subreddit not found',
|
||||
})
|
||||
Reference in New Issue
Block a user