-
-
Notifications
You must be signed in to change notification settings - Fork 244
/
reviews.js
74 lines (61 loc) · 1.73 KB
/
reviews.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
const R = require('ramda');
const common = require('./common');
const app = require('./app');
const c = require('./constants');
function ensureArray (value) {
if (!value) {
return [];
}
if (Array.isArray(value)) {
return value;
}
return [value];
}
function cleanList (results) {
const reviews = ensureArray(results.feed.entry);
return reviews.map((review) => ({
id: review.id.label,
userName: review.author.name.label,
userUrl: review.author.uri.label,
version: review['im:version'].label,
score: parseInt(review['im:rating'].label),
title: review.title.label,
text: review.content.label,
url: review.link.attributes.href,
updated: review.updated.label
}));
}
const reviews = (opts) => new Promise((resolve) => {
validate(opts);
if (opts.id) {
resolve(opts.id);
} else if (opts.appId) {
resolve(app(opts).then(app => app.id));
}
})
.then((id) => {
opts = opts || {};
opts.sort = opts.sort || c.sort.RECENT;
opts.page = opts.page || 1;
opts.country = opts.country || 'us';
const url = `https://itunes.apple.com/${opts.country}/rss/customerreviews/page=${opts.page}/id=${id}/sortby=${opts.sort}/json`;
return common.request(url, {}, opts.requestOptions);
})
.then(JSON.parse)
.then(cleanList);
function validate (opts) {
if (!opts.id && !opts.appId) {
throw Error('Either id or appId is required');
}
if (opts.sort && !R.includes(opts.sort, R.values(c.sort))) {
throw new Error('Invalid sort ' + opts.sort);
}
if (opts.page && opts.page < 1) {
throw new Error('Page cannot be lower than 1');
}
if (opts.page && opts.page > 10) {
throw new Error('Page cannot be greater than 10');
}
}
module.exports = reviews;