freewifi
Version:
Connection to the FreeWifi network (by the french internet and mobile provider free.fr)
173 lines (155 loc) • 4.77 kB
JavaScript
// Generated by CoffeeScript 1.4.0
(function() {
var FreeWifi,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
FreeWifi = (function() {
function FreeWifi(options) {
var _base, _base1, _base2, _base3, _base4, _ref, _ref1, _ref2, _ref3, _ref4;
this.options = options != null ? options : {};
this.connect = __bind(this.connect, this);
this._connectVerify = __bind(this._connectVerify, this);
this._connectRequest = __bind(this._connectRequest, this);
this._request = __bind(this._request, this);
this.loginRequired = __bind(this.loginRequired, this);
if ((_ref = (_base = this.options).host) == null) {
_base.host = 'wifi.free.fr';
}
if ((_ref1 = (_base1 = this.options).port) == null) {
_base1.port = 443;
}
if ((_ref2 = (_base2 = this.options).scheme) == null) {
_base2.scheme = 'https';
}
if ((_ref3 = (_base3 = this.options).verbose) == null) {
_base3.verbose = true;
}
if ((_ref4 = (_base4 = this.options).successString) == null) {
_base4.successString = 'CONNEXION AU SERVICE REUSSIE';
}
this.http = this.options.scheme === 'https' ? require('https') : require('http');
}
FreeWifi.prototype.loginRequired = function() {
if (!this.options.login) {
throw new Error('the `login` option is required');
}
if (!this.options.password) {
throw new Error('the `password` option is required');
}
};
FreeWifi.prototype._prepareQuery = function(query) {
var key, val;
return [
(function() {
var _results;
_results = [];
for (key in query) {
val = query[key];
_results.push("" + key + "=" + val);
}
return _results;
})()
][0].join('&') + '\n';
};
FreeWifi.prototype._request = function(opts, fn) {
var query, req, _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7;
if ((_ref = opts.host) == null) {
opts.host = this.options.host;
}
if ((_ref1 = opts.port) == null) {
opts.port = this.options.port;
}
if ((_ref2 = opts.scheme) == null) {
opts.scheme = this.options.scheme;
}
if ((_ref3 = opts.path) == null) {
opts.path = '/';
}
if ((_ref4 = opts.headers) == null) {
opts.headers = {};
}
if ((_ref5 = opts.method) == null) {
opts.method = 'GET';
}
if ((_ref6 = opts.expects) == null) {
opts.expects = 200;
}
if ((_ref7 = opts.query) == null) {
opts.query = null;
}
if (opts.query) {
query = this._prepareQuery(opts.query);
opts.headers['Content-Length'] = query.length;
opts.headers['Content-Type'] = 'application/x-www-form-urlencoded';
opts.headers['Accept'] = '*/*';
} else {
query = null;
}
req = this.http.request(opts, function(res) {
var data;
data = '';
res.on('data', function(buf) {
return data += buf;
});
res.on('error', function() {
return console.log('error');
});
return res.on('end', function() {
if (res.statusCode !== opts.expects) {
return fn(true, '', {
code: res.statusCode,
options: opts,
response: data
});
}
return fn(false, data, res);
});
});
req.on('error', function(e) {
return fn(true, '', e);
});
return req.end(query);
};
FreeWifi.prototype._connectRequest = function(fn) {
var opts;
opts = {
method: 'POST',
path: '/Auth',
expects: 200,
query: {
login: this.options.login,
password: this.options.password,
submit: 'Valider'
}
};
return this._request(opts, fn);
};
FreeWifi.prototype._connectVerify = function(data, res, fn) {
if (-1 === data.indexOf(this.options.successString)) {
return fn(true, 'Authentication error');
} else {
return fn(false, 'Success');
}
};
FreeWifi.prototype.connect = function(fn) {
var _this = this;
if (fn == null) {
fn = null;
}
try {
this.loginRequired();
} catch (e) {
return fn(true, e);
}
return this._connectRequest(function(err, data, res) {
if (err) {
return fn(true, res);
}
return _this._connectVerify(data, res, fn);
});
};
return FreeWifi;
})();
module.exports = {
FreeWifi: FreeWifi
};
}).call(this);