Skip to content

Instantly share code, notes, and snippets.

@carsonwright
Created August 10, 2018 17:13
Show Gist options
  • Save carsonwright/a1f1e0fe4bdfaaec5e679fc632b45dbb to your computer and use it in GitHub Desktop.
Save carsonwright/a1f1e0fe4bdfaaec5e679fc632b45dbb to your computer and use it in GitHub Desktop.
function API() {
this.apiToken = '';
this.request = function(method, route, data) {
var stuff = {
headers: { 'x-user-token': this.apiToken },
method: method,
url: route,
contentType: 'application/json'
};
if (data) {
stuff.data = JSON.stringify(data);
}
return new Promise(function (resolve, reject) {
$.ajax(stuff)
.success(resolve)
.fail(reject);
});
};
this.get = function(route) {
return this.request('GET', route);
};
this.post = function(route, data) {
return this.request('POST', route, data);
};
this.put = function(route, data) {
return this.request('PUT', route, data);
};
this.delete = function(route, data) {
return this.request('DELETE', route, data);
};
this.lines = function(filters) {
var fArray = [];
if (!filters) {
filters = {};
}
var fkeys = Object.keys(filters);
for (var i = 0, il = fkeys.length; i < il; i++) {
var key = fkeys[i];
var value = encodeURIComponent(filters[key]);
fArray.push('key=' + value);
}
var route = '/lines';
if (fArray.length) {
route += '?' + fArray.join('&');
}
return this.request('GET', route);
};
this.getAddressInfo = function (search) {
var api = this;
return this.geocode(search)
.then(function (data) {
return {
city: api.parseCity(data),
county: api.parseCounty(data),
state: api.parseState(data)
}
})
}
this.parseCity = function (data) {
if (!data || !data.length) {
return '';
}
return data[0].city;
};
this.parseState = function (data) {
if (!data || !data.length || !data[0].administrativeLevels) {
return '';
}
return data[0].administrativeLevels.level1short;
};
this.parseCounty = function (data) {
if (!data || !data.length) {
return '';
}
var county = '';
if (data[0].administrativeLevels.level2long) {
county = data[0].administrativeLevels.level2long.replace(/\s*County$/, '');
}
return county;
};
this.geocode = function (address) {
return this.request('POST', routes.geocode(), { address: address });
};
}
// var routes = {}
// var baseURL = window.location.protocol + "//" + window.location.host
// routes.organizations = function(){
// return baseURL + "/fmv/orgs";
// }
// routes.referralOptions = function(id){
// return baseURL + "/organizations/"+id+"/evet-referral-options";
// }
// routes.spotsByMonth = function(){
// return baseURL + "/spots/month"
// }
// routes.waitlist = function(){
// return baseURL + "/fmv/waitList"
// }
// routes.confirmation = function(){
// return baseURL + "/fmv/confirmation";
// }
// routes.booking = function(){
// return baseURL + "/evet/booking";
// }
// routes.promoCode = function(){
// return baseURL + "/fmv/promoCode";
// }
// routes.speciesBreeds = function(){
// return baseURL + "/species-and-breeds.csv"
// }
/**************************************************************************************
*
* Paste this at the bottom of your nilRoutes file and delete the old routes.geocode
*
**************************************************************************************/
routes.geocode = function(){
return baseURL + "/geocode"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment