UNPKG

1.26 kBJavaScriptView Raw
1// Require initiator.listener
2
3var events = require ('events'),
4 urlUtil = require ('url'),
5 util = require ('util');
6
7
8var model = module.exports = function (url, optionalParams) {
9
10 var self = this;
11
12 if (url.constructor === String) {
13 try {
14 this.url = urlUtil.parse (url, true);
15 var a = this.url.protocol.length;
16 } catch (e) {
17 self.emit ('error', e);
18 }
19 } else {
20 this.url = url;
21 if (!this.url.protocol) {
22 // assume http
23 this.url.protocol = 'http:';
24 }
25 }
26
27 // convert:
28 // http: -> http
29 // https: -> http
30 // ftp: -> ftp
31 // sftp: -> ftp
32 this.modelName = this.url.protocol.replace(/(^s|:$|s:$)/g, '');
33
34 // console.log (this.modelName);
35 var requiredModel = require ('./'+this.modelName);
36 this.dataSource = new requiredModel (this, optionalParams);
37
38 // fetch method
39
40 this.fetch = function (target) {
41 self.dataSource.fetch(target);
42 }
43
44 this.store = function (target) {
45 self.dataSource.store(target);
46 }
47
48 this.stop = function (reason) {
49 if (self.dataSource.stop)
50 self.dataSource.stop (reason);
51 }
52
53 // this.init();
54}
55
56util.inherits (model, events.EventEmitter);
57
58// there are some examples:
59// fetch: url (call GET for http, RETR for ftp)
60// store: url (call POST for http, PUT for ftp, send email for mailto)