-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests that ensure Media appends auth tokens to urls. Also fixed the ping functionality so the app now actually retrieves a token. Also enhanced $url so that it converts QSPs into ruby friendly casing.
- Loading branch information
Showing
9 changed files
with
275 additions
and
168 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,82 @@ | ||
angular.module("baw.models.media", []).factory("baw.models.Media", ["conf.paths", function(paths) { | ||
|
||
function Media(resource) { | ||
if (!(this instanceof Media)) { | ||
throw new Error("Constructor called as a function"); | ||
} | ||
|
||
if (!angular.isObject(resource)) { | ||
throw "Media must be constructed with a valid resource."; | ||
} | ||
|
||
angular.extend(this, resource); | ||
|
||
// convert the datetime | ||
this.recording.recordedDate = new Date(this.recording.recordedDate); | ||
|
||
// alias common parameters | ||
this.startOffset = this.commonParameters.startOffset; | ||
this.endOffset = this.commonParameters.endOffset; | ||
this.recordedDate = this.recording.recordedDate; | ||
this.id = this.recording.id; | ||
this.durationSeconds = this.recording.durationSeconds; | ||
if (angular.isNumber(resource.commonParameters.sampleRate)) { | ||
this.sampleRate = resource.commonParameters.sampleRate; | ||
} | ||
else { | ||
throw "The provided sample rate for the Media json must be a number!"; | ||
} | ||
|
||
|
||
/** | ||
* Change relative image and audio urls into absolute urls | ||
* @param {Media=} mediaItemToFix | ||
*/ | ||
this.formatPaths = function formatPaths(mediaItemToFix) { | ||
var mediaItem = mediaItemToFix || this; | ||
var imgKeys = Object.keys(mediaItem.available.image); | ||
if (imgKeys.length > 1) { | ||
throw "don't know how to handle more than one image format!"; | ||
} | ||
|
||
var imageKey = imgKeys[0]; | ||
var imageFormat = mediaItem.available.image[imageKey]; | ||
mediaItem.available.image[imageKey].url = paths.joinFragments(paths.api.root, imageFormat.url); | ||
mediaItem.spectrogram = imageFormat; | ||
|
||
// make the order explicit (ng-repeat alphabetizes the order >:-| | ||
mediaItem.available.audioOrder = []; | ||
angular.forEach(mediaItem.available.audio, function (value, key) { | ||
// just update the url so it is an absolute uri | ||
this[key].url = paths.joinFragments(paths.api.root, value.url); | ||
|
||
mediaItem.available.audioOrder.push(key); | ||
|
||
}, mediaItem.available.audio); | ||
}; | ||
|
||
this.formatPaths(); | ||
} | ||
|
||
Media.make = function(arg) { | ||
return new Media(arg); | ||
}; | ||
|
||
return Media; | ||
}]); | ||
angular | ||
.module("baw.models.media", ["bawApp.services"]) | ||
.factory( | ||
"baw.models.Media", | ||
["conf.paths", | ||
"Authenticator", | ||
"$url", | ||
function (paths, Authenticator, url) { | ||
|
||
function Media(resource) { | ||
if (!(this instanceof Media)) { | ||
throw new Error("Constructor called as a function"); | ||
} | ||
|
||
if (!angular.isObject(resource)) { | ||
throw "Media must be constructed with a valid resource."; | ||
} | ||
|
||
angular.extend(this, resource); | ||
|
||
// convert the datetime | ||
this.recording.recordedDate = new Date(this.recording.recordedDate); | ||
|
||
// alias common parameters | ||
this.startOffset = this.commonParameters.startOffset; | ||
this.endOffset = this.commonParameters.endOffset; | ||
this.recordedDate = this.recording.recordedDate; | ||
this.id = this.recording.id; | ||
this.durationSeconds = this.recording.durationSeconds; | ||
if (angular.isNumber(resource.commonParameters.sampleRate)) { | ||
this.sampleRate = resource.commonParameters.sampleRate; | ||
} | ||
else { | ||
throw "The provided sample rate for the Media json must be a number!"; | ||
} | ||
|
||
|
||
/** | ||
* Change relative image and audio urls into absolute urls. | ||
* Also appends auth tokens onto urls. | ||
* @param {Media=} mediaItemToFix | ||
*/ | ||
this.formatPaths = function formatPaths(mediaItemToFix) { | ||
var mediaItem = mediaItemToFix || this; | ||
var imgKeys = Object.keys(mediaItem.available.image); | ||
if (imgKeys.length > 1) { | ||
throw "don't know how to handle more than one image format!"; | ||
} | ||
|
||
var imageKey = imgKeys[0]; | ||
var imageFormat = mediaItem.available.image[imageKey]; | ||
var fullUrl = paths.joinFragments(paths.api.root, imageFormat.url); | ||
mediaItem.available.image[imageKey].url = url.formatUri(fullUrl, {userToken: Authenticator.authToken}); | ||
|
||
mediaItem.spectrogram = imageFormat; | ||
|
||
// make the order explicit (ng-repeat alphabetizes the order >:-| | ||
mediaItem.available.audioOrder = []; | ||
angular.forEach(mediaItem.available.audio, function (value, key) { | ||
// just update the url so it is an absolute uri | ||
var fullUrl = paths.joinFragments(paths.api.root, value.url); | ||
|
||
// also add auth token | ||
this[key].url = url.formatUri(fullUrl, {userToken: Authenticator.authToken}); | ||
|
||
mediaItem.available.audioOrder.push(key); | ||
|
||
}, mediaItem.available.audio); | ||
|
||
var jsonFullUrl = paths.joinFragments(paths.api.root, mediaItem.available.text["json"].url); | ||
mediaItem.available.text["json"].url = url.formatUri(jsonFullUrl, {userToken: Authenticator.authToken}); | ||
}; | ||
|
||
this.formatPaths(); | ||
} | ||
|
||
Media.make = function (arg) { | ||
return new Media(arg); | ||
}; | ||
|
||
return Media; | ||
}]); |
This file contains 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
Oops, something went wrong.