refactor [twitter] service (#3241)
* refactor [twitter] service * pass screen_names in options.qs
This commit is contained in:
committed by
Caleb Cartwright
parent
b03b644451
commit
a12ece3d7c
@@ -1,17 +1,10 @@
|
||||
'use strict'
|
||||
|
||||
const LegacyService = require('../legacy-service')
|
||||
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
|
||||
const { makeLogo: getLogo } = require('../../lib/logos')
|
||||
const Joi = require('joi')
|
||||
const { BaseService, BaseJsonService, NotFound } = require('..')
|
||||
const { metric } = require('../text-formatters')
|
||||
|
||||
// This legacy service should be rewritten to use e.g. BaseJsonService.
|
||||
//
|
||||
// Tips for rewriting:
|
||||
// https://github.com/badges/shields/blob/master/doc/rewriting-services.md
|
||||
//
|
||||
// Do not base new services on this code.
|
||||
class TwitterUrl extends LegacyService {
|
||||
class TwitterUrl extends BaseService {
|
||||
static get category() {
|
||||
return 'social'
|
||||
}
|
||||
@@ -27,12 +20,12 @@ class TwitterUrl extends LegacyService {
|
||||
return [
|
||||
{
|
||||
title: 'Twitter URL',
|
||||
pattern: ':protocol(https|http)/:hostAndPath',
|
||||
namedParams: {
|
||||
protocol: 'http',
|
||||
hostAndPath: 'shields.io',
|
||||
},
|
||||
queryParams: { style: 'social' },
|
||||
// hard code the static preview
|
||||
// because link[] is not allowed in examples
|
||||
staticPreview: {
|
||||
label: 'Tweet',
|
||||
message: '',
|
||||
@@ -48,40 +41,23 @@ class TwitterUrl extends LegacyService {
|
||||
}
|
||||
}
|
||||
|
||||
static registerLegacyRouteHandler({ camp, cache }) {
|
||||
camp.route(
|
||||
/^\/twitter\/url\/([^/]+)\/(.+)\.(svg|png|gif|jpg|json)$/,
|
||||
cache((data, match, sendBadge, request) => {
|
||||
const scheme = match[1] // eg, https
|
||||
const path = match[2] // eg, shields.io
|
||||
const format = match[3]
|
||||
const page = encodeURIComponent(`${scheme}://${path}`)
|
||||
// The URL API died: #568.
|
||||
//var url = 'http://cdn.api.twitter.com/1/urls/count.json?url=' + page;
|
||||
const badgeData = getBadgeData('tweet', data)
|
||||
if (badgeData.template === 'social') {
|
||||
badgeData.logo = getLogo('twitter', data)
|
||||
badgeData.links = [
|
||||
`https://twitter.com/intent/tweet?text=Wow:&url=${page}`,
|
||||
`https://twitter.com/search?q=${page}`,
|
||||
]
|
||||
}
|
||||
badgeData.text[1] = ''
|
||||
badgeData.colorscheme = undefined
|
||||
badgeData.colorB = data.colorB || '#55ACEE'
|
||||
sendBadge(format, badgeData)
|
||||
})
|
||||
)
|
||||
async handle({ protocol, hostAndPath }) {
|
||||
const page = encodeURIComponent(`${protocol}://${hostAndPath}`)
|
||||
return {
|
||||
label: 'tweet',
|
||||
message: '',
|
||||
style: 'social',
|
||||
link: [
|
||||
`https://twitter.com/intent/tweet?text=Wow:&url=${page}`,
|
||||
`https://twitter.com/search?q=${page}`,
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This legacy service should be rewritten to use e.g. BaseJsonService.
|
||||
//
|
||||
// Tips for rewriting:
|
||||
// https://github.com/badges/shields/blob/master/doc/rewriting-services.md
|
||||
//
|
||||
// Do not base new services on this code.
|
||||
class TwitterFollow extends LegacyService {
|
||||
const schema = Joi.any()
|
||||
|
||||
class TwitterFollow extends BaseJsonService {
|
||||
static get category() {
|
||||
return 'social'
|
||||
}
|
||||
@@ -101,6 +77,8 @@ class TwitterFollow extends LegacyService {
|
||||
user: 'espadrine',
|
||||
},
|
||||
queryParams: { label: 'Follow' },
|
||||
// hard code the static preview
|
||||
// because link[] is not allowed in examples
|
||||
staticPreview: {
|
||||
label: 'Follow',
|
||||
message: '393',
|
||||
@@ -116,49 +94,32 @@ class TwitterFollow extends LegacyService {
|
||||
}
|
||||
}
|
||||
|
||||
static registerLegacyRouteHandler({ camp, cache }) {
|
||||
camp.route(
|
||||
/^\/twitter\/follow\/@?([^/]+)\.(svg|png|gif|jpg|json)$/,
|
||||
cache((data, match, sendBadge, request) => {
|
||||
const user = match[1] // eg, shields_io
|
||||
const format = match[2]
|
||||
const options = {
|
||||
url: `http://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=${user}`,
|
||||
}
|
||||
const badgeData = getBadgeData(`follow @${user}`, data)
|
||||
async fetch({ user }) {
|
||||
return this._requestJson({
|
||||
schema,
|
||||
url: `http://cdn.syndication.twimg.com/widgets/followbutton/info.json`,
|
||||
options: { qs: { screen_names: user } },
|
||||
})
|
||||
}
|
||||
|
||||
badgeData.colorscheme = undefined
|
||||
badgeData.colorB = '#55ACEE'
|
||||
if (badgeData.template === 'social') {
|
||||
badgeData.logo = getLogo('twitter', data)
|
||||
}
|
||||
badgeData.links = [
|
||||
`https://twitter.com/intent/follow?screen_name=${user}`,
|
||||
`https://twitter.com/${user}/followers`,
|
||||
]
|
||||
badgeData.text[1] = ''
|
||||
request(options, (err, res, buffer) => {
|
||||
if (err != null) {
|
||||
badgeData.text[1] = 'inaccessible'
|
||||
sendBadge(format, badgeData)
|
||||
return
|
||||
}
|
||||
try {
|
||||
// The data is formatted as an array.
|
||||
const data = JSON.parse(buffer)[0]
|
||||
if (data === undefined) {
|
||||
badgeData.text[1] = 'invalid user'
|
||||
} else if (data.followers_count != null) {
|
||||
// data.followers_count could be zero… don't just check if falsey.
|
||||
badgeData.text[1] = metric(data.followers_count)
|
||||
}
|
||||
} catch (e) {
|
||||
badgeData.text[1] = 'invalid'
|
||||
}
|
||||
sendBadge(format, badgeData)
|
||||
})
|
||||
})
|
||||
)
|
||||
static render({ user, followers }) {
|
||||
return {
|
||||
label: `follow @${user}`,
|
||||
message: metric(followers),
|
||||
style: 'social',
|
||||
link: [
|
||||
`https://twitter.com/intent/follow?screen_name=${user}`,
|
||||
`https://twitter.com/${user}/followers`,
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
async handle({ user }) {
|
||||
const data = await this.fetch({ user })
|
||||
if (data.length === 0) {
|
||||
throw new NotFound({ prettyMessage: 'invalid user' })
|
||||
}
|
||||
return this.constructor.render({ user, followers: data[0].followers_count })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,13 +15,6 @@ t.create('Followers')
|
||||
message: isMetric,
|
||||
})
|
||||
|
||||
t.create('Followers - Custom Label')
|
||||
.get('/follow/shields_io.json?label=Follow')
|
||||
.expectBadge({
|
||||
label: 'Follow',
|
||||
message: isMetric,
|
||||
})
|
||||
|
||||
t.create('Invalid Username Specified')
|
||||
.get('/follow/invalidusernamethatshouldnotexist.json?label=Follow')
|
||||
.expectBadge({
|
||||
@@ -29,11 +22,6 @@ t.create('Invalid Username Specified')
|
||||
message: 'invalid user',
|
||||
})
|
||||
|
||||
t.create('No connection')
|
||||
.get('/follow/shields_io.json?label=Follow')
|
||||
.networkOff()
|
||||
.expectBadge({ label: 'Follow', message: 'inaccessible' })
|
||||
|
||||
t.create('URL')
|
||||
.get('/url/https/shields.io.json')
|
||||
.expectBadge({ label: 'tweet', message: '' })
|
||||
|
||||
Reference in New Issue
Block a user