StationFinder
Encapsulates all of the logic for communication with the Station Finder Service in the NPR One API.
Note that consumers should not be accessing this class directly but should instead use the provided pass-through functions in the main NprOneSDK class.
Example:
const nprOneSDK = new NprOneSDK();
nprOneSDK.config = { ... };
nprOneSDK.getUser() // optional; verifies that you have a logged-in user
.then(() => {
return nprOneSDK.searchStations('wnyc');
})
.then((stations) => {
const stationId = stations[0].id; // in reality, you'd probably have the user select a station from returned search results
nprOneSDK.setUserStation(stationId);
});
Test:
Static Method Summary
Static Public Methods | ||
public static |
validateStation(stationId: number | string): Promise<Station> Ensures a station ID is associated with a valid NPR station. |
Method Summary
Public Methods | ||
public |
getStationDetails(stationId: number | string): Promise<Station> Returns a Station model for the station with the given ID. |
|
public |
Performs a general search of all NPR One stations, using an optional query. |
|
public |
searchStationsByCity(city: string): Promise<Array<Station>> Performs a geographic search of all NPR One stations using a city name only. |
|
public |
Performs a geographic search of all NPR One stations using a city name and state. |
|
public |
Performs a geographic search of all NPR One stations using a passed-in pair of lat-long coordinates. |
|
public |
searchStationsByState(state: string): Promise<Array<Station>> Performs a geographic search of all NPR One stations using a state name or abbreviation only. |
Static Public Methods
public static validateStation(stationId: number | string): Promise<Station> source
Ensures a station ID is associated with a valid NPR station. While this technically returns the raw JSON for this station if it exists, these results are not meant to be consumed directly; if you need the station details to display to your end-user, use getStationDetails instead.
Public Methods
public getStationDetails(stationId: number | string): Promise<Station> source
Returns a Station model for the station with the given ID.
public searchStations(query: null | string): Promise<Array<Station>> source
Performs a general search of all NPR One stations, using an optional query. If passed in, the query can be any string, such as an address or keyword, but the recommended usage is to search for station names, network names (e.g. "Colorado Public Radio" is a network of stations throughout Colorado), or zip codes.
If no query is passed in, this function will return a list of one or more stations geographically closest to the client based on the consumer's IP address. If you are running this in a server-side environment where the IP address of the server is not necessarily the same as the IP address (and, by extension, the geographic location) of the end-user, you can use searchStationsByLatLongCoordinates instead to retrieve a list of stations geographically closest to the end-user.
public searchStationsByCity(city: string): Promise<Array<Station>> source
Performs a geographic search of all NPR One stations using a city name only. It is generally recommended that you use searchStationsByCityAndState instead, as it will return more accurate results and is the recommended function for clients wanting to offer a location search.
Params:
Name | Type | Attribute | Description |
city | string | A full city name (e.g. "New York", "San Francisco", "Phoenix") |
public searchStationsByCityAndState(city: string, state: string): Promise<Array<Station>> source
Performs a geographic search of all NPR One stations using a city name and state. While you can pass in a city
or state to searchStations as the query, searchStationsByCityAndState()
will return more accurate
results and is the recommended function for clients wanting to offer a location search.
public searchStationsByLatLongCoordinates(lat: number, long: number): Promise<Array<Station>> source
Performs a geographic search of all NPR One stations using a passed-in pair of lat-long coordinates. In most cases, this means you will need to first use the HTML5 Geolocation API or a similar library in order to obtain the lat-long coordinates for the end-user's location.
Note that searchStations without a query will already produce a list of stations closest to the user if
this code is being run in a client-side environment and/or the IP address of the device making the calls to this
SDK has the same geographic location as the end-user. For that reason, searchStationsByLatLongCoordinates()
is really only needed if this SDK is being run in a server-side environment.
public searchStationsByState(state: string): Promise<Array<Station>> source
Performs a geographic search of all NPR One stations using a state name or abbreviation only. It is generally recommended that you use searchStationsByCityAndState instead, as it will return more accurate results and is the recommended function for clients wanting to offer a location search.
Params:
Name | Type | Attribute | Description |
state | string | A state name (e.g. "Maryland") or abbreviation (e.g. "MD") |