angular.module('apiMock', [])

.config(function ($httpProvider) {
  $httpProvider.interceptors.push('httpInterceptor');
})

.factory('apiMock', function($location) {
  return {
    shouldReplace: function (req, apiPath) {
      return req.url.indexOf(apiPath) === 0;
    },

    replacePath: function (req, apiPath, mockDataPath) {
      var path = req.url.substring(apiPath.length);
      req.url = mockDataPath + path + '.' + req.method.toLowerCase() + '.json';
    },

    isMocking: function() {
      var regex = /apimock/i;
      var found = false;

      angular.forEach($location.search(), function(value, key) {
        if (regex.test(key)) {
          // Update $location object with primitive boolean compatibility in case if string type.
          if (value === true || angular.lowercase(value) === 'true') {
            found = true;
            $location.search(key, null);
            $location.search('apimock', true);
          }
        }
      });

      return found;
    }
  };
})

.provider('httpInterceptor', function() {
  var config = {
    mockDataPath: '/mock_data',
    apiPath: '/api'
  };

  this.config = function (options) {
    angular.extend(config, options);
  };

  function HttpInterceptor($q, apiMock) {
    this.request = function (req) {
      if (req && apiMock.isMocking() && apiMock.shouldReplace(req, config.apiPath)) {
        apiMock.replacePath(req, config.apiPath, config.mockDataPath);
      }

      return req || $q.when(req);
    };
  }

  this.$get = function ($q, apiMock) {
    return new HttpInterceptor($q, apiMock);
  };
});

@@ -122,15 +122,16 @@
 )%0A%0A.
-factory
+provider
 ('ap
@@ -151,79 +151,62 @@
 ion(
-$location) %7B%0A  return %7B%0A    shouldReplace: function (req, apiPath
+) %7B%0A  var $location;%0A%0A  function shouldReplace(req
 ) %7B%0A
-  
     
@@ -228,16 +228,23 @@
 indexOf(
+config.
 apiPath)
@@ -257,31 +257,13 @@
 ;%0A  
-  %7D,
+%7D
 %0A%0A  
- 
- replacePath: 
 func
@@ -271,43 +271,31 @@
 ion 
-(
 re
-q, apiPath, mockDataPath
+placePath(req
 ) %7B%0A    
   va
@@ -286,26 +286,24 @@
 (req) %7B%0A    
-  
 var path = r
@@ -319,16 +319,23 @@
 bstring(
+config.
 apiPath.
@@ -347,18 +347,16 @@
 h);%0A    
- 
- 
 req.url 
@@ -357,16 +357,23 @@
 q.url = 
+config.
 mockData
@@ -434,17 +434,21 @@
 ;%0A  
- 
- %7D,
+%7D
 %0A%0A  
- 
+function
  isM
@@ -457,25 +457,13 @@
 king
-: function
 () %7B%0A
-  
     
@@ -490,18 +490,16 @@
 /i;%0A    
-  
 var foun
@@ -510,18 +510,16 @@
 false;%0A%0A
-  
     angu
@@ -575,26 +575,24 @@
 ey) %7B%0A      
- 
- 
 if (regex.te
@@ -598,26 +598,24 @@
 est(key)) %7B%0A
-  
         // U
@@ -698,18 +698,16 @@
 g type.%0A
-  
         
@@ -777,18 +777,16 @@
         
-  
 found = 
@@ -791,18 +791,16 @@
 = true;%0A
-  
         
@@ -832,34 +832,32 @@
 ull);%0A          
-  
 $location.search
@@ -879,30 +879,26 @@
 e);%0A        
-  %7D%0A  
+%7D%0A
       %7D%0A    
@@ -901,17 +901,13 @@
     
-  
 %7D);%0A%0A
-  
     
@@ -926,62 +926,10 @@
 ;%0A  
-  %7D%0A  %7D;%0A%7D)%0A%0A.provider('httpInterceptor', function() %7B
+%7D%0A
 %0A  v
@@ -997,94 +997,407 @@
 api'
-%0A  %7D;%0A%0A  this.config = function (options) %7B%0A    angular.extend(config, options);%0A  %7D;%0A
+,%0A    shouldReplace: shouldReplace,%0A    replacePath: replacePath,%0A    isMocking: isMocking%0A  %7D;%0A%0A  function ApiMock(_$location) %7B%0A    angular.extend(this, config);%0A    $location = _$location;%0A  %7D%0A%0A  this.config = function (options) %7B%0A    angular.extend(config, options);%0A  %7D;%0A%0A  this.$get = function ($location) %7B%0A    return new ApiMock($location);%0A  %7D;%0A%7D)%0A%0A.provider('httpInterceptor', function() %7B
 %0A  f
@@ -1536,32 +1536,16 @@
 lace(req
-, config.apiPath
 )) %7B%0A   
@@ -1576,45 +1576,8 @@
 (req
-, config.apiPath, config.mockDataPath
 );%0A 

Refactor apiMock service to support issue #5.