Skip to content

Instantly share code, notes, and snippets.

@hedgerh
Last active August 29, 2015 14:02
Show Gist options
  • Save hedgerh/840dfebb71c7c3277b5b to your computer and use it in GitHub Desktop.
Save hedgerh/840dfebb71c7c3277b5b to your computer and use it in GitHub Desktop.
/**
* This is an example of the results from requesting
* a user stream from Soundcloud's API.
*
* The objects in the collection are a mix of tracks and playlists
* sorted by date.
*
* @type {Array}
*/
var items = [
// this is a track item
{
"type": "track",
"created_at": "2014/06/16 22:29:09 +0000",
"track": {
"id": 154591682,
"created_at": "2014/06/16 11:33:38 +0000",
"user_id": 46059856,
},
"playlist": null
},
// this a playlist
{
"type": "playlist_repost",
"track": null,
"playlist": {
"id": 40585650,
"created_at": "2014/06/20 16:22:58 +0000",
"permalink_url": "http://soundcloud.com/rudeluv/sets/coders-special",
"user": {
"id": 315279,
},
"tracks": [{
// collection of track objects
}]
}
}
];
/**
* I'm using Angular's $q service to return a promise in the Soundcloud.get function
* This is the function that is in the .this() after Soundcloud.get is called.
*
* I need to take the playlist and track fields up one level and add a 'reposted' boolean
* property to the object.
*
* @param {[type]} items
* @return {[type]}
*/
function makeStream() {
var new_item;
var tracks = [];
var playlists = [];
_.each(items, function(item) {
new_item.is_repost = (item.type.slice(-6) === 'repost');
if (item.type === 'playlist' || item.type === 'playlist_repost') {
new_item = item.playlist;
playlists.push(new_item);
} else {
new_item = item.track;
tracks.push(new_item);
}
});
return {'playlists': playlists, 'tracks':tracks};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment