Skip to content

Commit 9109a14

Browse files
authored
Merge pull request #74 from Jackymancs4-Fork/feature/random-endpoint
Feature: Random endpoint
2 parents a4c1930 + 6298eed commit 9109a14

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Assuming your server lives at `myserver.com`, and you've configured the middlewa
4141
* e.g. `myserver.com/myAvatars/face/eyes1/nose2/mouth4/DEADBF/300`
4242
* `myserver.com/myAvatars/list`
4343
* returns JSON of all valid parts for the custom endpoint above
44+
* `myserver.com/myAvatars/:size?/random`
45+
* returns a random avatar, different each time
46+
* e.g. `myserver.com/myAvatars/300/random`
47+
4448

4549
## Development
4650
If you're developing locally, you'll first need to bootstrap (assumes [nvm](https://github.com/creationix/nvm)):

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"find-port": "^1.0.0",
2424
"gm": "^1.16.0",
2525
"serve-favicon": "^2.0.1",
26-
"underscore": "^1.6.0"
26+
"underscore": "^1.6.0",
27+
"uuid": "^3.2.1"
2728
},
2829
"repository": {
2930
"type": "git",

src/routes/avatars.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import Router from 'express';
2+
import uuid from 'uuid';
23

3-
import {
4-
allNames,
5-
pathFor,
6-
} from '../lib/imageFiles';
4+
import { allNames, pathFor } from '../lib/imageFiles';
75
import common from './common';
86
import { combine } from '../lib/imager';
97
import potato from '../lib/potato';
@@ -30,13 +28,17 @@ router.get('/list', function(req, res) {
3028
return res.set('Content-Type', 'application/json').send(response);
3129
});
3230

33-
router.get('/:id', function(req, res, next) {
34-
return combine(req.faceParts, false, function(err, stdout) {
35-
return common.sendImage(err, stdout, req, res, next);
31+
router.get('/:size?/random', function(req, res) {
32+
var faceParts;
33+
faceParts = potato.parts(uuid.v4());
34+
req.faceParts = faceParts;
35+
36+
return combine(faceParts, req.params.size, function(err, stdout) {
37+
return common.sendImage(err, stdout, req, res);
3638
});
3739
});
3840

39-
router.get('/:size/:id', function(req, res, next) {
41+
router.get('/:size?/:id', function(req, res, next) {
4042
return combine(req.faceParts, req.params.size, function(err, stdout) {
4143
return common.sendImage(err, stdout, req, res, next);
4244
});

test/integration.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,39 @@ describe('routing', function() {
7272
});
7373
});
7474
});
75+
76+
describe('v2 avatar random requests', function() {
77+
it('can randomly generate a new avatar', function(done) {
78+
const getRandom = () => request
79+
.get('/avatars/random')
80+
.expect(200)
81+
.expect('Content-Type', /image/)
82+
.parse(parseImage);
83+
84+
getRandom().end((_, r1) => {
85+
getRandom().end((_, r2) => {
86+
im(r1.body).identify('%#', (_, id1) => {
87+
im(r2.body).identify('%#', (_, id2) => {
88+
expect(id1).not.to.equal(id2);
89+
done();
90+
});
91+
});
92+
});
93+
});
94+
});
95+
96+
it('supports a custom size parameter', function(done) {
97+
request
98+
.get('/avatars/50/random')
99+
.expect(200)
100+
.expect('Content-Type', /image/)
101+
.parse(parseImage)
102+
.end(function(err, res) {
103+
im(res.body).size(function(_, size) {
104+
expect(size).to.eql({ height: 50, width: 50 });
105+
done();
106+
});
107+
});
108+
});
109+
});
75110
});

0 commit comments

Comments
 (0)