Skip to content

Commit 89a7113

Browse files
committed
mirage/config: Implement "GET /summary" handler
1 parent 4dd83a6 commit 89a7113

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

mirage/config.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
import Response from 'ember-cli-mirage/response';
22

3-
import summaryFixture from '../mirage/fixtures/summary';
4-
53
export default function() {
6-
this.get('/summary', () => summaryFixture);
4+
this.get('/summary', function(schema) {
5+
let crates = schema.crates.all();
6+
7+
let just_updated = crates.sort((a, b) => compareIsoDates(b.updated_at, a.updated_at)).slice(0, 10);
8+
let most_downloaded = crates.sort((a, b) => b.downloads - a.downloads).slice(0, 10);
9+
let new_crates = crates.sort((a, b) => compareIsoDates(b.created_at, a.created_at)).slice(0, 10);
10+
11+
let num_crates = crates.length;
12+
let num_downloads = crates.models.reduce((sum, crate) => sum + crate.downloads, 0);
13+
14+
let popular_categories = schema.categories.all().sort((a, b) => b.crates_cnt - a.crates_cnt).slice(0, 10);
15+
let popular_keywords = schema.keywords.all().sort((a, b) => b.crates_cnt - a.crates_cnt).slice(0, 10);
16+
17+
return {
18+
just_updated: this.serialize(just_updated).crates
19+
.map(it => ({ ...it, versions: null })),
20+
most_downloaded: this.serialize(most_downloaded).crates
21+
.map(it => ({ ...it, versions: null })),
22+
new_crates: this.serialize(new_crates).crates
23+
.map(it => ({ ...it, versions: null })),
24+
num_crates,
25+
num_downloads,
26+
popular_categories: this.serialize(popular_categories).categories,
27+
popular_keywords: this.serialize(popular_keywords).keywords,
28+
};
29+
});
730

831
this.namespace = '/api/v1';
932

@@ -204,3 +227,9 @@ function withMeta(response, meta) {
204227
function compareStrings(a, b) {
205228
return (a < b) ? -1 : (a > b) ? 1 : 0;
206229
}
230+
231+
function compareIsoDates(a, b) {
232+
let aDate = new Date(a);
233+
let bDate = new Date(b);
234+
return (aDate < bDate) ? -1 : (aDate > bDate) ? 1 : 0;
235+
}

mirage/fixtures/crates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default [{
1717
"owner_team": "/api/v1/crates/nanomsg/owner_team",
1818
"reverse_dependencies": "/api/v1/crates/nanomsg/reverse_dependencies",
1919
"version_downloads": "/api/v1/crates/nanomsg/downloads",
20-
"versions": null
20+
"versions": "/api/v1/crates/nanomsg/versions",
2121
},
2222
"max_version": "0.7.0-alpha",
2323
"name": "nanomsg",

tests/acceptance/front-page-test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ test('visiting /', async function(assert) {
1414
findWithAssert('a[href="/crates"]');
1515
findWithAssert('a[href="/login"]');
1616

17-
hasText(assert, '.downloads .num', '13,534,453');
18-
hasText(assert, '.crates .num', '3,430');
17+
hasText(assert, '.downloads .num', '122,669');
18+
hasText(assert, '.crates .num', '19');
1919

2020
const $newCrate = findWithAssert('#new-crates ul > li:first a');
21-
hasText(assert, $newCrate, 'mkstemp (0.2.0)');
22-
assert.equal($newCrate.attr('href').trim(), '/crates/mkstemp');
21+
hasText(assert, $newCrate, 'Inflector (0.1.6)');
22+
assert.equal($newCrate.attr('href').trim(), '/crates/Inflector');
2323

2424
const $mostDownloaded = findWithAssert('#most-downloaded ul > li:first a');
25-
hasText(assert, $mostDownloaded, 'libc (0.2.2)');
26-
assert.equal($mostDownloaded.attr('href').trim(), '/crates/libc');
25+
hasText(assert, $mostDownloaded, 'serde (0.6.1)');
26+
assert.equal($mostDownloaded.attr('href').trim(), '/crates/serde');
2727

2828
const $justUpdated = findWithAssert('#just-updated ul > li:first a');
29-
hasText(assert, $justUpdated, 'nanomsg (0.4.2)');
29+
hasText(assert, $justUpdated, 'nanomsg (0.7.0-alpha)');
3030
assert.equal($justUpdated.attr('href').trim(), '/crates/nanomsg');
3131
});

0 commit comments

Comments
 (0)