Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Created October 6, 2013 01:34
Show Gist options
  • Select an option

  • Save clouddueling/6848246 to your computer and use it in GitHub Desktop.

Select an option

Save clouddueling/6848246 to your computer and use it in GitHub Desktop.
Example REST service for angularjs
services.factory('Card', function($http, $www){
return {
create: function(inputs) {
return $http.post('/api/v1/card', inputs);
},
delete: function(id) {
return $http.delete('/api/v1/card/' + id);
},
update: function(id, inputs) {
return $http.put('/api/v1/card/' + id, inputs);
},
load: function(id) {
return $http.get('/api/v1/card/' + id);
},
callStats: function(id) {
return $http.get('/api/v1/card/call_stats/' + id);
},
formStats: function(id) {
return $http.get('/api/v1/card/form_stats/' + id);
},
systemStats: function(id) {
return $http.get('/api/v1/card/system_stats/' + id);
},
records: function(id, inputs) {
return $www.get('/api/v1/card/records/' + id, inputs);
},
updateGroupOrder: function(id, inputs) {
return $http.put("/api/v1/card/group_order/" + id, inputs);
},
column_records: function(id, min, max) {
min = min || 0;
max = max || 5;
return $http.get("/api/v1/card/column_records/" + id + "?max=" + max + "&min=" + min)
},
updateConversionOrder: function(id, inputs) {
return $http.put('/api/v1/card/conversion_order/' + id, inputs);
},
updateSettings: function(id, inputs) {
return $http.put('/api/v1/card/settings/' + id, inputs);
},
loadEmailblasts: function(id) {
return $http.get('/api/v1/card/emailblasts/' + id);
},
createEmailblast: function(id, serieId) {
return $http.post('/api/v1/card/emailblast/' + id + '/' + serieId);
},
loadFlyer: function(id) {
return $http.get('/api/v1/card/flyer/' + id);
},
loadPhoneblasts: function(id) {
return $http.get('/api/v1/card/phoneblasts/' + id);
},
createPhoneblast: function(id, serieId) {
return $http.post('/api/v1/card/phoneblast/' + id + '/' + serieId);
},
loadPostcards: function(id) {
return $http.get('/api/v1/card/postcards/' + id);
},
createPostcard: function(id, serieId) {
return $http.post('/api/v1/card/postcard/' + id + '/' + serieId);
},
loadTextblasts: function(id) {
return $http.get('/api/v1/card/textblasts/' + id);
},
createTextblast: function(id, serieId) {
return $http.post('/api/v1/card/textblast/' + id + '/' + serieId);
},
defaultTypes: function(types) {
return _.map($scope.main.types, function(type, key) {
return {
name: type,
value: key
}
});
}
}
});
services.factory('$www', function($http){
function encodeObject(obj) {
var str = "";
for(var p in obj) {
switch(typeof obj[p]) {
case 'boolean' :
str += p + '=' + parseInt(obj[p]) + "&";
break;
case 'number' :
case 'string' :
str += p + "=" + obj[p] + "&";
break;
case 'object' :
for (var i in obj[p]) {
str += p + "[" + encodeURIComponent(i) + "]=" + encodeURIComponent(obj[p][i]) + "&";
}
break;
}
}
return str;
}
return {
delete: function(url, data) {
return $http({
method: 'DELETE',
url: url,
data: data,
});
},
get: function(url, data) {
url = url + "?" + encodeObject(data);
return $http({
method: 'GET',
url: url,
});
},
post: function(url, data) {
return $http({
method: 'POST',
data: data,
url: url,
});
},
put: function(url, data) {
return $http({
method: 'PUT',
data: data,
url: url,
});
},
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment