{"action":"created","comment":{"url":"https://api.github.com/repos/aalbuquerque73/apimock/pulls/comments/28195523","id":28195523,"diff_hunk":"@@ -0,0 +1,179 @@\n+var _ = require('underscore'),\n+    Q = require('q'),\n+    glob = require('glob'),\n+    fs = require('fs'),\n+    path = require('path'),\n+    request = require('request'),\n+    config = require('config'),\n+    \n+    restify = require('restify'),\n+    \n+    overriders = require('./overriders'),\n+    \n+    folders = require('./folders'),\n+    equals = require('./comparator');\n+\n+function Api(urlList) {\n+    this.folder = folders.path;\n+    this.urlList = urlList;\n+}\n+Api.prototype = {\n+    handle: function(req, res, next) {\n+        if (!this.urlList.hasOwnProperty(req.params.path)) {\n+            res.send(404, 'Not handled!');\n+            return next(new restify.errors.ResourceNotFoundError('Not Handled', '\"' + req.params.path + '\" is not defined by configuration'));\n+        }\n+        \n+        var proxy = this.urlList[req.params.path];\n+        overriders.save.folders(proxy, proxy.route);\n+        this.folder = folders[proxy.name];\n+        this.proxy(proxy, req, res, next)\n+            .then(function() {\n+                console.log('resolved', arguments);\n+                return next();\n+            })\n+            .fail(function(error) {\n+                console.error(error);\n+                return next(error);\n+            });\n+    },\n+    \n+    \n+    proxy: function(proxy, req, res, next) {\n+        var url = proxy.url;\n+        var patterns = [ path.join(folders[proxy.name], '*.req') ];\n+        overriders.override.if.needed(patterns, proxy, this);\n+        return Q.allSettled(_.map(patterns, function(pattern) {\n+            return Q.nfcall(glob, pattern, { nosort: true, nodir: true });\n+        }, this))\n+            .then(function(fileList) {\n+                fileList = _.chain(fileList).map(function(result) { return result.value; }).flatten().value();\n+                this.find(fileList, req, res, next)\n+                    .then(function(file) {\n+                        console.log('then', path.basename(file), 'found!');\n+                        return Q.promise(function(resolve) { resolve(file); });\n+                    })\n+                    .fail(function() {\n+                        console.log('fail', 'NOT found!');\n+                        return this.request(url, fileList, req, res, next);\n+                    }.bind(this));\n+            }.bind(this));\n+    },\n+    \n+    find: function(fileList, req, res, next) {\n+        var promises = _.map(fileList, function(file) {\n+            return Q.Promise(function(resolve, reject) {\n+                fs.readFile(file, { encoding: 'utf8' }, function(err, data) {\n+                    if (err) {\n+                        reject(err);\n+                        return;\n+                    }\n+                    equals(req.params, data)\n+                        .then(function() {\n+                            console.log('File', path.basename(file), 'found!');\n+                            resolve(file);\n+                        })\n+                        .fail(function() {\n+                            reject(new Error('Not found!'));\n+                        });\n+                });\n+            });\n+        });\n+        return Q.allSettled(promises)\n+            .then(function(result) {\n+                var chain = _.chain(result)\n+                    .filter(function(file) { return file.state === 'fulfilled'; })\n+                    .first();\n+                result = chain.value().value;\n+                console.log('found:', path.basename(result));\n+                return Q.Promise(function(resolve, reject) {\n+                    //console.log('response found... reading files...');\n+                    Q.allSettled([\n+                        Q.nfcall(fs.readFile, result.replace(/\\.req$/, '.stats'), { encoding: 'utf8' }),\n+                        Q.nfcall(fs.readFile, result.replace(/\\.req$/, '.res'), { encoding: 'utf8' })\n+                    ])\n+                    .then(function(files) {\n+                        var stats = JSON.parse(files[0].value);\n+                        res.status(stats.status);\n+                        _.each(stats.headers, function(value, key) {\n+                            res.setHeader(key, value);\n+                        });\n+                        res.write(files[1].value);\n+                        res.end();\n+                        resolve(result);\n+                    })\n+                    .fail(reject);\n+                });\n+            });\n+    },\n+    \n+    request: function(url, fileList, req, res, next) {\n+        return Q.Promise(function(resolve, reject) {\n+            console.log('file not found, requesting from', url + req.url);\n+            request(url + req.url, function(error, response, body) {\n+                if (error) {\n+                    reject(error);\n+                    return;\n+                }\n+                if (response.statusCode === 200) {\n+                    this.save(req.params, response, body, fileList.length)\n+                        .then(function() {\n+                            res.status(response.statusCode);\n+                            _.each(response.headers, function(value, key) {\n+                                res.setHeader(key, value);\n+                            });\n+                            res.write(body);\n+                            res.end();\n+                            resolve();\n+                        })\n+                        .fail(reject);\n+                    return;","path":"api/get.js","position":130,"original_position":130,"commit_id":"39ce4a10ea3507b2eb30602fe5095cac5130700c","original_commit_id":"39ce4a10ea3507b2eb30602fe5095cac5130700c","user":{"login":"zezutom","id":1328972,"avatar_url":"https://avatars.githubusercontent.com/u/1328972?v=3","gravatar_id":"","url":"https://api.github.com/users/zezutom","html_url":"https://github.com/zezutom","followers_url":"https://api.github.com/users/zezutom/followers","following_url":"https://api.github.com/users/zezutom/following{/other_user}","gists_url":"https://api.github.com/users/zezutom/gists{/gist_id}","starred_url":"https://api.github.com/users/zezutom/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zezutom/subscriptions","organizations_url":"https://api.github.com/users/zezutom/orgs","repos_url":"https://api.github.com/users/zezutom/repos","events_url":"https://api.github.com/users/zezutom/events{/privacy}","received_events_url":"https://api.github.com/users/zezutom/received_events","type":"User","site_admin":false},"body":"no need for a return statement, is it?","created_at":"2015-04-11T14:52:26Z","updated_at":"2015-04-11T14:52:26Z","html_url":"https://github.com/aalbuquerque73/apimock/pull/7#discussion_r28195523","pull_request_url":"https://api.github.com/repos/aalbuquerque73/apimock/pulls/7","_links":{"self":{"href":"https://api.github.com/repos/aalbuquerque73/apimock/pulls/comments/28195523"},"html":{"href":"https://github.com/aalbuquerque73/apimock/pull/7#discussion_r28195523"},"pull_request":{"href":"https://api.github.com/repos/aalbuquerque73/apimock/pulls/7"}}},"pull_request":{"url":"https://api.github.com/repos/aalbuquerque73/apimock/pulls/7","id":32901624,"html_url":"https://github.com/aalbuquerque73/apimock/pull/7","diff_url":"https://github.com/aalbuquerque73/apimock/pull/7.diff","patch_url":"https://github.com/aalbuquerque73/apimock/pull/7.patch","issue_url":"https://api.github.com/repos/aalbuquerque73/apimock/issues/7","number":7,"state":"open","locked":false,"title":"Apimock 2.0","user":{"login":"aalbuquerque73","id":6113168,"avatar_url":"https://avatars.githubusercontent.com/u/6113168?v=3","gravatar_id":"","url":"https://api.github.com/users/aalbuquerque73","html_url":"https://github.com/aalbuquerque73","followers_url":"https://api.github.com/users/aalbuquerque73/followers","following_url":"https://api.github.com/users/aalbuquerque73/following{/other_user}","gists_url":"https://api.github.com/users/aalbuquerque73/gists{/gist_id}","starred_url":"https://api.github.com/users/aalbuquerque73/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aalbuquerque73/subscriptions","organizations_url":"https://api.github.com/users/aalbuquerque73/orgs","repos_url":"https://api.github.com/users/aalbuquerque73/repos","events_url":"https://api.github.com/users/aalbuquerque73/events{/privacy}","received_events_url":"https://api.github.com/users/aalbuquerque73/received_events","type":"User","site_admin":false},"body":"Rewrite of apimock to use restify\r\nSimplification of config files","created_at":"2015-04-08T21:01:19Z","updated_at":"2015-04-11T14:52:26Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0ed4d201b4925dca0ab6521fc8a01d9dffa31b23","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/aalbuquerque73/apimock/pulls/7/commits","review_comments_url":"https://api.github.com/repos/aalbuquerque73/apimock/pulls/7/comments","review_comment_url":"https://api.github.com/repos/aalbuquerque73/apimock/pulls/comments{/number}","comments_url":"https://api.github.com/repos/aalbuquerque73/apimock/issues/7/comments","statuses_url":"https://api.github.com/repos/aalbuquerque73/apimock/statuses/39ce4a10ea3507b2eb30602fe5095cac5130700c","head":{"label":"aalbuquerque73:apimock-2.0","ref":"apimock-2.0","sha":"39ce4a10ea3507b2eb30602fe5095cac5130700c","user":{"login":"aalbuquerque73","id":6113168,"avatar_url":"https://avatars.githubusercontent.com/u/6113168?v=3","gravatar_id":"","url":"https://api.github.com/users/aalbuquerque73","html_url":"https://github.com/aalbuquerque73","followers_url":"https://api.github.com/users/aalbuquerque73/followers","following_url":"https://api.github.com/users/aalbuquerque73/following{/other_user}","gists_url":"https://api.github.com/users/aalbuquerque73/gists{/gist_id}","starred_url":"https://api.github.com/users/aalbuquerque73/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aalbuquerque73/subscriptions","organizations_url":"https://api.github.com/users/aalbuquerque73/orgs","repos_url":"https://api.github.com/users/aalbuquerque73/repos","events_url":"https://api.github.com/users/aalbuquerque73/events{/privacy}","received_events_url":"https://api.github.com/users/aalbuquerque73/received_events","type":"User","site_admin":false},"repo":{"id":27582489,"name":"apimock","full_name":"aalbuquerque73/apimock","owner":{"login":"aalbuquerque73","id":6113168,"avatar_url":"https://avatars.githubusercontent.com/u/6113168?v=3","gravatar_id":"","url":"https://api.github.com/users/aalbuquerque73","html_url":"https://github.com/aalbuquerque73","followers_url":"https://api.github.com/users/aalbuquerque73/followers","following_url":"https://api.github.com/users/aalbuquerque73/following{/other_user}","gists_url":"https://api.github.com/users/aalbuquerque73/gists{/gist_id}","starred_url":"https://api.github.com/users/aalbuquerque73/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aalbuquerque73/subscriptions","organizations_url":"https://api.github.com/users/aalbuquerque73/orgs","repos_url":"https://api.github.com/users/aalbuquerque73/repos","events_url":"https://api.github.com/users/aalbuquerque73/events{/privacy}","received_events_url":"https://api.github.com/users/aalbuquerque73/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/aalbuquerque73/apimock","description":"Data Mock proxy","fork":false,"url":"https://api.github.com/repos/aalbuquerque73/apimock","forks_url":"https://api.github.com/repos/aalbuquerque73/apimock/forks","keys_url":"https://api.github.com/repos/aalbuquerque73/apimock/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aalbuquerque73/apimock/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aalbuquerque73/apimock/teams","hooks_url":"https://api.github.com/repos/aalbuquerque73/apimock/hooks","issue_events_url":"https://api.github.com/repos/aalbuquerque73/apimock/issues/events{/number}","events_url":"https://api.github.com/repos/aalbuquerque73/apimock/events","assignees_url":"https://api.github.com/repos/aalbuquerque73/apimock/assignees{/user}","branches_url":"https://api.github.com/repos/aalbuquerque73/apimock/branches{/branch}","tags_url":"https://api.github.com/repos/aalbuquerque73/apimock/tags","blobs_url":"https://api.github.com/repos/aalbuquerque73/apimock/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aalbuquerque73/apimock/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aalbuquerque73/apimock/git/refs{/sha}","trees_url":"https://api.github.com/repos/aalbuquerque73/apimock/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aalbuquerque73/apimock/statuses/{sha}","languages_url":"https://api.github.com/repos/aalbuquerque73/apimock/languages","stargazers_url":"https://api.github.com/repos/aalbuquerque73/apimock/stargazers","contributors_url":"https://api.github.com/repos/aalbuquerque73/apimock/contributors","subscribers_url":"https://api.github.com/repos/aalbuquerque73/apimock/subscribers","subscription_url":"https://api.github.com/repos/aalbuquerque73/apimock/subscription","commits_url":"https://api.github.com/repos/aalbuquerque73/apimock/commits{/sha}","git_commits_url":"https://api.github.com/repos/aalbuquerque73/apimock/git/commits{/sha}","comments_url":"https://api.github.com/repos/aalbuquerque73/apimock/comments{/number}","issue_comment_url":"https://api.github.com/repos/aalbuquerque73/apimock/issues/comments{/number}","contents_url":"https://api.github.com/repos/aalbuquerque73/apimock/contents/{+path}","compare_url":"https://api.github.com/repos/aalbuquerque73/apimock/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aalbuquerque73/apimock/merges","archive_url":"https://api.github.com/repos/aalbuquerque73/apimock/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aalbuquerque73/apimock/downloads","issues_url":"https://api.github.com/repos/aalbuquerque73/apimock/issues{/number}","pulls_url":"https://api.github.com/repos/aalbuquerque73/apimock/pulls{/number}","milestones_url":"https://api.github.com/repos/aalbuquerque73/apimock/milestones{/number}","notifications_url":"https://api.github.com/repos/aalbuquerque73/apimock/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aalbuquerque73/apimock/labels{/name}","releases_url":"https://api.github.com/repos/aalbuquerque73/apimock/releases{/id}","created_at":"2014-12-05T08:43:11Z","updated_at":"2015-02-20T11:17:53Z","pushed_at":"2015-04-09T13:47:58Z","git_url":"git://github.com/aalbuquerque73/apimock.git","ssh_url":"git@github.com:aalbuquerque73/apimock.git","clone_url":"https://github.com/aalbuquerque73/apimock.git","svn_url":"https://github.com/aalbuquerque73/apimock","homepage":null,"size":997,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":2,"forks":1,"open_issues":2,"watchers":1,"default_branch":"develop"}},"base":{"label":"aalbuquerque73:develop","ref":"develop","sha":"258f54c1c445f787e7fab25d4d1517c49c5a7a89","user":{"login":"aalbuquerque73","id":6113168,"avatar_url":"https://avatars.githubusercontent.com/u/6113168?v=3","gravatar_id":"","url":"https://api.github.com/users/aalbuquerque73","html_url":"https://github.com/aalbuquerque73","followers_url":"https://api.github.com/users/aalbuquerque73/followers","following_url":"https://api.github.com/users/aalbuquerque73/following{/other_user}","gists_url":"https://api.github.com/users/aalbuquerque73/gists{/gist_id}","starred_url":"https://api.github.com/users/aalbuquerque73/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aalbuquerque73/subscriptions","organizations_url":"https://api.github.com/users/aalbuquerque73/orgs","repos_url":"https://api.github.com/users/aalbuquerque73/repos","events_url":"https://api.github.com/users/aalbuquerque73/events{/privacy}","received_events_url":"https://api.github.com/users/aalbuquerque73/received_events","type":"User","site_admin":false},"repo":{"id":27582489,"name":"apimock","full_name":"aalbuquerque73/apimock","owner":{"login":"aalbuquerque73","id":6113168,"avatar_url":"https://avatars.githubusercontent.com/u/6113168?v=3","gravatar_id":"","url":"https://api.github.com/users/aalbuquerque73","html_url":"https://github.com/aalbuquerque73","followers_url":"https://api.github.com/users/aalbuquerque73/followers","following_url":"https://api.github.com/users/aalbuquerque73/following{/other_user}","gists_url":"https://api.github.com/users/aalbuquerque73/gists{/gist_id}","starred_url":"https://api.github.com/users/aalbuquerque73/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aalbuquerque73/subscriptions","organizations_url":"https://api.github.com/users/aalbuquerque73/orgs","repos_url":"https://api.github.com/users/aalbuquerque73/repos","events_url":"https://api.github.com/users/aalbuquerque73/events{/privacy}","received_events_url":"https://api.github.com/users/aalbuquerque73/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/aalbuquerque73/apimock","description":"Data Mock proxy","fork":false,"url":"https://api.github.com/repos/aalbuquerque73/apimock","forks_url":"https://api.github.com/repos/aalbuquerque73/apimock/forks","keys_url":"https://api.github.com/repos/aalbuquerque73/apimock/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aalbuquerque73/apimock/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aalbuquerque73/apimock/teams","hooks_url":"https://api.github.com/repos/aalbuquerque73/apimock/hooks","issue_events_url":"https://api.github.com/repos/aalbuquerque73/apimock/issues/events{/number}","events_url":"https://api.github.com/repos/aalbuquerque73/apimock/events","assignees_url":"https://api.github.com/repos/aalbuquerque73/apimock/assignees{/user}","branches_url":"https://api.github.com/repos/aalbuquerque73/apimock/branches{/branch}","tags_url":"https://api.github.com/repos/aalbuquerque73/apimock/tags","blobs_url":"https://api.github.com/repos/aalbuquerque73/apimock/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aalbuquerque73/apimock/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aalbuquerque73/apimock/git/refs{/sha}","trees_url":"https://api.github.com/repos/aalbuquerque73/apimock/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aalbuquerque73/apimock/statuses/{sha}","languages_url":"https://api.github.com/repos/aalbuquerque73/apimock/languages","stargazers_url":"https://api.github.com/repos/aalbuquerque73/apimock/stargazers","contributors_url":"https://api.github.com/repos/aalbuquerque73/apimock/contributors","subscribers_url":"https://api.github.com/repos/aalbuquerque73/apimock/subscribers","subscription_url":"https://api.github.com/repos/aalbuquerque73/apimock/subscription","commits_url":"https://api.github.com/repos/aalbuquerque73/apimock/commits{/sha}","git_commits_url":"https://api.github.com/repos/aalbuquerque73/apimock/git/commits{/sha}","comments_url":"https://api.github.com/repos/aalbuquerque73/apimock/comments{/number}","issue_comment_url":"https://api.github.com/repos/aalbuquerque73/apimock/issues/comments{/number}","contents_url":"https://api.github.com/repos/aalbuquerque73/apimock/contents/{+path}","compare_url":"https://api.github.com/repos/aalbuquerque73/apimock/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aalbuquerque73/apimock/merges","archive_url":"https://api.github.com/repos/aalbuquerque73/apimock/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aalbuquerque73/apimock/downloads","issues_url":"https://api.github.com/repos/aalbuquerque73/apimock/issues{/number}","pulls_url":"https://api.github.com/repos/aalbuquerque73/apimock/pulls{/number}","milestones_url":"https://api.github.com/repos/aalbuquerque73/apimock/milestones{/number}","notifications_url":"https://api.github.com/repos/aalbuquerque73/apimock/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aalbuquerque73/apimock/labels{/name}","releases_url":"https://api.github.com/repos/aalbuquerque73/apimock/releases{/id}","created_at":"2014-12-05T08:43:11Z","updated_at":"2015-02-20T11:17:53Z","pushed_at":"2015-04-09T13:47:58Z","git_url":"git://github.com/aalbuquerque73/apimock.git","ssh_url":"git@github.com:aalbuquerque73/apimock.git","clone_url":"https://github.com/aalbuquerque73/apimock.git","svn_url":"https://github.com/aalbuquerque73/apimock","homepage":null,"size":997,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":2,"forks":1,"open_issues":2,"watchers":1,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/aalbuquerque73/apimock/pulls/7"},"html":{"href":"https://github.com/aalbuquerque73/apimock/pull/7"},"issue":{"href":"https://api.github.com/repos/aalbuquerque73/apimock/issues/7"},"comments":{"href":"https://api.github.com/repos/aalbuquerque73/apimock/issues/7/comments"},"review_comments":{"href":"https://api.github.com/repos/aalbuquerque73/apimock/pulls/7/comments"},"review_comment":{"href":"https://api.github.com/repos/aalbuquerque73/apimock/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/aalbuquerque73/apimock/pulls/7/commits"},"statuses":{"href":"https://api.github.com/repos/aalbuquerque73/apimock/statuses/39ce4a10ea3507b2eb30602fe5095cac5130700c"}}}}