-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[gem cdnjs appveyor clojars] refactor clojars, establish BaseJsonService #1702
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
const { BaseJsonService } = require('../base'); | ||
const { NotFound } = require('../errors'); | ||
const { version: versionColor} = require('../../lib/color-formatters'); | ||
|
||
module.exports = class Clojars extends BaseJsonService { | ||
async handle({clojar}) { | ||
const apiUrl = 'https://clojars.org/' + clojar + '/latest-version.json'; | ||
const json = await this._requestJson(apiUrl); | ||
|
||
if (Object.keys(json).length === 0) { | ||
/* Note the 'not found' response from clojars is: | ||
status code = 200, body = {} */ | ||
throw new NotFound(); | ||
} | ||
|
||
return { | ||
message: "[" + clojar + " \"" + json.version + "\"]", | ||
color: versionColor(json.version) | ||
}; | ||
} | ||
|
||
// Metadata | ||
static get defaultBadgeData() { | ||
return { label: 'clojars' }; | ||
} | ||
|
||
static get category() { | ||
return 'version'; | ||
} | ||
|
||
static get url() { | ||
return { | ||
base: 'clojars/v', | ||
format: '(.*)', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old implementation used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Good spot. I've also changed it in the other services I have touched in this PR as there is no case for empty string there either. I'll try to remember this as I refactor more services. |
||
capture: ['clojar'] | ||
}; | ||
} | ||
|
||
static get examples() { | ||
return [ | ||
{ previewUrl: 'prismic' } | ||
]; | ||
} | ||
|
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,7 @@ | |
|
||
const semver = require('semver'); | ||
|
||
const BaseService = require('../base'); | ||
const { | ||
checkErrorResponse, | ||
asJson, | ||
} = require('../../lib/error-helper'); | ||
const { BaseJsonService } = require('../base'); | ||
const { InvalidResponse } = require('../errors'); | ||
const { addv: versionText } = require('../../lib/text-formatters'); | ||
const { version: versionColor} = require('../../lib/color-formatters'); | ||
|
@@ -21,13 +17,10 @@ const { | |
const { latest: latestVersion } = require('../../lib/version'); | ||
|
||
|
||
class GemVersion extends BaseService { | ||
class GemVersion extends BaseJsonService { | ||
async handle({repo}) { | ||
const apiUrl = 'https://rubygems.org/api/v1/gems/' + repo + '.json'; | ||
const json = await this._sendAndCacheRequest(apiUrl, { | ||
headers: { 'Accept': 'application/json' } | ||
}).then(checkErrorResponse.asPromise()) | ||
.then(asJson); | ||
const json = await this._requestJson(apiUrl); | ||
const version = json.version; | ||
|
||
return { | ||
|
@@ -66,7 +59,7 @@ class GemVersion extends BaseService { | |
} | ||
}; | ||
|
||
class GemDownloads extends BaseService { | ||
class GemDownloads extends BaseJsonService { | ||
|
||
_getApiUrl(repo, info) { | ||
const endpoint = info === "dv" ? 'versions/' : 'gems/'; | ||
|
@@ -94,10 +87,7 @@ class GemDownloads extends BaseService { | |
version = (version === "stable") ? version : semver.valid(version); | ||
const label = this._getLabel(version, info); | ||
const apiUrl = this._getApiUrl(repo, info); | ||
const json = await this._sendAndCacheRequest(apiUrl, { | ||
headers: { 'Accept': 'application/atom+json,application/json' } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This particular line had a slightly different There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was added in #212 but there is no explanation there. It works fine with the standard |
||
}).then(checkErrorResponse.asPromise()) | ||
.then(asJson); | ||
const json = await this._requestJson(apiUrl); | ||
|
||
let downloads; | ||
if (info === "dt") { | ||
|
@@ -194,14 +184,11 @@ class GemDownloads extends BaseService { | |
} | ||
}; | ||
|
||
class GemOwner extends BaseService { | ||
class GemOwner extends BaseJsonService { | ||
|
||
async handle({user}) { | ||
const apiUrl = 'https://rubygems.org/api/v1/owners/' + user + '/gems.json'; | ||
const json = await this._sendAndCacheRequest(apiUrl, { | ||
headers: { 'Accept': 'application/json' } | ||
}).then(checkErrorResponse.asPromise()) | ||
.then(asJson); | ||
const json = await this._requestJson(apiUrl); | ||
const count = json.length; | ||
|
||
return { | ||
|
@@ -240,7 +227,7 @@ class GemOwner extends BaseService { | |
} | ||
}; | ||
|
||
class GemRank extends BaseService { | ||
class GemRank extends BaseJsonService { | ||
|
||
_getApiUrl(repo, totalRank, dailyRank) { | ||
let endpoint; | ||
|
@@ -256,10 +243,7 @@ class GemRank extends BaseService { | |
const totalRank = (info === 'rt'); | ||
const dailyRank = (info === 'rd'); | ||
const apiUrl = this._getApiUrl(repo, totalRank, dailyRank); | ||
const json = await this._sendAndCacheRequest(apiUrl, { | ||
headers: { 'Accept': 'application/json' } | ||
}).then(checkErrorResponse.asPromise()) | ||
.then(asJson); | ||
const json = await this._requestJson(apiUrl); | ||
|
||
let rank; | ||
if (totalRank) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Being picky: inconsistent spacing around
versionColor
^^There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surprised we don't have an eslint rule for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. I was also surprised this isn't caught by a lint rule. Looks like we would need to configure ESLint's object-curly-spacing rule. If we want to add a convention for this, there's going to be a bunch of other code knocking about that doesn't conform to it so lets do it in another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not that familiar with ESLint, but I'm happy to take care of this and dig into it some more. 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, after taking a closer look into this, we do have a rule enabled in Prettier. Nevertheless, it's currently awaiting to be enabled as part of #1167.