if (!$) { throw new Error('JQuery is required') } if (!_) { throw new Error('Lodash is required') } var simulados = {}; simulados.host = 'https://simulados.debolso.com.br'; simulados.practiceTestId = '4bZokWwxWCcfMtG3u4GxMD'; simulados.subscribe = subscribe.bind(simulados); simulados.getServerTime = getServerTime.bind(simulados); simulados.getSubscription = getSubscription.bind(simulados); simulados.getCurrentPracticeTest = getCurrentPracticeTest.bind(simulados); simulados.getReport = getReport.bind(simulados); function getServerTime () { var self = this; return new Promise(function(resolve, reject) { $.ajax({ method: 'GET', url: self.host + '/api/time', crossDomain: true, json: true, success: function (data, status, xhr) { return resolve({ data: data, status: status, xhr: xhr }) }, error: function (xhr, status, error) { return reject({ error: error, status: status, xhr: xhr }) } }); }); } function getCurrentPracticeTest () { var self = this; return new Promise(function(resolve, reject) { $.ajax({ method: 'GET', url: self.host + '/api/practicetests/' + self.practiceTestId, crossDomain: true, json: true, success: function (data, status, xhr) { return resolve({ data: data, status: status, xhr: xhr }) }, error: function (xhr, status, error) { return reject({ error: error, status: status, xhr: xhr }) } }); }); } function getSubscription (email) { var self = this; return new Promise(function(resolve, reject) { $.ajax({ method: 'GET', url: self.host + '/api/subscriptions?filter[email]=' + encodeURIComponent(email) + '&filter[practicetestid]=' + simulados.practiceTestId, crossDomain: true, json: true, success: function (data, status, xhr) { return resolve({ data: data, status: status, xhr: xhr }) }, error: function (xhr, status, error) { var response = { error: error, status: xhr.status, data: null, xhr: xhr } try { response.data = JSON.parse(xhr.responseText); } catch (error) { response.data = xhr.responseText; } return reject(response); } }); }); } function subscribe(email, fullName, discipline) { var self = this; return new Promise(function (resolve, reject) { $.ajax({ method: 'POST', url: self.host + '/api/subscriptions', json: true, crossDomain: true, dataType: 'json', contentType:'application/json', data: JSON.stringify({ email: _.deburr(_.trim(email.toLowerCase())), fullName: _.startCase(_.trim(fullName.toLowerCase())), discipline: discipline, practiceTestId: self.practiceTestId }), success: function (data, status, xhr) { return resolve({ data: data, status: status, xhr: xhr }) }, error: function (xhr, status, error) { var response = { error: error, status: xhr.status, data: null, xhr: xhr } try { response.data = JSON.parse(xhr.responseText); } catch (error) { response.data = xhr.responseText; } return reject(response); } }); }); } function getReport(email, practiceTest) { return new Promise(function (resolve, reject) { $.ajax({ method: 'GET', url: '/report/' + encodeURIComponent(email) + '?noredir=true' + (practiceTest ? ('&practicetest=' + practiceTest) : ''), success: function (data, status, xhr) { return resolve({ data: data, status: status, xhr: xhr }) }, error: function (xhr, status, error) { var response = { error: error, status: xhr.status, data: null, xhr: xhr } try { response.data = JSON.parse(xhr.responseText); } catch (error) { response.data = xhr.responseText; } return reject(response); } }); }); }