Guest User

Untitled

a guest
Aug 10th, 2018
61
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function API() {
  2. this.apiToken = '';
  3.  
  4. this.request = function(method, route, data) {
  5. var stuff = {
  6. headers: { 'x-user-token': this.apiToken },
  7. method: method,
  8. url: route,
  9. contentType: 'application/json'
  10. };
  11.  
  12. if (data) {
  13. stuff.data = JSON.stringify(data);
  14. }
  15.  
  16. return new Promise(function (resolve, reject) {
  17. $.ajax(stuff)
  18. .success(resolve)
  19. .fail(reject);
  20. });
  21. };
  22.  
  23. this.get = function(route) {
  24. return this.request('GET', route);
  25. };
  26.  
  27. this.post = function(route, data) {
  28. return this.request('POST', route, data);
  29. };
  30.  
  31. this.put = function(route, data) {
  32. return this.request('PUT', route, data);
  33. };
  34.  
  35. this.delete = function(route, data) {
  36. return this.request('DELETE', route, data);
  37. };
  38.  
  39. this.lines = function(filters) {
  40. var fArray = [];
  41.  
  42. if (!filters) {
  43. filters = {};
  44. }
  45.  
  46. var fkeys = Object.keys(filters);
  47. for (var i = 0, il = fkeys.length; i < il; i++) {
  48. var key = fkeys[i];
  49. var value = encodeURIComponent(filters[key]);
  50. fArray.push('key=' + value);
  51. }
  52.  
  53. var route = '/lines';
  54. if (fArray.length) {
  55. route += '?' + fArray.join('&');
  56. }
  57.  
  58. return this.request('GET', route);
  59. };
  60.  
  61. this.getAddressInfo = function (search) {
  62. var api = this;
  63.  
  64. return this.geocode(search)
  65. .then(function (data) {
  66. return {
  67. city: api.parseCity(data),
  68. county: api.parseCounty(data),
  69. state: api.parseState(data)
  70. }
  71. })
  72. }
  73.  
  74. this.parseCity = function (data) {
  75. if (!data || !data.length) {
  76. return '';
  77. }
  78.  
  79. return data[0].city;
  80. };
  81.  
  82. this.parseState = function (data) {
  83. if (!data || !data.length || !data[0].administrativeLevels) {
  84. return '';
  85. }
  86.  
  87. return data[0].administrativeLevels.level1short;
  88. };
  89.  
  90. this.parseCounty = function (data) {
  91. if (!data || !data.length) {
  92. return '';
  93. }
  94.  
  95. var county = '';
  96.  
  97. if (data[0].administrativeLevels.level2long) {
  98. county = data[0].administrativeLevels.level2long.replace(/\s*County$/, '');
  99. }
  100.  
  101. return county;
  102. };
  103.  
  104. this.geocode = function (address) {
  105. return this.request('POST', routes.geocode(), { address: address });
  106. };
  107. }
RAW Paste Data