{"action":"created","comment":{"url":"https://api.github.com/repos/talis/bearhug-angular/pulls/comments/62323797","id":62323797,"diff_hunk":"@@ -0,0 +1,207 @@\n+(function(angular) {\n+    bearhugAuthenticator.$inject = [\"$http\", \"$injector\", \"$q\", \"bearhugStorage\"];\n+    bearhugInterceptor.$inject = [\"$q\", \"$injector\", \"bearhugStorage\"];\n+    bearhugStorage.$inject = [\"bearerUtils\"];\n+    interceptorFilter.$inject = [\"$q\"];\n+    angular.module(\"talis.bearhug\", []);\n+    angular.module(\"talis.bearhug\").factory(\"bearerUtils\", bearerUtils);\n+    function bearerUtils() {\n+        var BEARER_REGEX = /^Bearer (.*)$/;\n+        return {\n+            bearer2token: bearer2token,\n+            token2bearer: token2bearer\n+        };\n+        function bearer2token(bearer) {\n+            var components = angular.isString(bearer) && bearer.match(BEARER_REGEX);\n+            return angular.isArray(components) && components[1] ? components[1] : void 0;\n+        }\n+        function token2bearer(token) {\n+            return angular.isString(token) ? \"Bearer \" + token : void 0;\n+        }\n+    }\n+    angular.module(\"talis.bearhug\").provider(\"bearhug\", BearhugProvider);\n+    function BearhugProvider() {\n+        var options = {\n+            authenticationFunction: null\n+        };\n+        this.setAuthenticationFunction = function(authFunc) {\n+            options.authenticationFunction = angular.isFunction(authFunc) ? authFunc : options.authenticationFunction;\n+        };\n+        this.$get = [\"bearhugAuthenticator\", \"bearhugStorage\", function(bearhugAuthenticator, bearhugStorage) {\n+            var opts = angular.copy(options);\n+            if (!angular.isFunction(opts.authenticationFunction)) {\n+                throw new Error(\"No authentication function specified. How can bearhug authenticate?\");\n+            } else {\n+                return {\n+                    options: opts,\n+                    authenticate: authenticate,\n+                    getToken: bearhugStorage.getToken,\n+                    getBearer: bearhugStorage.getBearer\n+                };\n+            }\n+            function authenticate(requestConfig) {\n+                return bearhugAuthenticator.authenticate(opts, requestConfig);\n+            }\n+        }];\n+    }\n+    angular.module(\"talis.bearhug\").factory(\"bearhugAuthenticator\", bearhugAuthenticator);\n+    function bearhugAuthenticator($http, $injector, $q, bearhugStorage) {\n+        var authenticationDeferred;\n+        return {\n+            authenticate: authenticate\n+        };\n+        function authenticate(options) {\n+            var opts = angular.isObject(options) ? options : {};\n+            if (!angular.isFunction(opts.authenticationFunction)) {\n+                throw new Error(\"No authentication function available to bearhugAuthenticator\");\n+            } else if (authenticationDeferred) {\n+                return authenticationDeferred.promise;\n+            } else {\n+                authenticationDeferred = $q.defer();\n+                var authPromise = opts.authenticationFunction($injector).then(function(tokenFromResponse) {\n+                    bearhugStorage.setToken(tokenFromResponse);\n+                    var token = bearhugStorage.getToken();\n+                    if (token) {\n+                        authenticationDeferred.resolve(token);\n+                    } else {\n+                        authenticationDeferred.reject(new Error(\"Token response is not a string: \" + angular.toJson(tokenFromResponse)));\n+                    }\n+                }).catch(authenticationDeferred.reject);\n+                authPromise.finally(function() {\n+                    authenticationDeferred = void 0;\n+                });\n+                return authenticationDeferred.promise;\n+            }\n+        }\n+    }\n+    angular.module(\"talis.bearhug\").factory(\"bearhugInterceptor\", bearhugInterceptor);\n+    function bearhugInterceptor($q, $injector, bearhugStorage) {\n+        var authenticationDeferred;\n+        return {\n+            request: request,\n+            response: response,\n+            responseError: responseError\n+        };\n+        function request(requestConfig) {\n+            var bearer = bearhugStorage.getBearer();\n+            if (bearer && requestConfig && requestConfig.headers) {\n+                requestConfig.headers.Authorization = bearer;\n+            }\n+            return requestConfig;\n+        }\n+        function response(responseObj) {\n+            var bearer = responseObj && responseObj.headers(\"Authorization\");","path":"dist/bearhug-angular.js","position":93,"original_position":93,"commit_id":"56e842738edf946e87646f7488452ddf5bd70899","original_commit_id":"56e842738edf946e87646f7488452ddf5bd70899","user":{"login":"junglebarry","id":242248,"avatar_url":"https://avatars.githubusercontent.com/u/242248?v=3","gravatar_id":"","url":"https://api.github.com/users/junglebarry","html_url":"https://github.com/junglebarry","followers_url":"https://api.github.com/users/junglebarry/followers","following_url":"https://api.github.com/users/junglebarry/following{/other_user}","gists_url":"https://api.github.com/users/junglebarry/gists{/gist_id}","starred_url":"https://api.github.com/users/junglebarry/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/junglebarry/subscriptions","organizations_url":"https://api.github.com/users/junglebarry/orgs","repos_url":"https://api.github.com/users/junglebarry/repos","events_url":"https://api.github.com/users/junglebarry/events{/privacy}","received_events_url":"https://api.github.com/users/junglebarry/received_events","type":"User","site_admin":false},"body":"It's a [generally accepted style](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md), using function-hoisting to keep public content at the top of files, whilst hiding implementation details lower down.\r\n\r\nI'm not sure how I feel about it, given that methods work top-to-bottom regardless.  I'll happily reformat to a linting standard.  I was going to move to ES2015 and eslint in short order, so that it's settled for the project.","created_at":"2016-05-06T12:34:47Z","updated_at":"2016-05-06T12:34:47Z","html_url":"https://github.com/talis/bearhug-angular/pull/4#discussion_r62323797","pull_request_url":"https://api.github.com/repos/talis/bearhug-angular/pulls/4","_links":{"self":{"href":"https://api.github.com/repos/talis/bearhug-angular/pulls/comments/62323797"},"html":{"href":"https://github.com/talis/bearhug-angular/pull/4#discussion_r62323797"},"pull_request":{"href":"https://api.github.com/repos/talis/bearhug-angular/pulls/4"}}},"pull_request":{"url":"https://api.github.com/repos/talis/bearhug-angular/pulls/4","id":68203359,"html_url":"https://github.com/talis/bearhug-angular/pull/4","diff_url":"https://github.com/talis/bearhug-angular/pull/4.diff","patch_url":"https://github.com/talis/bearhug-angular/pull/4.patch","issue_url":"https://api.github.com/repos/talis/bearhug-angular/issues/4","number":4,"state":"open","locked":false,"title":"60% Moves to use Angular 1.2 HTTP interceptors","user":{"login":"junglebarry","id":242248,"avatar_url":"https://avatars.githubusercontent.com/u/242248?v=3","gravatar_id":"","url":"https://api.github.com/users/junglebarry","html_url":"https://github.com/junglebarry","followers_url":"https://api.github.com/users/junglebarry/followers","following_url":"https://api.github.com/users/junglebarry/following{/other_user}","gists_url":"https://api.github.com/users/junglebarry/gists{/gist_id}","starred_url":"https://api.github.com/users/junglebarry/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/junglebarry/subscriptions","organizations_url":"https://api.github.com/users/junglebarry/orgs","repos_url":"https://api.github.com/users/junglebarry/repos","events_url":"https://api.github.com/users/junglebarry/events{/privacy}","received_events_url":"https://api.github.com/users/junglebarry/received_events","type":"User","site_admin":false},"body":"This moves bearhug along in a number of ways:\r\n\r\n0. Breaks up the single module into smaller files, assembled by a grunt task (`./node_mobules/.bin/grunt`)\r\n0. Adds unit tests around each of the modules (`npm run test`)\r\n0. Removes much of the unused logging code (perhaps we should use `$rootScope.broadcast` instead?)\r\n0. Moves to Angular 1.2 HTTP interceptors.","created_at":"2016-04-28T11:15:21Z","updated_at":"2016-05-06T12:34:47Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4bae4e9baa1420f3030e3bda4e1567f770a3687e","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/talis/bearhug-angular/pulls/4/commits","review_comments_url":"https://api.github.com/repos/talis/bearhug-angular/pulls/4/comments","review_comment_url":"https://api.github.com/repos/talis/bearhug-angular/pulls/comments{/number}","comments_url":"https://api.github.com/repos/talis/bearhug-angular/issues/4/comments","statuses_url":"https://api.github.com/repos/talis/bearhug-angular/statuses/56e842738edf946e87646f7488452ddf5bd70899","head":{"label":"talis:establish-tests","ref":"establish-tests","sha":"56e842738edf946e87646f7488452ddf5bd70899","user":{"login":"talis","id":181416,"avatar_url":"https://avatars.githubusercontent.com/u/181416?v=3","gravatar_id":"","url":"https://api.github.com/users/talis","html_url":"https://github.com/talis","followers_url":"https://api.github.com/users/talis/followers","following_url":"https://api.github.com/users/talis/following{/other_user}","gists_url":"https://api.github.com/users/talis/gists{/gist_id}","starred_url":"https://api.github.com/users/talis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/talis/subscriptions","organizations_url":"https://api.github.com/users/talis/orgs","repos_url":"https://api.github.com/users/talis/repos","events_url":"https://api.github.com/users/talis/events{/privacy}","received_events_url":"https://api.github.com/users/talis/received_events","type":"Organization","site_admin":false},"repo":{"id":27234356,"name":"bearhug-angular","full_name":"talis/bearhug-angular","owner":{"login":"talis","id":181416,"avatar_url":"https://avatars.githubusercontent.com/u/181416?v=3","gravatar_id":"","url":"https://api.github.com/users/talis","html_url":"https://github.com/talis","followers_url":"https://api.github.com/users/talis/followers","following_url":"https://api.github.com/users/talis/following{/other_user}","gists_url":"https://api.github.com/users/talis/gists{/gist_id}","starred_url":"https://api.github.com/users/talis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/talis/subscriptions","organizations_url":"https://api.github.com/users/talis/orgs","repos_url":"https://api.github.com/users/talis/repos","events_url":"https://api.github.com/users/talis/events{/privacy}","received_events_url":"https://api.github.com/users/talis/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/talis/bearhug-angular","description":"Response interceptor for elegant bearer-token handling for angular's $http service","fork":false,"url":"https://api.github.com/repos/talis/bearhug-angular","forks_url":"https://api.github.com/repos/talis/bearhug-angular/forks","keys_url":"https://api.github.com/repos/talis/bearhug-angular/keys{/key_id}","collaborators_url":"https://api.github.com/repos/talis/bearhug-angular/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/talis/bearhug-angular/teams","hooks_url":"https://api.github.com/repos/talis/bearhug-angular/hooks","issue_events_url":"https://api.github.com/repos/talis/bearhug-angular/issues/events{/number}","events_url":"https://api.github.com/repos/talis/bearhug-angular/events","assignees_url":"https://api.github.com/repos/talis/bearhug-angular/assignees{/user}","branches_url":"https://api.github.com/repos/talis/bearhug-angular/branches{/branch}","tags_url":"https://api.github.com/repos/talis/bearhug-angular/tags","blobs_url":"https://api.github.com/repos/talis/bearhug-angular/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/talis/bearhug-angular/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/talis/bearhug-angular/git/refs{/sha}","trees_url":"https://api.github.com/repos/talis/bearhug-angular/git/trees{/sha}","statuses_url":"https://api.github.com/repos/talis/bearhug-angular/statuses/{sha}","languages_url":"https://api.github.com/repos/talis/bearhug-angular/languages","stargazers_url":"https://api.github.com/repos/talis/bearhug-angular/stargazers","contributors_url":"https://api.github.com/repos/talis/bearhug-angular/contributors","subscribers_url":"https://api.github.com/repos/talis/bearhug-angular/subscribers","subscription_url":"https://api.github.com/repos/talis/bearhug-angular/subscription","commits_url":"https://api.github.com/repos/talis/bearhug-angular/commits{/sha}","git_commits_url":"https://api.github.com/repos/talis/bearhug-angular/git/commits{/sha}","comments_url":"https://api.github.com/repos/talis/bearhug-angular/comments{/number}","issue_comment_url":"https://api.github.com/repos/talis/bearhug-angular/issues/comments{/number}","contents_url":"https://api.github.com/repos/talis/bearhug-angular/contents/{+path}","compare_url":"https://api.github.com/repos/talis/bearhug-angular/compare/{base}...{head}","merges_url":"https://api.github.com/repos/talis/bearhug-angular/merges","archive_url":"https://api.github.com/repos/talis/bearhug-angular/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/talis/bearhug-angular/downloads","issues_url":"https://api.github.com/repos/talis/bearhug-angular/issues{/number}","pulls_url":"https://api.github.com/repos/talis/bearhug-angular/pulls{/number}","milestones_url":"https://api.github.com/repos/talis/bearhug-angular/milestones{/number}","notifications_url":"https://api.github.com/repos/talis/bearhug-angular/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/talis/bearhug-angular/labels{/name}","releases_url":"https://api.github.com/repos/talis/bearhug-angular/releases{/id}","deployments_url":"https://api.github.com/repos/talis/bearhug-angular/deployments","created_at":"2014-11-27T16:56:36Z","updated_at":"2016-04-01T09:50:49Z","pushed_at":"2016-05-06T09:57:42Z","git_url":"git://github.com/talis/bearhug-angular.git","ssh_url":"git@github.com:talis/bearhug-angular.git","clone_url":"https://github.com/talis/bearhug-angular.git","svn_url":"https://github.com/talis/bearhug-angular","homepage":null,"size":45,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"talis:master","ref":"master","sha":"850073c51f2ef0205e213067516568ee18dd57c9","user":{"login":"talis","id":181416,"avatar_url":"https://avatars.githubusercontent.com/u/181416?v=3","gravatar_id":"","url":"https://api.github.com/users/talis","html_url":"https://github.com/talis","followers_url":"https://api.github.com/users/talis/followers","following_url":"https://api.github.com/users/talis/following{/other_user}","gists_url":"https://api.github.com/users/talis/gists{/gist_id}","starred_url":"https://api.github.com/users/talis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/talis/subscriptions","organizations_url":"https://api.github.com/users/talis/orgs","repos_url":"https://api.github.com/users/talis/repos","events_url":"https://api.github.com/users/talis/events{/privacy}","received_events_url":"https://api.github.com/users/talis/received_events","type":"Organization","site_admin":false},"repo":{"id":27234356,"name":"bearhug-angular","full_name":"talis/bearhug-angular","owner":{"login":"talis","id":181416,"avatar_url":"https://avatars.githubusercontent.com/u/181416?v=3","gravatar_id":"","url":"https://api.github.com/users/talis","html_url":"https://github.com/talis","followers_url":"https://api.github.com/users/talis/followers","following_url":"https://api.github.com/users/talis/following{/other_user}","gists_url":"https://api.github.com/users/talis/gists{/gist_id}","starred_url":"https://api.github.com/users/talis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/talis/subscriptions","organizations_url":"https://api.github.com/users/talis/orgs","repos_url":"https://api.github.com/users/talis/repos","events_url":"https://api.github.com/users/talis/events{/privacy}","received_events_url":"https://api.github.com/users/talis/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/talis/bearhug-angular","description":"Response interceptor for elegant bearer-token handling for angular's $http service","fork":false,"url":"https://api.github.com/repos/talis/bearhug-angular","forks_url":"https://api.github.com/repos/talis/bearhug-angular/forks","keys_url":"https://api.github.com/repos/talis/bearhug-angular/keys{/key_id}","collaborators_url":"https://api.github.com/repos/talis/bearhug-angular/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/talis/bearhug-angular/teams","hooks_url":"https://api.github.com/repos/talis/bearhug-angular/hooks","issue_events_url":"https://api.github.com/repos/talis/bearhug-angular/issues/events{/number}","events_url":"https://api.github.com/repos/talis/bearhug-angular/events","assignees_url":"https://api.github.com/repos/talis/bearhug-angular/assignees{/user}","branches_url":"https://api.github.com/repos/talis/bearhug-angular/branches{/branch}","tags_url":"https://api.github.com/repos/talis/bearhug-angular/tags","blobs_url":"https://api.github.com/repos/talis/bearhug-angular/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/talis/bearhug-angular/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/talis/bearhug-angular/git/refs{/sha}","trees_url":"https://api.github.com/repos/talis/bearhug-angular/git/trees{/sha}","statuses_url":"https://api.github.com/repos/talis/bearhug-angular/statuses/{sha}","languages_url":"https://api.github.com/repos/talis/bearhug-angular/languages","stargazers_url":"https://api.github.com/repos/talis/bearhug-angular/stargazers","contributors_url":"https://api.github.com/repos/talis/bearhug-angular/contributors","subscribers_url":"https://api.github.com/repos/talis/bearhug-angular/subscribers","subscription_url":"https://api.github.com/repos/talis/bearhug-angular/subscription","commits_url":"https://api.github.com/repos/talis/bearhug-angular/commits{/sha}","git_commits_url":"https://api.github.com/repos/talis/bearhug-angular/git/commits{/sha}","comments_url":"https://api.github.com/repos/talis/bearhug-angular/comments{/number}","issue_comment_url":"https://api.github.com/repos/talis/bearhug-angular/issues/comments{/number}","contents_url":"https://api.github.com/repos/talis/bearhug-angular/contents/{+path}","compare_url":"https://api.github.com/repos/talis/bearhug-angular/compare/{base}...{head}","merges_url":"https://api.github.com/repos/talis/bearhug-angular/merges","archive_url":"https://api.github.com/repos/talis/bearhug-angular/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/talis/bearhug-angular/downloads","issues_url":"https://api.github.com/repos/talis/bearhug-angular/issues{/number}","pulls_url":"https://api.github.com/repos/talis/bearhug-angular/pulls{/number}","milestones_url":"https://api.github.com/repos/talis/bearhug-angular/milestones{/number}","notifications_url":"https://api.github.com/repos/talis/bearhug-angular/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/talis/bearhug-angular/labels{/name}","releases_url":"https://api.github.com/repos/talis/bearhug-angular/releases{/id}","deployments_url":"https://api.github.com/repos/talis/bearhug-angular/deployments","created_at":"2014-11-27T16:56:36Z","updated_at":"2016-04-01T09:50:49Z","pushed_at":"2016-05-06T09:57:42Z","git_url":"git://github.com/talis/bearhug-angular.git","ssh_url":"git@github.com:talis/bearhug-angular.git","clone_url":"https://github.com/talis/bearhug-angular.git","svn_url":"https://github.com/talis/bearhug-angular","homepage":null,"size":45,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/talis/bearhug-angular/pulls/4"},"html":{"href":"https://github.com/talis/bearhug-angular/pull/4"},"issue":{"href":"https://api.github.com/repos/talis/bearhug-angular/issues/4"},"comments":{"href":"https://api.github.com/repos/talis/bearhug-angular/issues/4/comments"},"review_comments":{"href":"https://api.github.com/repos/talis/bearhug-angular/pulls/4/comments"},"review_comment":{"href":"https://api.github.com/repos/talis/bearhug-angular/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/talis/bearhug-angular/pulls/4/commits"},"statuses":{"href":"https://api.github.com/repos/talis/bearhug-angular/statuses/56e842738edf946e87646f7488452ddf5bd70899"}}}}