Home Reference Source Test Repository
import Recommendation from '@npr/npr-one-sdk/model/recommendation.js'
public class | source

Recommendation

Extends:

CollectionDoc → Recommendation

Container class for all metadata pertaining to a recommendation.

Provides metadata and the recordAction method, which sends feedback on user actions to NPR's APIs and advances the flow of audio recommendations to the user

Test:

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public

The metadata used to describe this recommendation, such as type and title

public

The actual audio files associated with this recommendation; should never be empty

public

This is the action array from the API within links, and NOT this SDK's notion of Action

public

A list of images associated with this recommendation; could be empty

public

A list of API calls to make if this recommendation is of type sponsorship and the consuming client has played the accompanying audio

public

A list of links that are used as the canonical link(s) when sharing this story on social media

public

An internal store of ratings collected for this application; should never be accessed directly by consumers

public

A list of API calls the app can make to retrieve subsequent recommendations; should never be accessed directly by consumers

public

A list of API calls to make if this recommendation is of type sponsorship and the consuming client has chosen to interact with the sponsorship item using the contents of relateds

public

A list of links to places where the app can take the user if they interact with this sponsorship item

public

A list of links to other places where this story can be found on the web (for example, on NPR.org); could be empty

Method Summary

Public Methods
public

This method looks through the recommendation's action and related array to search for any URL starting with 'nprone://listen'. If found, everything from the query params is appended to the original recommendation URL. This value is then used anytime a user indicates they want more similar stories by clicking or tapping on this recommendation.

For many recommendations, this will not exist and getRecommendationUrl is used instead.

public

Returns the actual audio files associated with this recommendation

public

This is the action array from the API within links, and NOT this SDK's notion of Action

An example of what might be contained within this is array is a link to full-length content for a promo recommendation.

public

Returns a list of images associated with this recommendation

public

Returns a list of API calls to make if this recommendation is of type sponsorship and the consuming client has played the accompanying audio.

public

Returns a list of links that are used as the canonical link(s) when sharing this story on social media.

public

Returns an internal store of ratings collected for this application.

public

Returns the URL that should be used to obtain the next set of recommendations.

public

Returns a list of API calls to make if this recommendation is of type sponsorship and the consuming client has chosen to interact with the sponsorship item using the contents returned by getRelateds.

public

Returns a list of links to places where the app can take the user if they interact with this sponsorship item (such as by clicking/tapping on the image or using a voice command to learn more)

public

Returns a list of links to other places where this story can be found on the web (for example, on NPR.org)

public

Returns whether this recommendation has a given action

public

Returns whether this recommendation has received a rating indicating it is no longer being presented to the user

public

Returns whether this recommendation is shareable on social media

public

Returns whether this recommendation is of type sponsorship

public

recordAction(action: string, elapsedTimeInSeconds: number)

Record a user action taken and the time it was taken against this recommendation

public

A callback which provides for communication of a received rating

public

A convenience function to cast this object back to a string, generally only used by the Logger class.

Protected Methods
protected

Determines whether the collection doc has the required fields for a valid recommendation

Inherited Summary

From class CollectionDoc
public get

Exposed for legacy reasons.

protected
protected

Ensure that the given Collection Doc is valid

Public Constructors

public constructor(json: CollectionDocJSON) source

Override:

CollectionDoc#constructor

Params:

NameTypeAttributeDescription
json CollectionDocJSON

The decoded JSON object that should be used as the basis for this model

Test:

Public Members

public attributes: RecommendationAttributes source

The metadata used to describe this recommendation, such as type and title

public audio: Array<Link> source

The actual audio files associated with this recommendation; should never be empty

public callsToAction: Array<Link> source

This is the action array from the API within links, and NOT this SDK's notion of Action

public images: Array<ImageLink> source

A list of images associated with this recommendation; could be empty

public impressions: Array<FormFactorLink> source

A list of API calls to make if this recommendation is of type sponsorship and the consuming client has played the accompanying audio

public onramps: Array<Link> source

A list of links that are used as the canonical link(s) when sharing this story on social media

public ratings: Array<Rating> source

An internal store of ratings collected for this application; should never be accessed directly by consumers

public recommendations: Array<Link> source

A list of API calls the app can make to retrieve subsequent recommendations; should never be accessed directly by consumers

