-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexample-data.js
157 lines (139 loc) · 4.1 KB
/
example-data.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
/**
* This module contains two functions that generate random data for the examples/tests functions in examples.js.
*/
var faker = require( 'faker' ),
_ = require( 'underscore' ),
moment = require( 'moment' ),
request = require( 'request' );
_.mixin( require( 'underscore.deferred' ) );
/**
* Generates an array of random events. Each event is in the following format:
*
* {
* calendarId: 1,
* date: new Date(), // A `Date` instance for some time yesterday, today, or tomorrow
* title: 'Random title'
* }
*/
exports.events = function () {
var events = [],
today = moment().startOf( 'day' ),
tomorrow = moment( today ).add( 1, 'days' ),
yesterday = moment( today ).subtract( 1, 'days' );
_.each([ yesterday, today, tomorrow ], function (date) {
var hour;
for ( hour = 10; hour < 14; hour++ ) {
events.push({
calendarId: faker.random.number() + faker.random.number() + 1,
date: moment( date ).hour( hour ).toDate(),
title: faker.lorem.sentence()
});
}
});
return events;
};
/**
* Returns a promise that when it's resolved, provides an array of 20 randomly generated contacts.
* Each contact is in the following format:
*
* {
* firstName: 'Random name',
* lastName: 'Random name',
* dateOfBirth: new Date(), // A `Date` instance for a random date in the past
* isMale: false,
* addresses: [], // An array of objects containing: type, line1, line2 (optional), city, state, zip
* phones: [], // An array of objects containing: type and number
* emails: [], // An array of objects containing: type and address
* note: null, // Optional. If present it could be `null` or an object containing: lastUpdatedAt (a `Date` in the past) and note
* avatar: new Buffer() // Optional. If present, it would be a Buffer containing the bytes of an actual image
* }
*/
exports.contacts = function() {
var contacts = [],
promises = [];
_.times(20, function (i) {
var deferred,
contact = {
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
dateOfBirth: faker.date.past(),
isMale: !!faker.random.number(),
addresses:[{
type: 'home',
line1: faker.address.streetAddress(),
city: faker.address.city(),
state: faker.address.stateAbbr(),
zip: faker.address.zipCode()
}],
phones: [{
type: 'home',
number: faker.phone.phoneNumberFormat()
}],
emails: [{
type: 'home',
address: faker.internet.email()
}]
};
if ( faker.random.number() ) {
contact.addresses[ 0 ].line2 = faker.address.secondaryAddress();
}
if ( faker.random.number() ) {
contact.addresses.push({
type: 'work',
line1: faker.address.streetAddress(),
city: faker.address.city(),
state: faker.address.stateAbbr(),
zip: faker.address.zipCode()
});
if ( faker.random.number() ) {
contact.addresses[ 1 ].line2 = faker.address.secondaryAddress();
}
}
if ( faker.random.number() ) {
contact.phones.push({
type: 'work',
number: faker.phone.phoneNumberFormat()
});
}
if ( faker.random.number() ) {
contact.emails.push({
type: 'work',
address: faker.internet.email()
});
}
if ( faker.random.number() ) {
contact.note = {
lastUpdatedAt: faker.date.past(),
note: faker.lorem.sentences()
};
} else if ( faker.random.number() ) {
contact.note = null;
}
if ( faker.random.number() ) {
deferred = new _.Deferred();
promises.push( deferred.promise() );
/**
* Get an image, using a HTTP request, and use the response `body` (an instance of `Buffer`)
* as the `avatar` attribute.
*/
request({ url: faker.internet.avatar(), encoding: null }, function (error, response, body) {
if ( !error ) {
contact.avatar = body;
}
deferred.resolve();
});
}
contacts.push( contact );
});
if ( promises.length === 0 ) {
return new _.Deferred().resolve( contacts ).promise();
} else if ( promises.length === 1 ) {
return promises[ 0 ].then(function() {
return contacts;
});
} else {
return _.when( promises ).then(function() {
return contacts;
});
}
};