Home Reference Source Test Repository
public class | source

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:

How to change a user's station using station search
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

Ensures a station ID is associated with a valid NPR station.

Method Summary

Public Methods
public

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

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

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.

Params:

NameTypeAttributeDescription
stationId number | string

The station's ID, which is either an integer or a numeric string (e.g. 123 or '123')

Return:

Promise<Station>

Public Methods

public getStationDetails(stationId: number | string): Promise<Station> source

Returns a Station model for the station with the given ID.

Params:

NameTypeAttributeDescription
stationId number | string

The station's ID, which is either an integer or a numeric string (e.g. 123 or '123')

Return:

Promise<Station>

Test:

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.

Params:

NameTypeAttributeDescription
query null | string
  • optional

An optional query, which can be a station name, network name, or zip code

Return:

Promise<Array<Station>>

Test:

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:

NameTypeAttributeDescription
city string

A full city name (e.g. "New York", "San Francisco", "Phoenix")

Return:

Promise<Array<Station>>

Test:

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.

Params:

NameTypeAttributeDescription
city string

A full city name (e.g. "New York", "San Francisco", "Phoenix")

state string

A state name (e.g. "Maryland") or abbreviation (e.g. "MD")

Return:

Promise<Array<Station>>

Test:

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.

Params:

NameTypeAttributeDescription
lat number

A float representing the latitude value of the geographic coordinates

long number

A float representing the longitude value of the geographic coordinates

Return:

Promise<Array<Station>>

Test:

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:

NameTypeAttributeDescription
state string

A state name (e.g. "Maryland") or abbreviation (e.g. "MD")

Return:

Promise<Array<Station>>

Test: