-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
965 lines (852 loc) · 22.3 KB
/
index.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
//! itc-report.js
//! version : 0.0.1
//! authors : Marek Serafin
//! license : MIT
//! github.com/stoprocent/itc-report
/*
* Module dependencies.
*/
var async = require('async'),
request = require('request'),
moment = require('moment'),
fs = require('fs'),
path = require('path'),
_ = require('underscore');
/**
* @module itunesconnect
*/
/*
* Expose `Connect`
*/
exports.Connect = Connect;
/*
* Expose `Report`.
*/
exports.Report = Report;
/*
* Expose `Type`.
*/
exports.type = {
inapp : "IA0, IA1, IA4, IA9, IAA, IAC, IAW, IAY, IA3, IA6, IAB, IAD, IAX, IAZ",
app : "1E, 1EP, 1EU,1F,1T, 4E, 4EP, 4EU, 4F, 4T, 7, 8, 7F, 7T, 8F, 8T, 2, 3, 5, 6, 2F, 2T, 3F, 3T, 5F, 5T, 6F, 6T, 1, 4"
}
/*
* Expose `Transaction`.
*/
exports.transaction = {
free : "1, 2, 3, 4",
paid : "5, 6, 7, 8, 9, 54",
redownload : "1001, 1005, 1006",
update : "1002",
refund : "1003"
}
/*
* Expose `Platform`.
*/
exports.platform = {
desktop : "Windows, Macintosh, UNKNOWN",
iphone : "iPhone",
ipad : "iPad",
ipod : "iPod",
}
/*
* Expose `Measure`.
*/
exports.measure = {
proceeds : "Royalty",
units : "units"
}
/**
* Initialize a new `Connect` with the given `username`, `password` and `options`.
*
* Examples:
*
* // Import itc-report
* var itc = require("itunesconnect"),
* Report = itc.Report;
*
* // Init new iTunes Connect
* var itunes = new itc.Connect('[email protected]', 'password');
*
* // Init new iTunes Connect
* var itunes = new itc.Connect('[email protected]', 'password', {
* errorCallback: function(error) {
* console.log(error);
* },
* concurrentRequests: 1
* });
*
* @class Connect
* @constructor
* @param {String} username Apple ID login
* @param {String} password Apple ID password
* @param {Object} [options]
* @param {String} [options.baseURL] iTunes Connect Login URL
* @param {String} [options.apiURL] iTunes Connect API URL
* @param {Number} [options.concurrentRequests] Number of concurrent requests
* @param {Array} [options.cookies] Cookies array. If you provide cookies array it will not login and use this instead.
* @param {Function} [options.errorCallback] Error callback function called when requests are failing
* @param {Function} [options.errorCallback.error] Login error
* @param {Function} [options.loginCallback] Login callback function called when login to iTunes Connect was a success.
* @param {Function} [options.loginCallback.cookies] cookies are passed as a first argument. You can get it and cache it for later.
*/
function Connect(username, password, options) {
// Default Options
this.options = {
baseURL : "https://itunesconnect.apple.com",
apiURL : "https://reportingitc2.apple.com/api/",
loginURL : "https://idmsa.apple.com/appleauth/auth/signin",
appleWidgetKey : "22d448248055bab0dc197c6271d738c3",
concurrentRequests : 2,
errorCallback : function(e) {},
loginCallback : function(c) {}
};
// Extend options
_.extend(this.options, options);
// Set cookies
this._cookies = [];
// Task Executor
this._queue = async.queue(
this.executeRequest.bind(this),
this.options.concurrentRequests
);
// Pasue queue and wait for login to complete
this._queue.pause();
// Login to iTunes Connect
if(typeof this.options["cookies"] !== 'undefined') {
this._cookies = this.options.cookies;
this._queue.resume();
}
else {
this.login(username, password);
}
}
/**
* Request iTunes Connect report with the given `query` and `completed` callback.
*
* Examples:
*
* // Import itc-report
* var itc = require("itunesconnect"),
* Report = itc.Report;
*
* // Init new iTunes Connect
* var itunes = new itc.Connect('[email protected]', 'password');
*
* // Request timed report from yesterday to today
* itunes.request(Report.timed().time(1, 'day'), function(error, result) {
* console.log(result);
* })
*
* @method request
* @for Connect
* @param {Query} query
* @param {Function} completed
* @param {Error} completed.error Just an error if occure
* @param {Object} completed.result Report result
* @param {Object} [completed.query] Query that was sent
* @chainable
*/
Connect.prototype.request = function(query, completed) {
// Push request to queue
this._queue.push({
query: query,
completed: completed
});
return this;
}
/**
* Fetch iTunes Connect Reporting metadata with given `completed` callback.
*
* Examples:
*
* // Import itc-report
* var itc = require("itunesconnect"),
* Report = itc.Report;
*
* // Init new iTunes Connect
* var itunes = new itc.Connect('[email protected]', 'password');
*
* // Fetch API Metadata
* itunes.metadata(function(error, result) {
* console.log(result);
* })
*
* @method metadata
* @for Connect
* @for Connect
* @param {Function} completed
* @param {Error} completed.error Just an error if occure
* @param {Object} completed.result Metadata result
* @param {Object} [completed.query] Query that was sent
*/
Connect.prototype.metadata = function(completed) {
var query = {
body: function(){},
endpoint: "all_metadata"
}
this._queue.push({query: query, completed: completed});
}
/**
* Execute iTunes Connect report request with given `task` and `callback`.
*
* @private
* @method executeRequest
* @for Connect
* @param {Object} task
* @param {Function} callback
*/
Connect.prototype.executeRequest = function(task, callback) {
var query = task.query;
var completed = task.completed;
// Keep request body for callback
var requestBody = query.body();
// Run request
request.post({
url : this.options.apiURL + query.endpoint,
body : requestBody,
headers : {
'Content-Type': 'application/json',
'Cookie': this._cookies
}
}, function(error, response, body) {
if(!response.hasOwnProperty('statusCode')){
error = new Error('iTunes Connect is not responding. The service may be temporarily offline.');
body = null;
}else if(response.statusCode == 401) {
error = new Error('This request requires authentication. Please check your username and password.');
body = null;
}
else {
try {
body = JSON.parse(body);
} catch (e) {
error = new Error('There was an error while parsing JSON.');
body = null;
}
}
// Call completed callback
completed(error, body, requestBody);
// Call callback to mark queue task as done
callback();
})
}
/**
* Login to iTunes Connect with given `username` and `password`.
*
* @private
* @method login
* @for Connect
* @param {String} username Apple ID login
* @param {String} password Apple ID password
*/
Connect.prototype.login = function(username, password) {
var self = this;
//Request apple login page from which we need a "my account info" cookie
request.post({
url : this.options.loginURL,
headers : { 'Content-Type': 'application/json', 'X-Apple-Widget-Key': this.options.appleWidgetKey },
json : {
"accountName" : username,
"password" : password,
"rememberMe" : false
}
}, function(error, response, body) {
var cookies = response ? response.headers['set-cookie'] : null;
if (error || !(cookies && cookies.length)) {
error = error || new Error('There was a problem with loading the login page cookies. Check login credentials.');
} else {
//extract the account info cookie
var myAccount = /myacinfo=.+?;/.exec(cookies);
if (myAccount == null || myAccount.length == 0) {
error = error || new Error('No account cookie :( Apple probably changed the login process');
} else {
//Request itunes connect page that will give us itCtx cookie needed for api requests
request.get({
//not sure where this action comes from, so it's hardcoded
url : self.options.baseURL + "/WebObjects/iTunesConnect.woa",
followRedirect : false, //We can't follow redirects, otherwise we will "miss" the itCtx cookie
headers : {
'Cookie': myAccount[0]
},
}, function(error, response, body) {
cookies = response ? response.headers['set-cookie'] : null;
if (error || !(cookies && cookies.length)) {
error = error || new Error('There was a problem with loading the login page cookies.');
} else {
//extract the itCtx cookie
var itCtx = /itctx=.+?;/.exec(cookies);
if (itCtx == null || itCtx.length == 0) {
error = error || new Error('No itCtx cookie :( Apple probably changed the login process');
} else {
//We preserve only these two cookies, because keeping all of them resulted in some non-deterministic 500-errors from api
self._cookies = myAccount[0] + " " + itCtx[0];
self.options.loginCallback(self._cookies);
//Start requests queue
self._queue.resume();
}
}
});
}
}
})
}
/**
* Initialize a new `Query` with the given `type` and `config`.
*
* Examples:
*
* // Import itc-report
* var itc = require("itunesconnect"),
* Report = itc.Report;
*
* // Init new iTunes Connect
* var itunes = new itc.Connect('[email protected]', 'password');
*
* // Timed type query
* var query = Report('timed');
*
* // Ranked type query with config object
* var query = Report('ranked', { limit: 100 });
*
* // Advanced Example
* var advancedQuery = Report('timed', {
* start : '2014-04-08',
* end : '2014-04-25',
* limit : 100,
* filters : {
* content: [{AppID}, {AppID}, {AppID}],
* location: [{LocationID}, {LocationID}],
* transaction: itc.transaction.free,
* type: [
* itc.type.inapp,
* itc.type.app
* ],
* category: {CategoryID}
* },
* group: 'content'
* });
*
* @class Report
* @constructor
* @param {String} <type>
* @param {Object} [config]
* @param {String|Date} [config.start] Date or if String must be in format YYYY-MM-DD
* @param {Object} [config.end] Date or if String must be in format YYYY-MM-DD
* @param {String} [config.interval] One of the following:
* @param {String} config.interval.day
* @param {String} config.interval.week
* @param {String} config.interval.month
* @param {String} config.interval.quarter
* @param {String} config.interval.year
* @param {Object} [config.filters] Possible keys:
* @param {Number|Array} [config.filters.content]
* @param {String|Array} [config.filters.type]
* @param {String|Array} [config.filters.transaction]
* @param {Number|Array} [config.filters.category]
* @param {String|Array} [config.filters.platform]
* @param {Number|Array} [config.filters.location]
* @param {String} [config.group] One of following:
* @param {String} config.group.content
* @param {String} config.group.type
* @param {String} config.group.transaction
* @param {String} config.group.category
* @param {String} config.group.platform
* @param {String} config.group.location
* @param {Object} [config.measures]
* @param {Number} [config.limit]
* @return {Query}
*/
function Report(type, config) {
var fn = Query.prototype[type];
if(typeof fn !== 'function') {
throw new Error('Unknown Report type: ' + type);
}
return new Query(config)[type]();
}
/**
* Initialize a new `Query` with the ranked type and given `config`.
*
* Examples:
*
* // Import itc-report
* var itc = require("itunesconnect"),
* Report = itc.Report;
*
* // Init new iTunes Connect
* var itunes = new itc.Connect('[email protected]', 'password');
*
* // Ranked type query
* var query = Report.ranked();
*
* // Another query
* var otherQuery = Report.ranked({
* limit: 10
* });
*
* @method ranked
* @for Report
* @param {Object} [config]
* @chainable
* @return {Query}
*/
Report.ranked = function(config) {
return new Query(config).ranked();
}
/**
* Initialize a new `Query` with the timed type and given `config`.
*
* Examples:
*
* // Import itc-report
* var itc = require("itunesconnect"),
* Report = itc.Report;
*
* // Init new iTunes Connect
* var itunes = new itc.Connect('[email protected]', 'password');
*
* // Timed type query
* var query = Report.timed();
*
* // Another query
* var otherQuery = Report.timed({
* limit: 10
* });
*
* @method timed
* @for Report
* @param {Object} [config]
* @chainable
* @return {Query}
*/
Report.timed = function(config) {
return new Query(config).timed();
}
/**
* Initialize a new `Query` with the given `query`.
*
* Constants to use with Query
*
* // Import itc-report
* var itc = require("itunesconnect"),
*
* // Types
* itc.type.inapp
* itc.type.app
*
* // Transactions
* itc.transaction.free
* itc.transaction.paid
* itc.transaction.redownload
* itc.transaction.update
* itc.transaction.refund
*
* // Platforms
* itc.platform.desktop
* itc.platform.iphone
* itc.platform.ipad
* itc.platform.ipod
*
* // Measures
* itc.measure.proceeds
* itc.measure.units
*
* @class Query
* @constructor
* @private
* @param {Object} config
* @chainable
* @return {Query}
*/
function Query(config) {
this.type = null;
this.endpoint = null;
this.config = {
start : moment(),
end : moment(),
filters : {},
measures : ['units'],
limit : 100
};
// Extend options with user stuff
_.extend(this.config, config);
// Private Options
this._time = null;
this._body = {};
}
/**
* Builds and returns body for Connect request as JSON string
*
* @method body
* @for Query
* @private
* @return {String} JSON String
*/
Query.prototype.body = function() {
// Ensure date is moment object
this.config.start = TransformValue.toMomentObject(this.config.start);
this.config.end = TransformValue.toMomentObject(this.config.end);
// If start and end date are same and time() was used in query calculate new start date
if (this.config.end.diff(this.config.start, 'days') === 0 && _.isArray(this._time)) {
this.config.start = this.config.start.subtract(this._time[0], this._time[1]);
}
else if (this.config.end.diff(this.config.start, 'days') < 0) {
this.config.start = this.config.end;
}
// Building body
this._body = {
"start_date" : this.config.start.format("YYYY-MM-DD[T00:00:00.000Z]"),
"end_date" : this.config.end.format("YYYY-MM-DD[T00:00:00.000Z]"),
"interval" : this.config.interval,
"filters" : TransformValue.toBodyFilters(this.config.filters),
"group" : TransformValue.toAppleKey(this.config.group),
"measures" : this.config.measures,
"limit" : this.config.limit
};
return JSON.stringify(this._body);
}
/**
* Initialize a new `Query` with the timed type.
*
* @private
* @method timed
* @for Query
* @chainable
*/
Query.prototype.timed = function() {
this.type = 'timed';
this.endpoint = 'data/timeseries';
// Defaults for ranked type
this.config.group = this.config.group || null;
this.config.interval = this.config.interval || 'day';
return this;
}
/**
* Initialize a new `Query` with the ranked type.
*
* @private
* @method ranked
* @for Query
* @chainable
*/
Query.prototype.ranked = function() {
this.type = 'ranked';
this.endpoint = 'data/ranked';
// Defaults for ranked type
this.config.group = this.config.group || 'content';
this.config.interval = this.config.interval || 'day';
return this;
}
/**
* Sets interval property for `Query`
*
* @method interval
* @for Query
* @param {String} value One of the following:
* @param {String} value.day
* @param {String} value.week
* @param {String} value.month
* @param {String} value.quarter
* @param {String} value.year
* @chainable
*/
Query.prototype.interval = function(value) {
this.config.interval = value;
return this;
}
/**
* Sets start and end property for `Query`
*
* Examples:
*
* // Start and end date set manualy
* query.date('2014-04-08', new Date());
*
* // Start and end date will be todays date
* query.date(new Date());
*
* // Start and end date will be set as 8th of April 2014
* query.date('2014-04-08');
*
* @method date
* @for Query
* @param {String|Date} <start> If end is undefined it will be set same as start. If String, must be in format YYYY-MM-DD
* @param {String|Date} [end] If String, must be in format YYYY-MM-DD
* @chainable
*/
Query.prototype.date = function(start, end) {
this.config.start = TransformValue.toMomentObject( start );
this.config.end = TransformValue.toMomentObject(
((typeof end == 'undefined') ? start : end)
);
return this;
}
/**
* Sets start property for `Query` in more easy/generic way
*
* Examples:
*
* query.time(1, 'week');
* query.time(20, 'days');
*
* @method time
* @for Query
* @param {Number} <value>
* @param {String} <unit> day, week, month, etc...
* @chainable
*/
Query.prototype.time = function(value, unit) {
this._time = [value, unit];
return this;
}
/**
* Sets `group by` property for `Query`
*
* @method group
* @for Query
* @param {String} value One of following:
* @param {String} value.content
* @param {String} value.type
* @param {String} value.transaction
* @param {String} value.category
* @param {String} value.platform
* @param {String} value.location
* @chainable
*/
Query.prototype.group = function(value) {
this.config.group = value;
return this;
}
/**
* Sets measures property for `Query`
*
* @method measures
* @for Query
* @param {String|Array} <value>
* @chainable
*/
Query.prototype.measures = function(value) {
this.config.measures = value;
return this;
}
/**
* Sets limit property for `Query`
*
* @method limit
* @for Query
* @param {Number} <value> Not sure if Apple is using limit in ranked type query
* @chainable
*/
Query.prototype.limit = function(value) {
this.config.limit = value;
return this;
}
/**
* Sets content filter for `Query`
*
* @method content
* @for Query
* @param {Number|Array} <value> AppStore ID
* @chainable
*/
Query.prototype.content = function(value) {
if(typeof this.config.filters["content"] === "undefined")
this.config.filters.content = [];
if(!_.isArray(this.config.filters.content))
this.config.filters.content = [this.config.filters.content];
if(_.isArray(value))
this.config.filters.content = this.config.filters.content.concat(value);
else
this.config.filters.content.push(value);
return this;
}
/**
* Sets category filter for `Query`
*
* Examples:
*
* // Import itc-report
* var itc = require("itunesconnect"),
* Report = itc.Report;
*
* // Query
* var query = Report.timed({
* limit: 10
* }).category(6001);
*
* // Another Query
* var otherQuery = Report.timed({
* limit: 10
* });
*
* //
* otherQuery.category([6001, 6002, 6003]);
* otherQuery.category([6004, 6005, 6006]).category(6007);
*
* @method category
* @for Query
* @param {Number|Array} <value> Visit https://github.com/stoprocent/itc-report/wiki/Cheet-Sheet#categories for available options
* @chainable
*/
Query.prototype.category = function(value) {
if(typeof this.config.filters["category"] === "undefined")
this.config.filters.category = [];
if(!_.isArray(this.config.filters.category))
this.config.filters.category = [this.config.filters.category];
if(_.isArray(value))
this.config.filters.category = this.config.filters.category.concat(value);
else
this.config.filters.category.push(value);
return this;
}
/**
* Sets location filter for `Query`
*
* @method location
* @for Query
* @param {Number|Array} <value> Visit https://github.com/stoprocent/itc-report/wiki/Cheet-Sheet#countries for available options
* @chainable
*/
Query.prototype.location = function(value) {
if(typeof this.config.filters["location"] === "undefined")
this.config.filters.location = [];
if(!_.isArray(this.config.filters.location))
this.config.filters.location = [this.config.filters.location];
if(_.isArray(value))
this.config.filters.location = this.config.filters.location.concat(value);
else
this.config.filters.location.push(value);
return this;
}
/**
* Sets platform filter for `Query`
*
* @method platform
* @for Query
* @param {String|Array} <value> (Look in Constants under Platforms)
* @chainable
*/
Query.prototype.platform = function(value) {
if(typeof this.config.filters["platform"] === "undefined")
this.config.filters.platform = [];
if(!_.isArray(this.config.filters.platform))
this.config.filters.platform = [this.config.filters.platform];
if(_.isArray(value))
this.config.filters.platform = this.config.filters.platform.concat(value);
else
this.config.filters.platform.push(value);
return this;
}
/**
* Sets type filter for `Query`
*
* @method type
* @for Query
* @param {String|Array} <value> (Look in Constants under Types)
* @chainable
*/
Query.prototype.type = function(value) {
if(typeof this.config.filters["type"] === "undefined")
this.config.filters.type = [];
if(!_.isArray(this.config.filters.type))
this.config.filters.type = [this.config.filters.type];
if(_.isArray(value))
this.config.filters.type = this.config.filters.type.concat(value);
else
this.config.filters.type.push(value);
return this;
}
/**
* Sets transaction filter for `Query`
*
* @method transaction
* @for Query
* @param {String|Array} <value> (Look in Constants under Transactions)
* @chainable
*/
Query.prototype.transaction = function(value) {
if(typeof this.config.filters["transaction"] === "undefined")
this.config.filters.transaction = [];
if(!_.isArray(this.config.filters.transaction))
this.config.filters.transaction = [this.config.filters.transaction];
if(_.isArray(value))
this.config.filters.transaction = this.config.filters.transaction.concat(value);
else
this.config.filters.transaction.push(value);
return this;
}
/*
* Transform Value Object
*
* @private
*/
var TransformValue = {};
/*
* Translates simple filters object to apple api format
*
* @private
* @function toBodyFilters
* @for TransformValue
* @param {Object} filters
* @return {Object}
*/
TransformValue.toBodyFilters = function(filters) {
var result = [];
_.each(filters, function(value, dimension) {
if(!_.isArray(value))
value = [value];
result.push({
dimension_key : TransformValue.toAppleKey(dimension),
option_keys : value
});
});
return result;
}
/*
* Translates key to apple key
*
* @private
* @function toAppleKey
* @for TransformValue
* @param {String} key
* @return {String}
*/
TransformValue.toAppleKey = function(key) {
if(key === null)
return null;
var keys = {
content : "content",
type : "content_type",
transaction : "transaction_type",
category : "Category",
platform : "platform",
location : "piano_location"
};
if(typeof keys[key] === 'undefined')
throw new Error('Unknown Apple Key for key: ' + key);
return keys[key];
}
/*
* Translates given date to moment object
*
* @private
* @function toMomentObject
* @for TransformValue
* @param {Mixed} date
* @return {Moment}
*/
TransformValue.toMomentObject = function(date) {
// Quick check if moment
if(moment.isMoment(date)) {
return date;
}
else if(date instanceof Date) {
return moment(date);
}
else if(_.isString(date) && !!(date.match(new RegExp(/([0-9]{4})-([0-9]{2})-([0-9]{2})/)))) {
return moment(date, "YYYY-MM-DD");
}
else {
throw new Error('Unknown date format. Please use Date() object or String() with format YYYY-MM-DD.');
}
}