-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NHTSA DB make/model querying, bump version to 0.1.0
- Loading branch information
Showing
6 changed files
with
232 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import 'package:basic_utils/basic_utils.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'dart:convert'; | ||
|
||
class NHTSAResult { | ||
String value; | ||
String valueId; | ||
String variable; | ||
int variableId; | ||
|
||
NHTSAResult({this.value, this.valueId, this.variable, this.variableId}); | ||
|
||
NHTSAResult.fromJson(Map<String, dynamic> json) { | ||
value = json['Value']; | ||
valueId = json['ValueId']; | ||
variable = json['Variable']; | ||
variableId = json['VariableId']; | ||
} | ||
|
||
@override | ||
String toString() { | ||
return 'NHTSAResult[value=$value, valueId=$valueId, variable=$variable, variableId=$variableId]'; | ||
} | ||
} | ||
|
||
class NHTSAVehicleInfo { | ||
int count; | ||
String message; | ||
String searchCriteria; | ||
List<NHTSAResult> results; | ||
|
||
NHTSAVehicleInfo( | ||
{this.count, this.message, this.searchCriteria, this.results}); | ||
|
||
NHTSAVehicleInfo.fromJson(Map<String, dynamic> json) { | ||
count = json['Count']; | ||
message = json['Message']; | ||
searchCriteria = json['SearchCriteria']; | ||
if (json['Results'] != null) { | ||
results = List<NHTSAResult>(); | ||
json['Results'].forEach((v) { | ||
if (v['Value'] != null) { | ||
results.add(NHTSAResult.fromJson(v)); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
static String normalizeStringValue(String s) { | ||
return s.splitMapJoin(' ', | ||
onNonMatch: (m) => StringUtils.capitalize(m.toLowerCase())); | ||
} | ||
|
||
ExtendedVehicleInfo toExtendedVehicleInfo() { | ||
final ExtendedVehicleInfo info = ExtendedVehicleInfo(); | ||
|
||
results.forEach((f) { | ||
switch (f.variable) { | ||
case "Vehicle Type": | ||
info.vehicleType = normalizeStringValue(f.value); | ||
break; | ||
case "Make": | ||
info.make = normalizeStringValue(f.value); | ||
break; | ||
case "Model": | ||
info.model = normalizeStringValue(f.value); | ||
break; | ||
} | ||
}); | ||
|
||
return info; | ||
} | ||
|
||
@override | ||
String toString() { | ||
return 'NHTSAVehicleInfo[count=$count, message=$message, searchCriteria=$searchCriteria, results=$results]'; | ||
} | ||
} | ||
|
||
class ExtendedVehicleInfo { | ||
String make; | ||
String model; | ||
String vehicleType; | ||
|
||
static Future<ExtendedVehicleInfo> getExtendedVehicleInfo(String vin) async { | ||
var path = 'https://vpic.nhtsa.dot.gov/api//vehicles/DecodeVin/' + vin + '?format=json'; | ||
final response = await http.get(path); | ||
|
||
if (response.statusCode == 200) { | ||
var vehicleInfo = NHTSAVehicleInfo.fromJson(jsonDecode(response.body)); | ||
return vehicleInfo.toExtendedVehicleInfo(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@override | ||
String toString() { | ||
return 'ExtendedVehicleInfo[make=$make, model=$model, vehicleType=$vehicleType]'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
name: vin_decoder | ||
description: | ||
A simple Dart library for decoding and validating Vehicle Identification Numbers (VINs) based on ISO 3779:2009 and | ||
World Manufacturer Identifiers (WMIs) based on ISO 3780:2009. | ||
World Manufacturer Identifiers (WMIs) based on ISO 3780:2009. Further enriched by data obtained from the NHTSA | ||
database. | ||
|
||
version: 0.0.1+1 | ||
version: 0.1.0 | ||
homepage: https://www.github.com/adaptant-labs/vin-decoder-dart | ||
author: Adaptant Labs <[email protected]> | ||
|
||
|
@@ -14,6 +15,8 @@ dependencies: | |
# The Flutter SDK presently depends on v1.1.6 of the meta package, so we artificially | ||
# constrain the version number until this limitation is addressed in a future SDK update. | ||
meta: ^1.1.6 | ||
basic_utils: ^2.0.1 | ||
http: ^0.12.0+2 | ||
|
||
dev_dependencies: | ||
test: ^1.0.0 |