Skip to content

Instantly share code, notes, and snippets.

@ghankerson
Last active August 29, 2015 14:04
Show Gist options
  • Save ghankerson/b8ccf3167a3db0911f44 to your computer and use it in GitHub Desktop.
Save ghankerson/b8ccf3167a3db0911f44 to your computer and use it in GitHub Desktop.
/**
* @name nowplaying_ws.js
*/
/**
* @name APMNowPlaying
* @description protoype pattern to allow
* @class
*/
var APMNowPlaying = function(websocketurl) {
this.subscription = new Faye.Client(websocket_url);
this.config = {
apm_player_track_info: 'apm_player_track_info',
apm_player_track_info_selector: '#' + this.config.apm_player_track_info
};
};
APMNowPlaying.prototype =
{
/**
* @name getInstance
* @description return a Single Instance of APMNowPlaying
*/
getInstance = function() {
if( ! instance ) {
instance = new APMNowPlaying();
}
return instance;
}
/**
* @name subscribe
* @param {string} station_id - The radio station you want to monitor, i.e., '/the-current'
* @description format now playing markup and display it
*/
subscribe: function (station_id) {
if (this.station_id) {
this.subscription.unsubscribe(this.station_id);
}
this.station_id = station_id;
this.subscription.subscribe(station_id, function (message) {
this.songMetadata = JSON.parse(message.text);
this.showNowPlaying();
}, this);
},
/**
* @name showNowPlaying
* @description format now playing markup and display it
*/
showNowPlaying: function () {
if (this.songMetadata) {
var song = this.songMetadata.songs[0];
var title, artist, album;
if (song.title !== '') {
title = '<div class="apm-title">' + song.title + '</div>';
}
if (song.artist !== '') {
artist = '<div class="apm-artist">' + song.artist + '</div>';
}
if (song.album !== '') {
album = '<div class="apm-album">' + song.album + '</div>';
}
var track_info = '<div id="apm_player_track_information">' + title + artist + album + '</div>';
$(this.config.apm_player_track_info_selector).fadeOut("slow", function () {
$(this.config.apm_player_track_info_selector).html(track_info);
$(this.config.apm_player_track_info_selector).fadeIn("slow");
});
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment