Create Geocoder abstraction
This commit is contained in:
11
routes/geocoders/Geocoder.ts
Normal file
11
routes/geocoders/Geocoder.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { GeoCoordinates } from "../../types";
|
||||||
|
|
||||||
|
export abstract class Geocoder {
|
||||||
|
/**
|
||||||
|
* Converts a location name to geographic coordinates.
|
||||||
|
* @param location A location name.
|
||||||
|
* @return A Promise that will be resolved with the GeoCoordinates of the specified location, or rejected with a
|
||||||
|
* CodedError.
|
||||||
|
*/
|
||||||
|
public abstract geocodeLocation( location: string ): Promise<GeoCoordinates>;
|
||||||
|
}
|
||||||
31
routes/geocoders/WUnderground.ts
Normal file
31
routes/geocoders/WUnderground.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { GeoCoordinates } from "../../types";
|
||||||
|
import { CodedError, ErrorCode } from "../../errors";
|
||||||
|
import { httpJSONRequest } from "../weather";
|
||||||
|
import { Geocoder } from "./Geocoder";
|
||||||
|
|
||||||
|
export default class WUnderground extends Geocoder {
|
||||||
|
public async geocodeLocation( location: string ): Promise<GeoCoordinates> {
|
||||||
|
// Generate URL for autocomplete request
|
||||||
|
const url = "http://autocomplete.wunderground.com/aq?h=0&query=" +
|
||||||
|
encodeURIComponent( location );
|
||||||
|
|
||||||
|
let data;
|
||||||
|
try {
|
||||||
|
data = await httpJSONRequest( url );
|
||||||
|
} catch ( err ) {
|
||||||
|
// If the request fails, indicate no data was found.
|
||||||
|
throw new CodedError( ErrorCode.LocationServiceApiError );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the data is valid
|
||||||
|
if ( typeof data.RESULTS === "object" && data.RESULTS.length && data.RESULTS[ 0 ].tz !== "MISSING" ) {
|
||||||
|
|
||||||
|
// If it is, reply with an array containing the GPS coordinates
|
||||||
|
return [ parseFloat( data.RESULTS[ 0 ].lat ), parseFloat( data.RESULTS[ 0 ].lon ) ];
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// Otherwise, indicate no data was found
|
||||||
|
throw new CodedError( ErrorCode.NoLocationFound );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,9 +14,11 @@ import ZimmermanAdjustmentMethod from "./adjustmentMethods/ZimmermanAdjustmentMe
|
|||||||
import RainDelayAdjustmentMethod from "./adjustmentMethods/RainDelayAdjustmentMethod";
|
import RainDelayAdjustmentMethod from "./adjustmentMethods/RainDelayAdjustmentMethod";
|
||||||
import EToAdjustmentMethod from "./adjustmentMethods/EToAdjustmentMethod";
|
import EToAdjustmentMethod from "./adjustmentMethods/EToAdjustmentMethod";
|
||||||
import { CodedError, ErrorCode, makeCodedError } from "../errors";
|
import { CodedError, ErrorCode, makeCodedError } from "../errors";
|
||||||
|
import { Geocoder } from "./geocoders/Geocoder";
|
||||||
|
|
||||||
const WEATHER_PROVIDER: WeatherProvider = new ( require("./weatherProviders/" + ( process.env.WEATHER_PROVIDER || "OWM" ) ).default )();
|
const WEATHER_PROVIDER: WeatherProvider = new ( require("./weatherProviders/" + ( process.env.WEATHER_PROVIDER || "OWM" ) ).default )();
|
||||||
const PWS_WEATHER_PROVIDER: WeatherProvider = new ( require("./weatherProviders/" + ( process.env.PWS_WEATHER_PROVIDER || "WUnderground" ) ).default )();
|
const PWS_WEATHER_PROVIDER: WeatherProvider = new ( require("./weatherProviders/" + ( process.env.PWS_WEATHER_PROVIDER || "WUnderground" ) ).default )();
|
||||||
|
const GEOCODER: Geocoder = new ( require("./geocoders/" + ( process.env.GEOCODER || "WUnderground" ) ).default )();
|
||||||
|
|
||||||
// Define regex filters to match against location
|
// Define regex filters to match against location
|
||||||
const filters = {
|
const filters = {
|
||||||
@@ -55,28 +57,7 @@ export async function resolveCoordinates( location: string ): Promise< GeoCoordi
|
|||||||
const split: string[] = location.split( "," );
|
const split: string[] = location.split( "," );
|
||||||
return [ parseFloat( split[ 0 ] ), parseFloat( split[ 1 ] ) ];
|
return [ parseFloat( split[ 0 ] ), parseFloat( split[ 1 ] ) ];
|
||||||
} else {
|
} else {
|
||||||
// Generate URL for autocomplete request
|
return GEOCODER.geocodeLocation( location );
|
||||||
const url = "http://autocomplete.wunderground.com/aq?h=0&query=" +
|
|
||||||
encodeURIComponent( location );
|
|
||||||
|
|
||||||
let data;
|
|
||||||
try {
|
|
||||||
data = await httpJSONRequest( url );
|
|
||||||
} catch (err) {
|
|
||||||
// If the request fails, indicate no data was found.
|
|
||||||
throw new CodedError( ErrorCode.LocationServiceApiError );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the data is valid
|
|
||||||
if ( typeof data.RESULTS === "object" && data.RESULTS.length && data.RESULTS[ 0 ].tz !== "MISSING" ) {
|
|
||||||
|
|
||||||
// If it is, reply with an array containing the GPS coordinates
|
|
||||||
return [ parseFloat( data.RESULTS[ 0 ].lat ), parseFloat( data.RESULTS[ 0 ].lon ) ];
|
|
||||||
} else {
|
|
||||||
|
|
||||||
// Otherwise, indicate no data was found
|
|
||||||
throw new CodedError( ErrorCode.NoLocationFound );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user