Last active
August 29, 2015 14:05
-
-
Save wesleytodd/34fd31e9822e6ee567ee to your computer and use it in GitHub Desktop.
An example of hydrating a media url from an api response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mockVideo = { | |
title: 'My Awesome Video', | |
public_id: 'abc123', | |
media: { | |
_links: { | |
thumbnail: { | |
template: 'http://frame.thestaticvube.com/snap/{width}x{height}/{public_id}.jpg;t={timestamp}' | |
}, | |
video: { | |
template: 'http://video.thestaticvube.com/video/{resolution_id}/{public_id}.mp4' | |
} | |
}, | |
thumbnail: [{ | |
width: 300, | |
height: 250, | |
timestamp: 25.123, | |
transcoding_stauts: 'processed', | |
}, { | |
width: 500, | |
height: 400, | |
timestamp: 25.123, | |
transcoding_stauts: 'processed', | |
}, { | |
width: 300, | |
height: 250, | |
timestamp: 123.123, | |
transcoding_stauts: 'processed', | |
}, { | |
width: 500, | |
height: 400, | |
timestamp: 123.123, | |
transcoding_stauts: 'processed', | |
}], | |
video: [{ | |
width: 300, | |
height: 250, | |
bitrate: 300000, | |
resolution_id: 2, | |
transcoding_status: 'processed' | |
}] | |
} | |
}; | |
function hydrateTemplate(tmpl, mediaData) { | |
for (var i in mediaData) { | |
tmpl = tmpl.replace(new RegExp('{' + i + '}', 'g'), mediaData[i]); | |
} | |
return tmpl; | |
}; | |
function mediaUrl(video, type, index) { | |
var d = util.extend({}, video, video.media[type][index]); | |
return hydrateTemplate(video.media._links[type].template, d); | |
}; | |
mediaUrl(mockVideo, 'thumbnail', 1); // http://frame.thestaticvube.com/snap/300x250/abc123.jpg;t=25.123 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment