Skip to content

Commit

Permalink
refactor [vaadindirectory] badges (#3339)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s authored Apr 21, 2019
1 parent b55e3ee commit 05af1f8
Show file tree
Hide file tree
Showing 13 changed files with 385 additions and 378 deletions.
34 changes: 34 additions & 0 deletions services/vaadin-directory/vaadin-directory-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const Joi = require('joi')
const { BaseJsonService } = require('..')
const { nonNegativeInteger } = require('../validators')

const schema = Joi.object({
ratingCount: nonNegativeInteger,
averageRating: Joi.number()
.min(0)
.required(),
latestAvailableRelease: Joi.object({
publicationDate: Joi.date().required(),
name: Joi.string().required(),
}).required(),
status: Joi.string().required(),
}).required()

class BaseVaadinDirectoryService extends BaseJsonService {
async fetch({ packageName }) {
return this._requestJson({
schema,
url: `https://vaadin.com/vaadincom/directory-service/components/search/findByUrlIdentifier`,
options: {
qs: {
projection: 'summary',
urlIdentifier: packageName,
},
},
})
}
}

module.exports = { BaseVaadinDirectoryService }
46 changes: 46 additions & 0 deletions services/vaadin-directory/vaadin-directory-rating-count.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict'

const { metric } = require('../text-formatters')
const { floorCount: floorCountColor } = require('../color-formatters')
const { BaseVaadinDirectoryService } = require('./vaadin-directory-base')

module.exports = class VaadinDirectoryRatingCount extends BaseVaadinDirectoryService {
static get category() {
return 'rating'
}

static get route() {
return {
base: 'vaadin-directory',
pattern: ':which(rc|rating-count)/:packageName',
}
}

static get examples() {
return [
{
title: 'Vaadin Directory',
pattern: 'rating-count/:packageName',
namedParams: { packageName: 'vaadinvaadin-grid' },
staticPreview: this.render({ ratingCount: 6 }),
keywords: ['vaadin-directory', 'rating-count', 'rating count'],
},
]
}

static get defaultBadgeData() {
return { label: 'rating count' }
}

static render({ ratingCount }) {
return {
message: `${metric(ratingCount)} total`,
color: floorCountColor(ratingCount, 5, 50, 500),
}
}

async handle({ which, packageName }) {
const { ratingCount } = await this.fetch({ packageName })
return this.constructor.render({ ratingCount })
}
}
25 changes: 25 additions & 0 deletions services/vaadin-directory/vaadin-directory-rating-count.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

const Joi = require('joi')
const t = (module.exports = require('../tester').createServiceTester())

t.create('rating count of component')
.get('/rating-count/vaadinvaadin-grid.json')
.expectBadge({
label: 'rating count',
message: Joi.string().regex(/^\d+?\stotal$/),
})

t.create('rating count of component')
.get('/rc/vaadinvaadin-grid.json')
.expectBadge({
label: 'rating count',
message: Joi.string().regex(/^\d+?\stotal$/),
})

t.create('not found')
.get('/rating-count/does-not-exist.json')
.expectBadge({
label: 'rating count',
message: 'not found',
})
55 changes: 55 additions & 0 deletions services/vaadin-directory/vaadin-directory-rating.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'

const { starRating } = require('../text-formatters')
const { floorCount: floorCountColor } = require('../color-formatters')
const { BaseVaadinDirectoryService } = require('./vaadin-directory-base')

module.exports = class VaadinDirectoryRating extends BaseVaadinDirectoryService {
static get category() {
return 'rating'
}

static get route() {
return {
base: 'vaadin-directory',
pattern: ':which(star|stars|rating)/:packageName',
}
}

static get examples() {
return [
{
title: 'Vaadin Directory',
pattern: ':which(stars|rating)/:packageName',
namedParams: { which: 'rating', packageName: 'vaadinvaadin-grid' },
staticPreview: this.render({ which: 'rating', score: 4.75 }),
keywords: ['vaadin-directory', 'rating'],
},
]
}

static get defaultBadgeData() {
return { label: 'rating' }
}

static render({ which, score }) {
const rating = (Math.round(score * 10) / 10).toFixed(1)
if (which === 'rating') {
return {
label: 'rating',
message: `${rating}/5`,
color: floorCountColor(rating, 2, 3, 4),
}
}
return {
label: 'stars',
message: starRating(rating),
color: floorCountColor(rating, 2, 3, 4),
}
}

async handle({ which, packageName }) {
const { averageRating } = await this.fetch({ packageName })
return this.constructor.render({ which, score: averageRating })
}
}
33 changes: 33 additions & 0 deletions services/vaadin-directory/vaadin-directory-rating.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const Joi = require('joi')
const { isStarRating } = require('../test-validators')
const t = (module.exports = require('../tester').createServiceTester())

t.create('stars of component displayed in star icons')
.get('/star/vaadinvaadin-grid.json')
.expectBadge({
label: 'stars',
message: isStarRating,
})

t.create('stars of component displayed in star icons')
.get('/stars/vaadinvaadin-grid.json')
.expectBadge({
label: 'stars',
message: isStarRating,
})

t.create('rating of the component (eg: 4.2/5)')
.get('/rating/vaadinvaadin-grid.json')
.expectBadge({
label: 'rating',
message: Joi.string().regex(/^\d\.\d\/5$/),
})

t.create('not found')
.get('/rating/does-not-exist.json')
.expectBadge({
label: 'rating',
message: 'not found',
})
45 changes: 45 additions & 0 deletions services/vaadin-directory/vaadin-directory-release-date.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict'

const { formatDate } = require('../text-formatters')
const { age: ageColor } = require('../color-formatters')
const { BaseVaadinDirectoryService } = require('./vaadin-directory-base')

module.exports = class VaadinDirectoryReleaseDate extends BaseVaadinDirectoryService {
static get category() {
return 'activity'
}

static get route() {
return {
base: 'vaadin-directory',
pattern: ':which(rd|release-date)/:packageName',
}
}

static get examples() {
return [
{
title: 'Vaadin Directory',
pattern: 'release-date/:packageName',
namedParams: { packageName: 'vaadinvaadin-grid' },
staticPreview: this.render({ date: '2018-12-12' }),
keywords: ['vaadin-directory', 'date', 'latest release date'],
},
]
}

static get defaultBadgeData() {
return { label: 'latest release date' }
}

static render({ date }) {
return { message: formatDate(date), color: ageColor(date) }
}

async handle({ which, packageName }) {
const data = await this.fetch({ packageName })
return this.constructor.render({
date: data.latestAvailableRelease.publicationDate,
})
}
}
22 changes: 22 additions & 0 deletions services/vaadin-directory/vaadin-directory-release-date.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const { isFormattedDate } = require('../test-validators')
const t = (module.exports = require('../tester').createServiceTester())

t.create('latest release date of the component (format: yyyy-mm-dd)')
.get('/rd/vaadinvaadin-grid.json')
.expectBadge({
label: 'latest release date',
message: isFormattedDate,
})

t.create('latest release date of the component (format: yyyy-mm-dd)')
.get('/release-date/vaadinvaadin-grid.json')
.expectBadge({
label: 'latest release date',
message: isFormattedDate,
})

t.create('not found')
.get('/release-date/does-not-exist.json')
.expectBadge({ label: 'latest release date', message: 'not found' })
43 changes: 43 additions & 0 deletions services/vaadin-directory/vaadin-directory-status.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

const { BaseVaadinDirectoryService } = require('./vaadin-directory-base')

module.exports = class VaadinDirectoryStatus extends BaseVaadinDirectoryService {
static get category() {
return 'other'
}

static get route() {
return {
base: 'vaadin-directory/status',
pattern: ':packageName',
}
}

static get examples() {
return [
{
title: 'Vaadin Directory',
namedParams: { packageName: 'vaadinvaadin-grid' },
staticPreview: this.render({ status: 'published' }),
keywords: ['vaadin-directory', 'status'],
},
]
}

static get defaultBadgeData() {
return { label: 'vaadin directory' }
}

static render({ status }) {
if (status.toLowerCase() === 'published') {
return { message: 'published', color: '#00b4f0' }
}
return { message: 'unpublished' }
}

async handle({ packageName }) {
const { status } = await this.fetch({ packageName })
return this.constructor.render({ status })
}
}
18 changes: 18 additions & 0 deletions services/vaadin-directory/vaadin-directory-status.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const Joi = require('joi')
const t = (module.exports = require('../tester').createServiceTester())

t.create('publish status of the component')
.get('/vaadinvaadin-grid.json')
.expectBadge({
label: 'vaadin directory',
message: Joi.equal('published', 'unpublished'),
})

t.create('not found')
.get('/does-not-exist.json')
.expectBadge({
label: 'vaadin directory',
message: 'not found',
})
39 changes: 39 additions & 0 deletions services/vaadin-directory/vaadin-directory-version.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

const { renderVersionBadge } = require('../version')
const { BaseVaadinDirectoryService } = require('./vaadin-directory-base')

module.exports = class VaadinDirectoryVersion extends BaseVaadinDirectoryService {
static get category() {
return 'version'
}

static get route() {
return {
base: 'vaadin-directory',
pattern: ':which(v|version)/:packageName',
}
}

static get examples() {
return [
{
title: 'Vaadin Directory',
pattern: 'v/:packageName',
namedParams: { packageName: 'vaadinvaadin-grid' },
staticPreview: renderVersionBadge({ version: 'v5.3.0-alpha4' }),
keywords: ['vaadin-directory', 'version', 'latest version'],
},
]
}

static get defaultBadgeData() {
return { label: 'vaadin directory' }
}

async handle({ which, packageName }) {
const data = await this.fetch({ packageName })
const lv = data.latestAvailableRelease.name.toLowerCase()
return renderVersionBadge({ version: lv })
}
}
25 changes: 25 additions & 0 deletions services/vaadin-directory/vaadin-directory-version.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

const { isSemver } = require('../test-validators')
const t = (module.exports = require('../tester').createServiceTester())

t.create('latest version of the component (can have v prefixed or without)')
.get('/v/vaadinvaadin-grid.json')
.expectBadge({
label: 'vaadin directory',
message: isSemver,
})

t.create('latest version of the component (can have v prefixed or without)')
.get('/version/vaadinvaadin-grid.json')
.expectBadge({
label: 'vaadin directory',
message: isSemver,
})

t.create('not found')
.get('/v/does-not-exist.json')
.expectBadge({
label: 'vaadin directory',
message: 'not found',
})
Loading

0 comments on commit 05af1f8

Please sign in to comment.