public relatedImpressions: Array<FormFactorLink> source

A list of API calls to make if this recommendation is of type sponsorship and the consuming client has chosen to interact with the sponsorship item using the contents of relateds

public relateds: Array<FormFactorLink> source

A list of links to places where the app can take the user if they interact with this sponsorship item

public web: Array<Link> source

A list of links to other places where this story can be found on the web (for example, on NPR.org); could be empty

Public Methods

public getActionRecommendationUrl(): string source

This method looks through the recommendation's action and related array to search for any URL starting with 'nprone://listen'. If found, everything from the query params is appended to the original recommendation URL. This value is then used anytime a user indicates they want more similar stories by clicking or tapping on this recommendation.

For many recommendations, this will not exist and getRecommendationUrl is used instead.

Return:

string

Test:

public getAudio(): Array<Link> source

Returns the actual audio files associated with this recommendation

Return:

Array<Link>

Test:

public getCallsToAction(): Array<Link> source

This is the action array from the API within links, and NOT this SDK's notion of Action

An example of what might be contained within this is array is a link to full-length content for a promo recommendation.

Return:

Array<Link>

Test:

public getImages(): Array<ImageLink> source

Returns a list of images associated with this recommendation

Return:

Array<ImageLink>

Test:

public getImpressions(): Array<FormFactorLink> source

Returns a list of API calls to make if this recommendation is of type sponsorship and the consuming client has played the accompanying audio. Note that the SDK will take care of this automatically as long as the client uses recordAction to send the rating.

Test:

public getOnRamps(): Array<Link> source

Returns a list of links that are used as the canonical link(s) when sharing this story on social media.

Return:

Array<Link>

Test:

public getRatings(): Array<Rating> source

Returns an internal store of ratings collected for this application. This should never be accessed directly by consumers; use recordAction to send ratings, and the SDK will figure out the appropriate time to make the API call that submits them to the server.

Return:

Array<Rating>

Test:

public getRecommendationUrl(): string source

Returns the URL that should be used to obtain the next set of recommendations. This should typically not be used by clients directly; use recordAction followed by NprOneSDK#getRecommendation instead.

Return:

string

Test:

public getRelatedImpressions(): Array<FormFactorLink> source

Returns a list of API calls to make if this recommendation is of type sponsorship and the consuming client has chosen to interact with the sponsorship item using the contents returned by getRelateds. Note that the SDK will take care of this automatically as long as the client uses recordAction to send the rating.

Test:

public getRelateds(): Array<FormFactorLink> source

Returns a list of links to places where the app can take the user if they interact with this sponsorship item (such as by clicking/tapping on the image or using a voice command to learn more)

Test:

public getWeb(): Array<Link> source

Returns a list of links to other places where this story can be found on the web (for example, on NPR.org)

Return:

Array<Link>

Test:

public hasAction(action: string): boolean source

Returns whether this recommendation has a given action

Params:

NameTypeAttributeDescription
action string

Which action to look up; should be one of the static string constants returned by Action

Return:

boolean

Test:

public hasEndAction(): boolean source

Returns whether this recommendation has received a rating indicating it is no longer being presented to the user

Return:

boolean

Test:

public isShareable(): boolean source

Returns whether this recommendation is shareable on social media

Return:

boolean

Test:

public isSponsorship(): boolean source

Returns whether this recommendation is of type sponsorship

Return:

boolean

Test:

public recordAction(action: string, elapsedTimeInSeconds: number) source

Record a user action taken and the time it was taken against this recommendation

Params:

NameTypeAttributeDescription
action string

Which action to record; should be one of the static string constants returned by Action

elapsedTimeInSeconds number

The number of seconds this piece of audio has been playing for

Test:

public setRatingReceivedCallback(callback: Function) source

A callback which provides for communication of a received rating

Params:

NameTypeAttributeDescription
callback Function
  • nullable: true

A function to call whenever this recommendation has received a rating (action)

public toString(): string source

A convenience function to cast this object back to a string, generally only used by the Logger class.

Return:

string

Test:

Protected Methods

protected _validate() source

Determines whether the collection doc has the required fields for a valid recommendation

Override:

CollectionDoc#_validate

Throw:

TypeError

if the collection doc is invalid