(function(){
var mod = angular.module('caspy.api', ['ngResource', 'caspy.server']);

mod.config(['$resourceProvider', 
    function($resourceProvider) {
        $resourceProvider.defaults.stripTrailingSlashes = false;
    }]
);

mod.factory('caspyAPI',
    ['$q', '$http', '$resource', 'Constants',
    function($q, $http, $resource, Constants) {
        var api = {
            root: null
            , resources: {}

            , resolve: function(name) {
                var d = $q.defer();
                if (typeof api.root[name] === 'undefined') {
                    d.reject(new Error(name + ' endpoint not available'));
                }
                else {
                    d.resolve(api.root[name]);
                }
                return d.promise;
            }

            , get_endpoint: function(name) {
                if (api.root)
                    return api.resolve(name);
                return $http.get(Constants.apiRootUrl)
                    .then(function(response) {
                            api.root = response.data;
                            return api.resolve(name);
                    })
            }

            , get_resource: function(name) {
                if (typeof api.resources[name] !== 'undefined')
                    return api.resources[name];
                return api.resources[name] = api.get_endpoint(name)
                        .then(api.build_resource);
            }

            , build_resource: function(endpoint) {
                return $resource(endpoint);
            }

        };
        return api;
    }]
);
})();

@@ -426,22 +426,27 @@
       , 
+get_
 reso
-lv
+urc
 e: funct
@@ -461,44 +461,8 @@
 ) %7B%0A
-                var d = $q.defer();%0A
     
@@ -493,19 +493,24 @@
 pi.r
-oot
+esources
 %5Bname%5D 
-=
+!
 == '
@@ -520,18 +520,16 @@
 efined')
- %7B
 %0A       
@@ -545,150 +545,103 @@
     
-d.reject(new Error(name + ' endpoint not available'));%0A                %7D%0A                else %7B%0A                    d.resolve(api.root%5B
+return api.resources%5Bname%5D;%0A                return api.resources%5Bname%5D = api.get_endpoint(
 name
-%5D);
+)
 %0A   
@@ -657,18 +657,16 @@
     
-%7D%0A
         
     
@@ -665,32 +665,33 @@
     
-        return d.promise
+.then(api.build_resource)
 ;%0A  
@@ -1078,35 +1078,37 @@
 %0A%0A            , 
-get
+build
 _resource: funct
@@ -1103,36 +1103,40 @@
 ource: function(
-name
+endpoint
 ) %7B%0A            
@@ -1143,55 +1143,35 @@
     
-if (typeof api.resources%5Bname%5D !== 'undefined')
+return $resource(endpoint);
 %0A   
@@ -1168,32 +1168,38 @@
 oint);%0A         
+   %7D%0A%0A
            retur
@@ -1197,35 +1197,36 @@
     
-return api.resources%5B
+ , resolve: function(
 name
-%5D;
+) %7B
 %0A   
@@ -1242,108 +1242,161 @@
     
-return api.resources%5Bname%5D = api.get_endpoint(name)%0A                        .then(api.build_resource
+var d = $q.defer();%0A                if (typeof api.root%5Bname%5D === 'undefined') %7B%0A                    d.reject(new Error(name + ' endpoint not available')
 );%0A 
@@ -1398,34 +1398,37 @@
 ));%0A            
+  
+  
 %7D
-%0A
 %0A            , b
@@ -1428,46 +1428,83 @@
     
-, build_resource: function(endpoint) %7B
+    else %7B%0A                    d.resolve(api.root%5Bname%5D);%0A                %7D
 %0A   
@@ -1523,35 +1523,25 @@
  return 
-$resource(endpoint)
+d.promise
 ;%0A      

Put higher level functions first