-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (111 loc) · 4.36 KB
/
index.js
File metadata and controls
136 lines (111 loc) · 4.36 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
'use strict';
var AnalyticsTracker = require('reviewsii-analytics-tracker');
var request = require('superagent');
/**
* Begin the Mobials JS SDK
*/
var MobialsAPI = {};
//we need to fetch our global MobialsAPI variable from wherever this was called.
(function (global){
MobialsAPI = typeof global.MobialsAPI !== 'undefined' ? global.MobialsAPI : MobialsAPI;
MobialsAPI.debug = typeof MobialsAPI.debug === 'undefined' ? false : MobialsAPI.debug;
MobialsAPI.dispatch_uri = MobialsAPI.dispatch_uri ? MobialsAPI.dispatch_uri : 'https://api.mobials.com/tracker/dispatch';
MobialsAPI.APIUri = MobialsAPI.api_uri ? MobialsAPI.api_uri : 'https://api.mobials.com/api/js';
MobialsAPI.APIKey = MobialsAPI.APIKey ? MobialsAPI.APIKey : null;
MobialsAPI.domain = MobialsAPI.domain ? MobialsAPI.domain : 'static.mobials.com';
MobialsAPI.language = MobialsAPI.language ? MobialsAPI.language : 'en';
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {});
module.exports = {
/**
* Pass in your config before using
*
* @param config object containing config properties
*/
init: function(config) {
if (typeof config.APIKey === 'undefined' || !config.APIKey) {
return null;
}
MobialsAPI.APIKey = config.APIKey;
MobialsAPI.debug = config.debug ? config.debug : false;
MobialsAPI.APIUri = config.APIUri ? config.APIUri : MobialsAPI.APIUri;
MobialsAPI.language = config.language ? config.language : MobialsAPI.language;
return true;
},
/**
* Fetch rating information for a single business
*
* @param businessId
* @param callback
*/
fetchRating: function(businessId, callback) {
if (!MobialsAPI.APIKey) {
return callback({
success: false,
statusCode: 400,
message: 'No API key has been set'
});
}
var url = MobialsAPI.APIUri + '/business/' + businessId + '/rating?access_token=' + MobialsAPI.APIKey + '&language=' + MobialsAPI.language;
request
.get(url)
.set('Content-Type', 'text/plain')
.end(function(err, res){
if (err || !res.ok) {
return callback({
success: false,
statusCode: err.code == 'ENOTFOUND' || !err.response ? 404 : err.response.statusCode,
message: err.response && err.response.body ? err.response.body.error : null
});
}
callback(res.body);
});
AnalyticsTracker.track('impression', {
client_id: businessId,
resource_id: 1
});
},
/**
* Fetch rating information for multiple business (max 100)
*
* @param businessIds
* @param callback
*/
fetchBatchRatings: function(businessIds, callback) {
if (!MobialsAPI.APIKey) {
return callback({
success: false,
statusCode: 400,
message: 'No API key has been set'
});
}
if (businessIds.length > 100) {
return callback({
success: false,
statusCode: 400,
message: 'Maximum 100 businesses can be passsed at a time'
});
}
var url = MobialsAPI.APIUri + '/businesses/ratings?access_token=' + MobialsAPI.APIKey + '&language=' + MobialsAPI.language + '&business_ids=';
url += businessIds.join(',');
request
.get(url)
.set('Content-Type', 'text/plain')
.end(function(err, res){
if (err || !res.ok) {
return callback({
success: false,
statusCode: err.code == 'ENOTFOUND' || !err.response ? 404 : err.response.statusCode,
message: err.response && err.response.body ? err.response.body.error : null
});
}
callback(res.body);
});
var payloads = businessIds.map(function(businessId) {
return {
client_id: businessId,
resource_id: 1
};
});
AnalyticsTracker.trackBatch('impression', payloads);
}
};