UNPKG

epitech-intranet

Version:

epitech intranet wrapper

211 lines (185 loc) 6.13 kB
// Generated by CoffeeScript 1.4.0 (function() { var models, qs, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; models = require('./models'); qs = require('querystring'); module.exports.Client = (function() { function Client(opts) { this.opts = opts != null ? opts : {}; this.request = __bind(this.request, this); this.fetchRequest = __bind(this.fetchRequest, this); this.parseRequest = __bind(this.parseRequest, this); this.authRequired = __bind(this.authRequired, this); this.debug = __bind(this.debug, this); this.initModels = __bind(this.initModels, this); this.initModel = __bind(this.initModel, this); this.parseOptions = __bind(this.parseOptions, this); this.defaultOptions = __bind(this.defaultOptions, this); this.defaultOptions(); this.parseOptions(); this.initModels(); return this; } Client.prototype.defaultOptions = function() { var _base, _base1, _ref, _ref1; if ((_ref = (_base = this.opts).login) == null) { _base.login = ''; } return (_ref1 = (_base1 = this.opts).url) != null ? _ref1 : _base1.url = 'https://intra.epitech.eu/'; }; Client.prototype.parseOptions = function() { var host, k, v, _base, _base1, _base10, _base2, _base3, _base4, _base5, _base6, _base7, _base8, _base9, _ref, _ref1, _ref10, _ref11, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9; if (this.opts.url != null) { _ref = require('url').parse(this.opts.url); for (k in _ref) { v = _ref[k]; if ((_ref1 = (_base = this.opts)[k]) == null) { _base[k] = v; } } } if (this.opts.ssl != null) { if ((_ref2 = (_base1 = this.opts).protocol) == null) { _base1.protocol = 'https:'; } } if ((_ref3 = (_base2 = this.opts).host) == null) { _base2.host = this.opts.hostname; } host = this.opts.host.split(':'); if ((_ref4 = (_base3 = this.opts).hostname) == null) { _base3.hostname = host[0]; } if ((_ref5 = (_base4 = this.opts).port) == null) { _base4.port = host[1]; } if ((_ref6 = (_base5 = this.opts).username) == null) { _base5.username = 'admin'; } if ((_ref7 = (_base6 = this.opts).protocol) == null) { _base6.protocol = 'http:'; } if ((_ref8 = (_base7 = this.opts).port) == null) { _base7.port = this.opts.protocol === 'https:' ? 443 : 80; } this.opts.port = parseInt(this.opts.port); if ((_ref9 = (_base8 = this.opts).path) == null) { _base8.path = '/'; } if ((_ref10 = (_base9 = this.opts).pathname) == null) { _base9.pathname = this.opts.path; } this.opts.host = "" + this.opts.hostname + ":" + this.opts.port; if ((_ref11 = (_base10 = this.opts).verbose) == null) { _base10.verbose = false; } this.http = this.opts.protocol === 'https:' ? require('https') : require('http'); delete this.opts.href; delete this.opts.url; delete this.opts.slashes; return delete this.opts.auth; }; Client.prototype.initModel = function(cls) { var model; model = new cls(this); model.setupRoutes(); model.attach(); return model; }; Client.prototype.initModels = function() { var k, v, _results; this.models = {}; _results = []; for (k in models) { v = models[k]; _results.push(this.models[k] = this.initModel(v)); } return _results; }; Client.prototype.debug = function(args) { if (this.opts.verbose) { return console.log(args); } }; Client.prototype.authRequired = function(fn) {}; Client.prototype.parseRequest = function(buffer, fn) { var json; json = JSON.parse(buffer.split('\n').slice(1).join('\n')); return fn(false, json); }; Client.prototype.fetchRequest = function(args, fn) { var fullPath, fullPathWithQuery, query, req, reqOpts, _ref; if (args == null) { args = {}; } fullPath = "" + this.opts.path + args.path; if ((_ref = args['format']) == null) { args['format'] = 'json'; } delete args.path; query = qs.stringify(args); fullPathWithQuery = "" + fullPath + "?" + query; reqOpts = { host: this.opts.hostname, port: this.opts.port, method: args.method || 'GET', path: fullPathWithQuery, headers: { Host: this.opts.hostname } }; this.debug("fullpath: " + fullPath + ", fullPathWithQuery: " + fullPathWithQuery); req = this.http.request(reqOpts); req.on('error', function(err) { return fn(err, {}); }); req.end(); return req.on('response', function(response) { var buffer; buffer = ''; response.on('data', function(chunk) { return buffer += chunk; }); return response.on('end', function() { switch (response.statusCode) { case 401: return fn({ "code": "AUTH REQUIRED" }, buffer); case 200: if (fn) { return fn(null, buffer); } break; default: return fn({ "code": "BADSTATUSCODE", "message": response.statusCode }, buffer); } }); }); }; Client.prototype.request = function(args, fn) { var _this = this; if (args == null) { args = {}; } if (typeof args === 'string') { args = { path: args }; } return this.fetchRequest(args, function(err, buffer) { if (err) { return fn(err, buffer); } return _this.parseRequest(buffer, function(err, object) { return fn(err, object); }); }); }; return Client; })(); }).call(this);