-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtin-lookup.js
67 lines (53 loc) · 1.76 KB
/
gtin-lookup.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
"use strict";
var request = require('request');
class GTINLookup {
constructor(key){
this.api = 'https://api.ean-search.org/api?format=json&token=' + key;
}
apiCall(url, callback) {
request(url, function(err, response, body){
var resp = {};
if (!err) {
resp.statusCode = response.statusCode;
if (response.statusCode == 200) {
resp.serverError = false;
try {
resp.product = JSON.parse(body);
} catch (e) {
resp.serverError = true;
}
callback(resp);
}
} else {
resp.serverError = true;
resp.statusCode = 500;
callback(resp);
}
});
}
gtinLookup(gtin, lang, callback) {
this.apiCall(this.api + "&op=barcode-lookup&ean=" + gtin + "&lang=" + lang, callback);
}
isbnLookup(isbn, callback) {
this.apiCall(this.api + "&op=barcode-lookup&isbn=" + isbn, callback);
}
gtinSearch(name, lang, callback) {
this.apiCall(this.api + "&op=product-search&name=" + encodeURI(name) + "&lang=" + lang, callback);
}
gtinCategorySearch(name, categoryCode, lang, callback) {
this.apiCall(this.api + "&op=category-search&name=" + encodeURI(name) + "&category=" + categoryCode + "&lang=" + lang, callback);
}
gtinPrefixSearch(prefix, lang, page, callback) {
this.apiCall(this.api + "&op=barcode-prefix-search&prefix=" + prefix + "&lang=" + lang + "&page=" + page, callback);
}
gtinIssuingCountryLookup(gtin, callback) {
this.apiCall(this.api + "&op=issuing-country&ean=" + gtin, callback);
}
gtinVerifyChecksum(gtin, callback) {
this.apiCall(this.api + "&op=verify-checksum&ean=" + gtin, callback);
}
gtinBarcodeImage(gtin, width, height, callback) {
this.apiCall(this.api + "&op=barcode-image&ean=" + gtin + "&width=" + width + "&height=" + height, callback);
}
}
module.exports = GTINLookup;