-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.js
500 lines (428 loc) · 15.4 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
var logger = require('winston'),
EventEmitter = require('events').EventEmitter,
assert = require("assert"),
util = require('util'),
auth = require('./lib/auth.js'),
metadata = require('./lib/metadata.js'),
search = require('./lib/search.js'),
object = require('./lib/object.js');
var KEY_MEMBER_NAME = "MemberName";
var KEY_USER = "User";
var KEY_BROKER = "Broker";
var KEY_LOGIN = "Login";
var KEY_LOGOUT = "Logout";
var KEY_SEARCH = "Search";
var KEY_ACTION = "Action";
var KEY_GET_OBJECT = "GetObject";
var KEY_GET_METADATA = "GetMetadata";
var KEY_METADATA_VERSION = "MetadataVersion";
var KEY_METADATA_TIMESTAMP = "MetadataTimestamp";
var KEY_MIN_METADATA_TIMESTAMP = "MinMetadataTimestamp";
var KEY_RETS_VERSION = "retsVersion";
var KEY_RETS_SERVER = "retsServer";
module.exports = new EventEmitter();
/**
* Connects to a RETS Service and creates a RETS client instance.
*
* @param loginUrl RETS login URL (i.e http://<MLS_DOMAIN>/rets/login.ashx)
* @param username username credential
* @param password password credential
*
* @event connection.success Connection succeeded.
* @event connection.failure(error) Failed to connect.
*
* @return RETS Client
*/
module.exports.getClient = function(loginUrl, username, password) {
var client = new Client();
auth.login(loginUrl, username, password, function(error, systemData) {
if (error) {
client.emit('connection.failure', error);
return;
}
client.configure(systemData);
client.emit('connection.success');
});
return client;
};
/**
* RETS Client Object
*
* @constructor Creates a new RETS Client object.
*/
var Client = function Client() {};
/** Rets Client inherits from EventEmitter **/
util.inherits(Client, EventEmitter);
/**
* Configures Rets Client
* @param systemData RETS system URL data object (Login, GetMetadata, GetObject, etc.)
*/
Client.prototype.configure = function(systemData) {
var self = this;
self.systemData = systemData;
self.retsVersion = self.systemData[KEY_RETS_VERSION];
self.retsServer = self.systemData[KEY_RETS_SERVER];
self.memberName = self.systemData[KEY_MEMBER_NAME];
self.user = self.systemData[KEY_USER];
self.broker = self.systemData[KEY_BROKER];
self.metadataVersion = self.systemData[KEY_METADATA_VERSION];
self.metadataTimestamp = self.systemData[KEY_METADATA_TIMESTAMP];
self.minMetadataTimestamp = self.systemData[KEY_MIN_METADATA_TIMESTAMP];
//metadata module
self.metadataModule = metadata(self.systemData[KEY_GET_METADATA]);
//search module
self.searchModule = search(self.systemData[KEY_SEARCH]);
//object module
self.objectModule = object(self.systemData[KEY_GET_OBJECT]);
};
/**
* Private method that handles client interface logic.
*
* @param client The RETS client instance
* @param error Error object (optional)
* @param data The RETS data (optional)
* @param eventSuccess Success event name (optional)
* @param eventFailure Failure event name (optional)
* @param callback Callback function (optional)
*/
var processRetsResponse = function(client, error, data, eventSuccess, eventFailure, callback) {
assert(client, "Client is present");
if (error) {
if (callback)
callback(error);
if (eventFailure)
client.emit(eventFailure, error);
return;
}
if (callback)
callback(error, data);
if (eventSuccess)
client.emit(eventSuccess, data);
};
/**
* Logout RETS user.
*
* @param url Logout URL
* @param callback(error)
*
* @event logout.success Disconnect was successful
* @event logout.failure(error) Disconnect failure
*
*/
Client.prototype.logout = function(callback) {
var self = this;
auth.logout(self.systemData[KEY_LOGOUT], function(error){
processRetsResponse(self, error, null, "logout.success", "logout.failure", callback);
});
};
/*
* Retrieves RETS Metadata.
*
* @param type Metadata type (i.e METADATA-RESOURCE, METADATA-CLASS)
* @param id Metadata id
* @param format Data format (i.e. COMPACT, COMPACT-DECODED)
* @param callback(error, data) (optional)
*
* @event metadata.success(data) Metadata call is successful
* @event metadata.failure(error) Metadata call failed.
*/
Client.prototype.getMetadata = function(type, id, format, callback) {
var self = this;
self.metadataModule.getMetadata(type, id, format, function(error, data) {
processRetsResponse(self, error, data, "metadata.success", "metadata.failure", callback);
});
};
/**
* Helper that retrieves RETS system metadata
*
* @param callback(error, data) (optional)
*
* @event metadata.system.success(data) Metadata call is successful
* @event metadata.system.failure(error) Metadata call failed
*/
Client.prototype.getSystem = function(callback) {
var self = this;
self.metadataModule.getSystem(function(error, data) {
processRetsResponse(self, error, data, "metadata.system.success", "metadata.system.failure", callback);
});
};
/**
* Helper that retrieves RETS resource metadata.
*
* @param callback(error, data) (optional)
*
* @event metadata.resources.success(data) Metadata call is successful
* @event metadata.resources.failure(error) Metadata call failed
*/
Client.prototype.getResources = function(callback) {
var self = this;
self.metadataModule.getResources(function(error, data){
processRetsResponse(self, error, data, "metadata.resources.success", "metadata.resources.failure", callback);
});
};
/**
* Helper that retrieves a listing of ALL RETS foreign key metadata.
*
* @param callback(error, data) (optional)
*
* @event metadata.all.foreignkeys.success(data) Metadata call is successful
* @event metadata.all.foreignkeys.failure(error) Metadata call failed
*/
Client.prototype.getAllForeignKeys = function(callback) {
var self = this;
self.metadataModule.getAllForeignKeys(function(error, data) {
processRetsResponse(self, error, data, "metadata.all.foreignkeys.success", "metadata.all.foreignkeys.failure", callback);
});
};
/**
* Helper that retrieves RETS foreign key metadata.
*
* @param resourceType Class resource type (i.e. Property, OpenHouse)
* @param callback(error, data) (optional)
*
* @event metadata.foreignkeys.success(data) Metadata call is successful
* @event metadata.foreignkeys.failure(error) Metadata call failed
*/
Client.prototype.getForeignKeys = function(resourceType, callback) {
var self = this;
self.metadataModule.getForeignKeys(resourceType, function(error, data) {
processRetsResponse(self, error, data, "metadata.foreignkeys.success", "metadata.foreignkeys.failure", callback);
});
};
/**
* Helper that retrieves a listing of ALL RETS class metadata.
*
* @param callback(error, data) (optional)
*
* @event metadata.all.class.success(data) Metadata call is successful
* @event metadata.all.class.failure(error) Metadata call failed
*/
Client.prototype.getAllClass = function(callback) {
var self = this;
self.metadataModule.getAllClass(function(error, data) {
processRetsResponse(self, error, data, "metadata.all.class.success", "metadata.all.class.failure", callback);
});
};
/**
* Helper that retrieves RETS class metadata.
*
* @param resourceType Class resource type (i.e. Property, OpenHouse)
* @param callback(error, data) (optional)
*
* @event metadata.class.success(data) Metadata call is successful
* @event metadata.class.failure(error) Metadata call failed
*/
Client.prototype.getClass = function(resourceType, callback) {
var self = this;
self.metadataModule.getClass(resourceType, function(error, data) {
processRetsResponse(self, error, data, "metadata.class.success", "metadata.class.failure", callback);
});
};
/**
* Helper that retrieves a listing of ALL RETS table metadata.
*
* @param callback(error, data) (optional)
*
* @event metadata.all.table.success(data) Metadata call is successful
* @event metadata.all.table.failure(error) Metadata call failed
*/
Client.prototype.getAllTable = function(callback) {
var self = this;
self.metadataModule.getAllTable(function(error, data) {
processRetsResponse(self, error, data, "metadata.all.table.success", "metadata.all.table.failure", callback);
});
};
/**
* Helper that retrieves RETS table metadata.
*
* @param resourceType Table resource type (i.e. Property, OpenHouse)
* @param classType Table class type (RESI, LAND, etc.)
* @param callback(error, data) (optional)
*
* @event metadata.table.success(data) Metadata call is successful
* @event metadata.table.failure(error) Metadata call failed
*/
Client.prototype.getTable = function(resourceType, classType, callback) {
var self = this;
self.metadataModule.getTable(resourceType, classType, function(error, data) {
processRetsResponse(self, error, data, "metadata.table.success", "metadata.table.failure", callback);
});
};
/**
* Helper that retrieves a listing of ALL RETS resource lookups metadata.
*
* @param callback(error, data) (optional)
*
* @event metadata.all.lookups.success(data) Metadata call is successful
* @event metadata.all.lookups.failure(error) Metadata call failed
*/
Client.prototype.getAllLookups = function(callback) {
var self = this;
self.metadataModule.getAllLookups(function(error, data) {
processRetsResponse(self, error, data, "metadata.all.lookups.success", "metadata.all.lookups.failure", callback);
});
};
/**
* Helper that retrieves a RETS resource lookups metadata.
*
* @param resourceType Table resource type (i.e. Property, OpenHouse)
* @param callback(error, data) (optional)
*
* @event metadata.lookups.success(data) Metadata call is successful
* @event metadata.lookups.failure(error) Metadata call failed
*
*/
Client.prototype.getLookups = function(resourceType, callback) {
var self = this;
self.metadataModule.getLookups(resourceType, function(error, data) {
processRetsResponse(self, error, data, "metadata.lookups.success", "metadata.lookups.failure", callback);
});
};
/**
* Helper that retrieves a listing of ALL RETS resource lookup types metadata.
*
* @param callback(error, data) (optional)
*
* @event metadata.all.lookupTypes.success(data) Metadata call is successful
* @event metadata.all.lookupTypes.failure(error) Metadata call failed
*/
Client.prototype.getAllLookupTypes = function(callback) {
var self = this;
self.metadataModule.getAllLookupTypes(function(error, data) {
processRetsResponse(self, error, data, "metadata.all.lookupTypes.success", "metadata.all.lookupTypes.failure", callback);
});
};
/**
* Helper that retrieves a RETS resource lookup type metadata.
*
* @param resourceType Table resource type (i.e. Property, OpenHouse)
* @param lookupType (ArchitecturalStyle, etc.)
* @param callback(error, data) (optional)
*
* @event metadata.lookupTypes.success(data) Metadata call is successful
* @event metadata.lookupTypes.failure(error) Metadata call failed
*/
Client.prototype.getLookupTypes = function(resourceType, lookupType, callback) {
var self = this;
self.metadataModule.getLookupTypes(resourceType, lookupType, function(error, data) {
processRetsResponse(self, error, data, "metadata.lookupTypes.success", "metadata.lookupTypes.failure", callback);
});
};
/**
* Helper that retrieves a RETS resource object metadata.
*
* @param resourceType Table resource type (i.e. Property, OpenHouse)
* @param callback(error, data) (optional)
*
* @event metadata.object.success(data) Metadata call is successful
* @event metadata.object.failure(error) Metadata call failed
*/
Client.prototype.getObjectMeta = function(resourceType, callback) {
var self = this;
self.metadataModule.getObject(resourceType, function(error, data) {
processRetsResponse(self, error, data, "metadata.object.success", "metadata.object.failure", callback);
});
};
/**
* Invokes RETS search operation.
*
* @param _queryOptions Search query options.
* See RETS specification for query options.
*
* Default values query params:
*
* queryType:'DMQL2',
* format:'COMPACT-DECODED',
* count:1,
* standardNames:0,
* restrictedIndicator:'***',
* limit:"NONE"
*
* @param callback(error, data) (optional)
*
* @event search.success(data) Search RETS is successful
* @event search.failure(error) Search RETS failed
*/
Client.prototype.searchRets = function(_queryOptions, callback) {
var self = this;
self.searchModule.searchRets(_queryOptions, function(error, data) {
processRetsResponse(self, error, data, "search.success", "search.failure", callback);
});
};
/**
*
* Helper that performs a targeted RETS search query and parses results.
*
* @param resourceType Rets resource type (ex: Property)
* @param classType Rets class type (ex: RESI)
* @param queryString Rets query string. See RETS specification - (ex: MatrixModifiedDT=2014-01-01T00:00:00.000+)
* @param callback(error, data) (optional)
* @param _limit (optional) Limits the number of records returned.
*
* @event query.success(data) Query is successful
* @event query.failure(error) Query failed
*/
Client.prototype.query = function(resourceType, classType, queryString, callback, _limit) {
var self = this;
self.searchModule.query(resourceType, classType, queryString, function(error, data){
processRetsResponse(self, error, data, "query.success", "query.failure", callback);
},
_limit);
};
/**
* Retrieves RETS object data.
*
* @param resourceType Rets resource type (ex: Property)
* @param objectType Rets object type (ex: LargePhoto)
* @param objectId Object identifier
* @param callback(error, contentType, data) (optional)
*
* @event object.success({
*
* contentType,
* data
* }) Object call is successful
* @event object.failure(error) Object call failed
*/
Client.prototype.getObject = function(resourceType, objectType, objectId, callback) {
var self = this;
self.objectModule.getObject(resourceType, objectType, objectId, function(error, contentType, data) {
if (error) {
if (callback)
callback(error);
self.emit("object.failure", error);
}
if (callback)
callback(error, contentType, data);
self.emit("object.success", {contentType:contentType, data:data});
});
};
/**
* Helper that retrieves a list of photo objects.
*
* @param resourceType Rets resource type (ex: Property)
* @param photoType Photo object type, based on getObjects meta call (ex: LargePhoto, Photo)
* @param matrixId Photo matrix identifier.
* @param callback(error, dataList) (optional)
*
* Each item in data list is an object with the following data elements:
*
* {
* buffer:<data buffer>,
* mime:<data buffer mime type>,
* description:<data description>,
* contentDescription:<data content description>,
* contentId:<content identifier>,
* objectId:<object identifier>
* }
*
* @event photos.success(dataList) Photos call is successful
* @event photos.failure(error) Photos call failed
*
*/
Client.prototype.getPhotos = function(resourceType, photoType, matrixId, callback) {
var self = this;
self.objectModule.getPhotos(resourceType, photoType, matrixId, function(error, data) {
processRetsResponse(self, error, data, "photos.success", "photos.failure", callback);
});
};