-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
181 lines (148 loc) · 4.11 KB
/
index.js
File metadata and controls
181 lines (148 loc) · 4.11 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var request = require('request');
var moment = require('moment');
module.exports = function(o) {
const apiKey = o.apiKey;
const bookSlug = o.bookSlug;
if (!bookSlug) {
throw new Error('Missing `bookSlug`!');
}
const urlRoot = 'https://leanpub.com/' + bookSlug;
return {
previewFull: function (cb) {
req('post', '/preview.json', {}, cb);
},
previewSubset: function (cb) {
req('post', '/preview/subset.json', {}, cb);
},
publish: function (o, cb) {
if(!cb) {
return req('post', '/publish.json', {}, o);
}
const releaseNotes = o.releaseNotes || '';
req('post', '/publish.json', {
'publish[email_readers]': true,
'publish[release_notes]': releaseNotes
}, cb);
},
jobStatus: function (cb) {
req('get', '/book_status', {}, cb);
},
bookSummary: function (cb) {
req('get', '.json', {}, cb);
},
latestVersion: function (o, cb) {
const format = o.format;
const output = o.output;
if(!format) {
return cb(new Error('Missing `format`'));
}
if(!output) {
return cb(new Error('Missing `output`'));
}
cb(new Error('Not implemented yet!'));
},
sales: function (o, cb) {
if(!cb) {
return req('get', '/sales.json', {}, o);
}
const format = o.format || 'json';
req('get', '/sales.' + format, {}, cb);
},
individualSales: function (o, cb) {
if(!cb) {
return req('get', '/individual_purchases.json', {}, o);
}
const format = o.format || 'json';
const page = o.page || 1;
req('get', '/individual_purchases.' + format + '?page=' + page, {}, cb);
},
coupons: function (o, cb) {
if(!cb) {
return req('get', '/coupons.json', {}, o);
}
const format = o.format || 'json';
req('get', '/coupons.' + format, {}, cb);
},
createCoupon: function (o, cb) {
// TODO: tidy up this logic
if(!o.couponCode) {
return cb(new Error('Missing `couponCode`'));
}
if(!o.packageDiscounts) {
return cb(new Error('Missing `packageDiscounts`'));
}
if(!o.startDate) {
return cb(new Error('Missing `startDate`'));
}
if(!o.maxUses) {
return cb(new Error('Missing `maxUses`'));
}
const body = {
'coupon_code': o.couponCode,
'package_discounts_attributes': toUnderscore(o.packageDiscounts),
'start_date': leanpubDate(o.startDate),
'max_uses': o.maxUses,
'note': o.note,
'suspended': o.suspended
};
if(o.endDate) {
body['end_date'] = leanpubDate(o.endDate);
}
req('post', '/coupons.json', body, cb);
},
updateCoupon: function (o, cb) {
const couponCode = o.couponCode;
const body = {};
// TODO: tidy up this logic
if(!couponCode) {
return cb(new Error('Missing `couponCode`'));
}
if('packageDiscounts' in o) {
body['package_discounts_attributes'] = toUnderscore(o.packageDiscounts);
}
if('startDate' in o) {
body['start_date'] = leanpubDate(o.startDate);
}
if('endDate' in o) {
body['end_date'] = leanpubDate(o.endDate);
}
if('maxUses' in o) {
body['max_uses'] = o.maxUses;
}
if('note' in o) {
body['note'] = o.note;
}
if('suspended' in o) {
body['suspended'] = o.suspended;
}
req('put', '/coupons/' + couponCode + '.json', body, cb);
}
};
function leanpubDate(date) {
return moment(date).format('YYYY-MM-DD');
}
function toUnderscore(discounts) {
return (discounts || []).map(function(discount) {
return {
'discounted_price': discount.discountedPrice,
'package_slug': discount.packageSlug
};
});
}
function req(method, resource, body, cb) {
request({
method: method,
url: urlRoot + resource,
qs: {
'api_key': apiKey
},
body: body,
json: true
}, function (err, d) {
if(err) {
return cb(err);
}
cb(null, d.body);
});
}
};