From aed39bfde97fe4baa5f0f3066e67ad47fb8596a9 Mon Sep 17 00:00:00 2001
From: Emlyn Corrin <emlyn.corrin@gmail.com>
Date: Thu, 15 Nov 2018 18:32:22 +0000
Subject: [PATCH] [Clojars] Add downloads badge (#2305)

Add Clojars downloads badge
---
 services/clojars/clojars-downloads.service.js | 61 +++++++++++++++++++
 services/clojars/clojars-downloads.tester.js  | 21 +++++++
 2 files changed, 82 insertions(+)
 create mode 100644 services/clojars/clojars-downloads.service.js
 create mode 100644 services/clojars/clojars-downloads.tester.js

diff --git a/services/clojars/clojars-downloads.service.js b/services/clojars/clojars-downloads.service.js
new file mode 100644
index 0000000000..b48d4b7d93
--- /dev/null
+++ b/services/clojars/clojars-downloads.service.js
@@ -0,0 +1,61 @@
+'use strict'
+
+const Joi = require('joi')
+const BaseJsonService = require('../base-json')
+const { metric } = require('../../lib/text-formatters')
+const { nonNegativeInteger } = require('../validators')
+const { downloadCount: downloadsColor } = require('../../lib/color-formatters')
+
+const clojarsSchema = Joi.object({
+  downloads: nonNegativeInteger,
+}).required()
+
+module.exports = class Clojars extends BaseJsonService {
+  async fetch({ clojar }) {
+    const url = `https://clojars.org/api/artifacts/${clojar}`
+    return this._requestJson({
+      url,
+      schema: clojarsSchema,
+    })
+  }
+
+  static render({ downloads }) {
+    return {
+      label: 'downloads',
+      message: metric(downloads),
+      color: downloadsColor(downloads),
+    }
+  }
+
+  async handle({ clojar }) {
+    const json = await this.fetch({ clojar })
+    return this.constructor.render({ downloads: json.downloads })
+  }
+
+  // Metadata
+  static get defaultBadgeData() {
+    return { label: 'downloads' }
+  }
+
+  static get category() {
+    return 'downloads'
+  }
+
+  static get route() {
+    return {
+      base: 'clojars/dt',
+      format: '(.+)',
+      capture: ['clojar'],
+    }
+  }
+
+  static get examples() {
+    return [
+      {
+        exampleUrl: 'prismic',
+        urlPattern: ':package',
+        staticExample: this.render({ downloads: 117 }),
+      },
+    ]
+  }
+}
diff --git a/services/clojars/clojars-downloads.tester.js b/services/clojars/clojars-downloads.tester.js
new file mode 100644
index 0000000000..45e3338e68
--- /dev/null
+++ b/services/clojars/clojars-downloads.tester.js
@@ -0,0 +1,21 @@
+'use strict'
+
+const Joi = require('joi')
+const createServiceTester = require('../create-service-tester')
+const { isMetric } = require('../test-validators')
+
+const t = createServiceTester()
+module.exports = t
+
+t.create('clojars downloads (valid)')
+  .get('/prismic.json')
+  .expectJSONTypes(
+    Joi.object().keys({
+      name: 'downloads',
+      value: isMetric,
+    })
+  )
+
+t.create('clojars downloads (not found)')
+  .get('/not-a-package.json')
+  .expectJSON({ name: 'downloads', value: 'not found' })
-- 
GitLab