jam-cloud/app/assets/javascripts/jam_rest.js

173 lines
4.6 KiB
JavaScript

(function(context,$) {
/**
* Javascript wrappers for the REST API
*/
"use strict";
context.JK = context.JK || {};
context.JK.Rest = function() {
var self = this;
var logger = context.JK.logger;
function updateSession(id, newSession, onSuccess) {
logger.debug('Rest.updateSession');
return $.ajax('/api/sessions/' + id, {
type: "PUT",
data : newSession,
dataType : 'json',
success: onSuccess
});
}
function getUserDetail(options) {
var id = getId(options);
var url = "/api/users/" + id;
return $.ajax({
type: "GET",
dataType: "json",
url: url,
async: true,
processData: false
});
}
function getCities (options) {
var country = options['country']
var region = options['region']
return $.ajax('/api/cities', {
data : { country: country, region: region },
dataType : 'json'
});
}
function getRegions(options) {
var country = options["country"]
return $.ajax('/api/regions', {
data : { country: country},
dataType : 'json'
});
}
function getIsps(options) {
var country = options["country"]
return $.ajax('/api/isps', {
data : { country: country},
dataType : 'json'
});
}
function getInstruments(options) {
return $.ajax('/api/instruments', {
data : { },
dataType : 'json'
});
}
function updateAvatar(options) {
var id = getId(options);
var original_fpfile = options['original_fpfile'];
var cropped_fpfile = options['cropped_fpfile'];
var crop_selection = options['crop_selection'];
var url = "/api/users/" + id + "/avatar";
return $.ajax({
type: "POST",
dataType: "json",
url: url,
contentType: 'application/json',
processData:false,
data: JSON.stringify({
original_fpfile : original_fpfile,
cropped_fpfile : cropped_fpfile,
crop_selection : crop_selection
})
});
}
function deleteAvatar(options) {
var id = getId(options);
var url = "/api/users/" + id + "/avatar";
return $.ajax({
type: "DELETE",
dataType: "json",
url: url,
contentType: 'application/json',
processData:false
});
}
function getFilepickerPolicy(options) {
var id = getId(options);
var handle = options && options["handle"];
var convert = options && options["convert"]
var url = "/api/users/" + id + "/filepicker_policy";
return $.ajax(url, {
data : { handle : handle, convert: convert },
dataType : 'json'
});
}
function getFriends(options) {
var id = getId(options);
return $.ajax({
type: "GET",
url: '/api/users/' + id + '/friends',
dataType: 'json'
});
}
function getClientDownloads(options) {
return $.ajax({
type: "GET",
url: '/api/artifacts/clients',
dataType: 'json'
});
}
function getId(options) {
var id = options && options["id"]
if(!id) {
id = context.JK.currentUserId;
}
return id;
}
function initialize() {
return self;
}
// Expose publics
this.initialize = initialize;
this.getUserDetail = getUserDetail;
this.getCities = getCities;
this.getRegions = getRegions;
this.getIsps = getIsps;
this.getInstruments = getInstruments;
this.updateAvatar = updateAvatar;
this.deleteAvatar = deleteAvatar;
this.getFilepickerPolicy = getFilepickerPolicy;
this.getFriends = getFriends;
this.updateSession = updateSession;
this.getClientDownloads = getClientDownloads
return this;
};
})(window,jQuery);