-
Notifications
You must be signed in to change notification settings - Fork 0
/
comicvine.js
71 lines (60 loc) · 2.49 KB
/
comicvine.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
var request = require('request'),
backbone = require('backbone'),
Caching = require('caching'),
models = require('./lib/db'),
UrlBuilder = require('./lib/urlbuilder.js'),
RequestBuilder = require('./lib/requestbuilder.js'),
SyncBuilder = require('./lib/syncbuilder.js');
/**
* ComicVine API
* @param config {Object} Config object
*
* @cfg cache {Boolean/Caching} Caching function compatible with node-caching
*
* @return {*}
* @constructor
*/
var ComicVine = function(config) {
var apikey,
cache,
apiUrl;
/*
* @cfg apikey {String} ComicVine API Key
*/
if (!config.apikey) throw new Error ("ComicVine API key not found. Did you use the 'apikey' config option?");
apikey = config.apikey;
/*
* @cfg cache {Boolean/String} Use 'redis' or 'memory' to use different caches. Use false to disable the caching system.
* @cfg host {Number} Redis server host, mandatory if cache=='redis'.
* @cfg port {Number} Redis server port, mandatory if cache=='redis'.
*/
if (config.cache===false) {
cache = false;
} else if (typeof config.cache == "string") {
if (config.cache == "redis") {
if (typeof config.port !== "number") throw new Error ("Invalid port. If you use 'redis' cache, be sure to specify the Redis server port");
if (typeof config.host !== "string") throw new Error ("Invalid host. If you use 'redis' cache, be sure to specify the Redis server host");
cache = new Caching("redis", { host: config.host, port: config.port });
}else if (config.cache == "memory") {
cache = new Caching("memory");
} else {
throw new Error ("Unknow cache mechanism '"+config.cache+"'. Please, use 'redis' or 'memory'");
}
}else {
throw new Error ("Cache info not found. If you wan't to disable cache, use cache:false config option.");
}
/**
* @cfg apiUrl {String} (Optional) Base URl to use for the API requests (defaults to 'http://api.comicvine.com'). Only useful if you are using your own mirror.
*/
if (typeof config.apiUrl !== "string") {
apiUrl = "http://api.comicvine.com";
} else {
apiUrl = config.apiUrl;
}
var urlBuilder = new UrlBuilder(apikey, apiUrl);
var requestBuilder = new RequestBuilder(urlBuilder,request);
var syncBuilder = new SyncBuilder(urlBuilder, requestBuilder , cache);
backbone.sync = syncBuilder.build();
return models;
}
module.exports = ComicVine;