Skip to content

Instantly share code, notes, and snippets.

@nathanbarrett
Created November 7, 2015 16:42
Show Gist options
  • Select an option

  • Save nathanbarrett/d918b6e2c191ca650264 to your computer and use it in GitHub Desktop.

Select an option

Save nathanbarrett/d918b6e2c191ca650264 to your computer and use it in GitHub Desktop.
Angular service that interacts with Edmunds Api
/*
* Angular service for working with the edmunds api
*
* How to use:
* 1. Include in controller
* 2. $scope.vehicle = EdmundsService.vehicle
* 3. Put a watcher on $scope.vehicle and send the new value to EdmundsService.updateVehicle(vehicle)
* 4. Check edmundsConfig and constants. Adjust as necessary.
*
* @params $http, ExceptionService
*
* @returns none
*/
function EdmundsService ($http, ExceptionService) {
var vm = this;
/**** Constants / Private Variables ****/
var edmundsKey;
var oldestYear = 1930;
var now = new Date();
var newestYear = now.getFullYear() + 1;
var yearsArray = getYearsArray();
// Edmunds config
var edmundsConfig = {
route: '/getEdmundsKey',
method: 'GET',
getKeyOnInit: true,
getTrimInfo: true
};
/**** Public Variables/Functions ****/
vm.vehicle = {
vin: {
search: true,
loading: false,
value: null,
invalidVin: null
},
year: {
options: [],
value: null
},
make: {
options: [],
loading: false,
niceName: '',
value: null
},
model: {
options: [],
loading: false,
niceName: '',
value: null
},
trim: {
options: [],
loading: false,
value: null
}
};
vm.updateVehicle = updateVehicle;
vm.getEdmundsKey = getEdmundsKey;
vm.getYearsArray = getYearsArray;
vm.initializeVehicle = initializeVehicle;
vm.getMakesAndModels = getMakesAndModels;
vm.getModelOptions = getModelOptions;
vm.getTrimOptions = getTrimOptions;
vm.vinSearch = vinSearch;
/**** Initializations ****/
if(edmundsConfig.getKeyOnInit){
vm.getEdmundsKey();
}
vm.vehicle.year.options = vm.getYearsArray();
/**** Private Functions ****/
/*
* Allows the scope to update the vehicle object. Options are automatically added
*
* @params vehicle (object)
*
* @returns updated vehicle
*/
function updateVehicle (vehicle) {
if(vehicle.vin.value !== vehicle.vin.invalidVin){
vehicle.vin.invalidVin = null;
}
//Check for vin search
if(vehicle.vin.search &&
vehicle.vin.value &&
!vehicle.vin.loading &&
!vehicle.year.value &&
!vehicle.vin.invalidVin){
if(vehicle.vin.value.length === 17){
vehicle.vin.loading = true;
vehicle.year.loading = true;
vehicle.make.loading = true;
vehicle.model.loading = true;
vm.vinSearch();
}
}
else if(!vehicle.vin.search &&
yearsArray.indexOf(parseInt(vehicle.year.value)) >= 0 &&
vehicle.make.options.length === 0 &&
!vehicle.make.loading &&
!vehicle.make.value){
vehicle.make.loading = true;
vm.getMakesAndModels();
}
else if(!vehicle.vin.search &&
vehicle.make.value &&
vehicle.model.options.length === 0){
vehicle.model.loading = true;
vm.getModelOptions();
}
if(!vehicle.vin.search &&
vehicle.model.value &&
vehicle.trim.options.length === 0 &&
!vehicle.trim.loading &&
!vehicle.trim.value &&
edmundsConfig.getTrimInfo){
vehicle.trim.loading = true;
vm.getTrimOptions();
}
return vm.vehicle;
}
/*
* Gets the edmunds key from the server
*
* @params none
*
* @returns edmundsKey (string)
*/
function getEdmundsKey () {
return $http({
method: edmundsConfig.method,
url: edmundsConfig.route
})
.then(function (response) {
if (response.data.success) {
edmundsKey = response.data.edmundsKey;
}
else if(response.data.error) {
ExceptionService.logError({
code: 201,
display: false,
log: true,
details: response
});
}
}, function (error) {
ExceptionService.logError({
code: 201,
display: false,
log: true,
details: error
});
});
}
/*
* Returns an array to populate the Year select box
*
* @params none
*
* @returns array
*/
function getYearsArray () {
var yearsArray = [];
for (var i = newestYear; i >= oldestYear; i--) {
yearsArray.push(i);
}
return yearsArray;
}
/*
* Initializes vehicle object to original state
*
* @params none
*
* @returns Updated vehicle object
*/
function initializeVehicle () {
var newVehicle = {
vin: {
search: true,
loading: false,
value: null,
invalidVin: null
},
year: {
options: getYearsArray(),
value: null
},
make: {
options: [],
loading: false,
niceName: '',
value: null
},
model: {
options: [],
loading: false,
niceName: '',
value: null
},
trim: {
options: [],
loading: false,
value: null
}
};
return angular.copy(newVehicle, vm.vehicle);
}
/*
* Queries Edmunds api for makes and models given a year
*
* @params year (integer)
*
* @returns makes and models array
*/
function getMakesAndModels (year) {
if(typeof year === 'string'){
year = parseInt(year);
}
else if(year === undefined){
year = parseInt(vm.vehicle.year.value);
}
if(yearsArray.indexOf(year) < 0){
ExceptionService.logError({
code: 202,
display: false,
log: true,
details: [year, yearsArray]
});
return false;
}
else if(!edmundsKey){
ExceptionService.logError({
code: 209,
display: false,
log: true,
details: ['getMakesAndModels', edmundsKey]
});
return false;
}
return $http.get('https://api.edmunds.com/api/vehicle/v2/makes?state=used&year=' +
year + '&view=basic&fmt=json&api_key=' + edmundsKey)
.then(function (response) {
vm.vehicle.make.options = response.data.makes;
vm.vehicle.make.loading = false;
}, function (error) {
ExceptionService.logError({
code: 203,
display: false,
log: true,
details: error
});
});
}
/*
* Gets an array of models for the given make and also sets the make 'niceName' for getting vehicle trim
*
* @params make (string)
*
* @returns model options (array)
*/
function getModelOptions (make) {
if(make === undefined){
make = vm.vehicle.make.value;
}
if(typeof make !== 'string'){
ExceptionService.logError({
code: 204,
display: false,
log: true,
details: [make]
});
return false;
}
else if(vm.vehicle.make.options.length === 0){
ExceptionService.logError({
code: 205,
display: false,
log: true,
details: [make]
});
return false;
}
for (var i = 0; i < vm.vehicle.make.options.length; i++) {
if(make === vm.vehicle.make.options[i].name){
vm.vehicle.make.niceName = vm.vehicle.make.options[i].niceName;
vm.vehicle.model.options = vm.vehicle.make.options[i].models;
vm.vehicle.model.loading = false;
return vm.vehicle.model.options;
}
}
ExceptionService.logError({
code: 206,
display: false,
log: true,
details: [make, vm.vehicle]
});
return false;
}
/*
* Gets the trim options for the vehicle
*
* @params year, make, model OR fetch from local copy
*
* @returns trim options array
*/
function getTrimOptions(year, make, model){
if(year === undefined && make === undefined && model === undefined){
year = parseInt(vm.vehicle.year.value);
make = vm.vehicle.make.niceName;
if(vm.vehicle.vin.search){
model = vm.vehicle.model.niceName;
}
else {
// get the model nice name
for (var i = 0; i < vm.vehicle.model.options.length; i++) {
if(vm.vehicle.model.value === vm.vehicle.model.options[i].name){
vm.vehicle.model.niceName = vm.vehicle.model.options[i].niceName;
model = vm.vehicle.model.niceName;
break;
}
}
}
}
else if(!edmundsKey){
ExceptionService.logError({
code: 209,
display: false,
log: true,
details: ['getTrimOptions', edmundsKey]
});
return false;
}
return $http.get('https://api.edmunds.com/api/vehicle/v2/' +
make + '/' + model +
'/' + year +
'/styles?state=used&view=full&fmt=json&api_key=' + edmundsKey)
.then(function (response) {
if(response.data.stylesCount === 0){
vm.vehicle.trim.value = false;
}
else if(response.data.stylesCount === 1){
vm.vehicle.trim.value = response.data.styles[0].name;
}
else if(response.data.stylesCount > 1){
vm.vehicle.trim.options = response.data.styles;
}
vm.vehicle.trim.loading = false;
}, function (error) {
ExceptionService.logError({
code: 208,
display: false,
log: true,
details: error
});
return false;
});
}
/*
* Queries edmunds api with a vin and updates vehicle object
*
* @params optional vin
*
* @returns $http function
*/
function vinSearch (vin) {
if(vin === undefined){
vin = vm.vehicle.vin.value;
}
else if(!edmundsKey){
ExceptionService.logError({
code: 209,
display: false,
log: true,
details: ['vinSearch', edmundsKey]
});
return false;
}
else if(vm.vehicle.vin.value && vm.vehicle.year.value){
ExceptionService.logError({
code: 210,
display: false,
log: true,
details: vm.vehicle
});
return false;
}
return $http.get('https://api.edmunds.com/api/vehicle/v2/vins/' +
vin + '?fmt=json&api_key=' + edmundsKey)
.then(function (response) {
vm.vehicle.year.value = response.data.years[0].year;
vm.vehicle.make.value = response.data.make.name;
vm.vehicle.make.niceName = response.data.make.niceName;
vm.vehicle.model.value = response.data.model.name;
vm.vehicle.model.niceName = response.data.model.niceName;
vm.vehicle.vin.loading = false;
vm.vehicle.year.loading = false;
vm.vehicle.make.loading = false;
vm.vehicle.model.loading = false;
vm.vehicle.vin.invalidVin = null;
if(edmundsConfig.getTrimInfo){
vm.vehicle.trim.loading = true;
getTrimOptions();
}
}, function (error) {
vm.vehicle.vin.invalidVin = vm.vehicle.vin.value;
vm.vehicle.vin.loading = false;
vm.vehicle.year.loading = false;
vm.vehicle.make.loading = false;
vm.vehicle.model.loading = false;
vm.vehicle.trim.loading = false;
ExceptionService.logError({
code: 207,
display: false,
log: true,
details: error
});
return false;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment