{"action":"created","comment":{"url":"https://api.github.com/repos/fxbox/app/pulls/comments/57914964","id":57914964,"diff_hunk":"@@ -0,0 +1,206 @@\n+'use strict';\n+\n+// Private members.\n+const p = {\n+  // Private properties.\n+  // Whether we can connect to the box via a local connection.\n+  local: Symbol('local'),\n+  // Whether we can connect to the box via a remote connection.\n+  remote: Symbol('remote'),\n+  // A reference to the interval to get the online status.\n+  pingInterval: Symbol('pingInterval'),\n+\n+  // Private methods.\n+  fetch: Symbol('fetch'),\n+  ping: Symbol('ping'),\n+  pingBox: Symbol('pingBox')\n+};\n+\n+export default class Network {\n+  constructor(settings) {\n+    this.settings = settings;\n+\n+    this[p.local] = false;\n+    this[p.remote] = false;\n+    this[p.pingInterval] = null;\n+  }\n+\n+  /**\n+   * Attach event listeners related to the connection status.\n+   *\n+   * @return {Promise}\n+   */\n+  init() {\n+    const pingBox = this[p.pingBox].bind(this);\n+\n+    window.addEventListener('online', pingBox);\n+    window.addEventListener('offline', pingBox);\n+\n+    if ('connection' in navigator && 'onchange' in navigator.connection) {\n+      navigator.connection.addEventListener('change', pingBox);\n+\n+      // We also ping the box every few minutes to make sure it's still there.\n+      this[p.pingInterval] = setInterval(pingBox,\n+        this.settings.onlineCheckingLongInterval);\n+    } else {\n+      // If the Network Information API is not implemented, fallback to polling.\n+      this[p.pingInterval] = setInterval(pingBox,\n+        this.settings.onlineCheckingInterval);\n+    }\n+\n+    this[p.pingBox]();\n+\n+    return Promise.resolve();\n+  }\n+\n+  get origin() {\n+    if (this[p.local]) {\n+      return this.localOrigin;\n+    } else if (this[p.remote]) {\n+      return this.tunnelOrigin;\n+    }\n+\n+    console.error('The box is out of reach.');\n+    return this.localOrigin;\n+  }\n+\n+  get localOrigin() {\n+    return `${this.settings.localScheme}://${this.settings.localHostname}:${this.settings.localPort}`;\n+  }\n+\n+  get tunnelOrigin() {\n+    return `${this.settings.tunnelScheme}://${this.settings.tunnelHostname}:${this.settings.tunnelPort}`;\n+  }\n+\n+  get connected() {\n+    return this[p.local] || this[p.remote];\n+  }\n+\n+  get connection() {\n+    if (this[p.local]) {\n+      return 'local';\n+    } else if (this[p.remote]) {\n+      return 'remote';\n+    }\n+    return 'unknown';\n+  }\n+\n+  /**\n+   * Request a JSON from a specified URL.\n+   *\n+   * @param {string} url The URL to send the request to.\n+   * @param {string} method The HTTP method (defaults to \"GET\").\n+   * @param {Object} body An object of key/value.\n+   * @return {Promise}\n+   */\n+  fetchJSON(url, method = 'GET', body = undefined) {\n+    return this[p.fetch](url, 'application/json', method, body)\n+      .then(response => response.json());\n+  }\n+\n+  /**\n+   * Request a Blob from a specified URL.\n+   *\n+   * @param {string} url The URL to send the request to.\n+   * @param {string} blobType The Blob mime type (eg. image/jpeg).\n+   * @param {string} method The HTTP method (defaults to \"GET\").\n+   * @param {Object} body An object of key/value.\n+   * @return {Promise<Blob>}\n+   */\n+  fetchBlob(url, blobType, method, body) {\n+    return this[p.fetch](url, blobType, method, body)\n+      .then(response => response.blob());\n+  }\n+\n+  /**\n+   * Request a content of the specified type from a specified URL.\n+   *\n+   * @todo Detect if the URL is relative, if so prepend this.origin.\n+   *\n+   * @param {string} url The URL to send the request to.\n+   * @param {string} accept The content mime type (eg. image/jpeg).\n+   * @param {string=} method The HTTP method (defaults to \"GET\").","path":"app/js/lib/foxbox/network.js","position":122,"original_position":122,"commit_id":"960ceb8e79dd044271d1e513113b7d1604b0a895","original_commit_id":"960ceb8e79dd044271d1e513113b7d1604b0a895","user":{"login":"gmarty","id":333865,"avatar_url":"https://avatars.githubusercontent.com/u/333865?v=3","gravatar_id":"","url":"https://api.github.com/users/gmarty","html_url":"https://github.com/gmarty","followers_url":"https://api.github.com/users/gmarty/followers","following_url":"https://api.github.com/users/gmarty/following{/other_user}","gists_url":"https://api.github.com/users/gmarty/gists{/gist_id}","starred_url":"https://api.github.com/users/gmarty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gmarty/subscriptions","organizations_url":"https://api.github.com/users/gmarty/orgs","repos_url":"https://api.github.com/users/gmarty/repos","events_url":"https://api.github.com/users/gmarty/events{/privacy}","received_events_url":"https://api.github.com/users/gmarty/received_events","type":"User","site_admin":false},"body":"Yes, I've started using the [annotations system from Closure Compiler](https://developers.google.com/closure/compiler/docs/js-for-compiler#multiple-template-types) that doesn't support mentioning the default value. So I think it's redundant to use this type of annotation and an explanation in plain English (like `(defaults to \"GET\")`). So I decided to only keep the latter.\r\n\r\nWhat do you think?\r\n\r\nThe rationale from using the annotations of Closure Compiler is that we're going to use it in our build system one day.\r\nAlso, in the future when we generate documentation from the code, we may want to add mentioning the default value of parameters if supported by the doc generator and if it doesn't cause any issues with Closure Compiler.","created_at":"2016-03-30T16:01:32Z","updated_at":"2016-03-30T16:01:32Z","html_url":"https://github.com/fxbox/app/pull/34#discussion_r57914964","pull_request_url":"https://api.github.com/repos/fxbox/app/pulls/34","_links":{"self":{"href":"https://api.github.com/repos/fxbox/app/pulls/comments/57914964"},"html":{"href":"https://github.com/fxbox/app/pull/34#discussion_r57914964"},"pull_request":{"href":"https://api.github.com/repos/fxbox/app/pulls/34"}}},"pull_request":{"url":"https://api.github.com/repos/fxbox/app/pulls/34","id":64218211,"html_url":"https://github.com/fxbox/app/pull/34","diff_url":"https://github.com/fxbox/app/pull/34.diff","patch_url":"https://github.com/fxbox/app/pull/34.patch","issue_url":"https://api.github.com/repos/fxbox/app/issues/34","number":34,"state":"open","locked":false,"title":"Refactor the network related code out of the main class","user":{"login":"gmarty","id":333865,"avatar_url":"https://avatars.githubusercontent.com/u/333865?v=3","gravatar_id":"","url":"https://api.github.com/users/gmarty","html_url":"https://github.com/gmarty","followers_url":"https://api.github.com/users/gmarty/followers","following_url":"https://api.github.com/users/gmarty/following{/other_user}","gists_url":"https://api.github.com/users/gmarty/gists{/gist_id}","starred_url":"https://api.github.com/users/gmarty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gmarty/subscriptions","organizations_url":"https://api.github.com/users/gmarty/orgs","repos_url":"https://api.github.com/users/gmarty/repos","events_url":"https://api.github.com/users/gmarty/events{/privacy}","received_events_url":"https://api.github.com/users/gmarty/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-03-25T23:15:59Z","updated_at":"2016-03-30T16:01:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d09f0d9ee11e3d36f638abb230a7d75d453fcace","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/fxbox/app/pulls/34/commits","review_comments_url":"https://api.github.com/repos/fxbox/app/pulls/34/comments","review_comment_url":"https://api.github.com/repos/fxbox/app/pulls/comments{/number}","comments_url":"https://api.github.com/repos/fxbox/app/issues/34/comments","statuses_url":"https://api.github.com/repos/fxbox/app/statuses/960ceb8e79dd044271d1e513113b7d1604b0a895","head":{"label":"gmarty:network-class","ref":"network-class","sha":"960ceb8e79dd044271d1e513113b7d1604b0a895","user":{"login":"gmarty","id":333865,"avatar_url":"https://avatars.githubusercontent.com/u/333865?v=3","gravatar_id":"","url":"https://api.github.com/users/gmarty","html_url":"https://github.com/gmarty","followers_url":"https://api.github.com/users/gmarty/followers","following_url":"https://api.github.com/users/gmarty/following{/other_user}","gists_url":"https://api.github.com/users/gmarty/gists{/gist_id}","starred_url":"https://api.github.com/users/gmarty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gmarty/subscriptions","organizations_url":"https://api.github.com/users/gmarty/orgs","repos_url":"https://api.github.com/users/gmarty/repos","events_url":"https://api.github.com/users/gmarty/events{/privacy}","received_events_url":"https://api.github.com/users/gmarty/received_events","type":"User","site_admin":false},"repo":{"id":54418298,"name":"app","full_name":"gmarty/app","owner":{"login":"gmarty","id":333865,"avatar_url":"https://avatars.githubusercontent.com/u/333865?v=3","gravatar_id":"","url":"https://api.github.com/users/gmarty","html_url":"https://github.com/gmarty","followers_url":"https://api.github.com/users/gmarty/followers","following_url":"https://api.github.com/users/gmarty/following{/other_user}","gists_url":"https://api.github.com/users/gmarty/gists{/gist_id}","starred_url":"https://api.github.com/users/gmarty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gmarty/subscriptions","organizations_url":"https://api.github.com/users/gmarty/orgs","repos_url":"https://api.github.com/users/gmarty/repos","events_url":"https://api.github.com/users/gmarty/events{/privacy}","received_events_url":"https://api.github.com/users/gmarty/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/gmarty/app","description":"An app for Project Link","fork":true,"url":"https://api.github.com/repos/gmarty/app","forks_url":"https://api.github.com/repos/gmarty/app/forks","keys_url":"https://api.github.com/repos/gmarty/app/keys{/key_id}","collaborators_url":"https://api.github.com/repos/gmarty/app/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/gmarty/app/teams","hooks_url":"https://api.github.com/repos/gmarty/app/hooks","issue_events_url":"https://api.github.com/repos/gmarty/app/issues/events{/number}","events_url":"https://api.github.com/repos/gmarty/app/events","assignees_url":"https://api.github.com/repos/gmarty/app/assignees{/user}","branches_url":"https://api.github.com/repos/gmarty/app/branches{/branch}","tags_url":"https://api.github.com/repos/gmarty/app/tags","blobs_url":"https://api.github.com/repos/gmarty/app/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/gmarty/app/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/gmarty/app/git/refs{/sha}","trees_url":"https://api.github.com/repos/gmarty/app/git/trees{/sha}","statuses_url":"https://api.github.com/repos/gmarty/app/statuses/{sha}","languages_url":"https://api.github.com/repos/gmarty/app/languages","stargazers_url":"https://api.github.com/repos/gmarty/app/stargazers","contributors_url":"https://api.github.com/repos/gmarty/app/contributors","subscribers_url":"https://api.github.com/repos/gmarty/app/subscribers","subscription_url":"https://api.github.com/repos/gmarty/app/subscription","commits_url":"https://api.github.com/repos/gmarty/app/commits{/sha}","git_commits_url":"https://api.github.com/repos/gmarty/app/git/commits{/sha}","comments_url":"https://api.github.com/repos/gmarty/app/comments{/number}","issue_comment_url":"https://api.github.com/repos/gmarty/app/issues/comments{/number}","contents_url":"https://api.github.com/repos/gmarty/app/contents/{+path}","compare_url":"https://api.github.com/repos/gmarty/app/compare/{base}...{head}","merges_url":"https://api.github.com/repos/gmarty/app/merges","archive_url":"https://api.github.com/repos/gmarty/app/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/gmarty/app/downloads","issues_url":"https://api.github.com/repos/gmarty/app/issues{/number}","pulls_url":"https://api.github.com/repos/gmarty/app/pulls{/number}","milestones_url":"https://api.github.com/repos/gmarty/app/milestones{/number}","notifications_url":"https://api.github.com/repos/gmarty/app/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/gmarty/app/labels{/name}","releases_url":"https://api.github.com/repos/gmarty/app/releases{/id}","deployments_url":"https://api.github.com/repos/gmarty/app/deployments","created_at":"2016-03-21T19:53:28Z","updated_at":"2016-03-21T19:53:28Z","pushed_at":"2016-03-30T14:29:25Z","git_url":"git://github.com/gmarty/app.git","ssh_url":"git@github.com:gmarty/app.git","clone_url":"https://github.com/gmarty/app.git","svn_url":"https://github.com/gmarty/app","homepage":"https://fxbox.github.io/app","size":1675,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"fxbox:master","ref":"master","sha":"5b9e5cb53bd97235dc0528978ad2d2bcb708180f","user":{"login":"fxbox","id":17052474,"avatar_url":"https://avatars.githubusercontent.com/u/17052474?v=3","gravatar_id":"","url":"https://api.github.com/users/fxbox","html_url":"https://github.com/fxbox","followers_url":"https://api.github.com/users/fxbox/followers","following_url":"https://api.github.com/users/fxbox/following{/other_user}","gists_url":"https://api.github.com/users/fxbox/gists{/gist_id}","starred_url":"https://api.github.com/users/fxbox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fxbox/subscriptions","organizations_url":"https://api.github.com/users/fxbox/orgs","repos_url":"https://api.github.com/users/fxbox/repos","events_url":"https://api.github.com/users/fxbox/events{/privacy}","received_events_url":"https://api.github.com/users/fxbox/received_events","type":"Organization","site_admin":false},"repo":{"id":51533667,"name":"app","full_name":"fxbox/app","owner":{"login":"fxbox","id":17052474,"avatar_url":"https://avatars.githubusercontent.com/u/17052474?v=3","gravatar_id":"","url":"https://api.github.com/users/fxbox","html_url":"https://github.com/fxbox","followers_url":"https://api.github.com/users/fxbox/followers","following_url":"https://api.github.com/users/fxbox/following{/other_user}","gists_url":"https://api.github.com/users/fxbox/gists{/gist_id}","starred_url":"https://api.github.com/users/fxbox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fxbox/subscriptions","organizations_url":"https://api.github.com/users/fxbox/orgs","repos_url":"https://api.github.com/users/fxbox/repos","events_url":"https://api.github.com/users/fxbox/events{/privacy}","received_events_url":"https://api.github.com/users/fxbox/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/fxbox/app","description":"An app for Project Link","fork":false,"url":"https://api.github.com/repos/fxbox/app","forks_url":"https://api.github.com/repos/fxbox/app/forks","keys_url":"https://api.github.com/repos/fxbox/app/keys{/key_id}","collaborators_url":"https://api.github.com/repos/fxbox/app/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/fxbox/app/teams","hooks_url":"https://api.github.com/repos/fxbox/app/hooks","issue_events_url":"https://api.github.com/repos/fxbox/app/issues/events{/number}","events_url":"https://api.github.com/repos/fxbox/app/events","assignees_url":"https://api.github.com/repos/fxbox/app/assignees{/user}","branches_url":"https://api.github.com/repos/fxbox/app/branches{/branch}","tags_url":"https://api.github.com/repos/fxbox/app/tags","blobs_url":"https://api.github.com/repos/fxbox/app/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/fxbox/app/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/fxbox/app/git/refs{/sha}","trees_url":"https://api.github.com/repos/fxbox/app/git/trees{/sha}","statuses_url":"https://api.github.com/repos/fxbox/app/statuses/{sha}","languages_url":"https://api.github.com/repos/fxbox/app/languages","stargazers_url":"https://api.github.com/repos/fxbox/app/stargazers","contributors_url":"https://api.github.com/repos/fxbox/app/contributors","subscribers_url":"https://api.github.com/repos/fxbox/app/subscribers","subscription_url":"https://api.github.com/repos/fxbox/app/subscription","commits_url":"https://api.github.com/repos/fxbox/app/commits{/sha}","git_commits_url":"https://api.github.com/repos/fxbox/app/git/commits{/sha}","comments_url":"https://api.github.com/repos/fxbox/app/comments{/number}","issue_comment_url":"https://api.github.com/repos/fxbox/app/issues/comments{/number}","contents_url":"https://api.github.com/repos/fxbox/app/contents/{+path}","compare_url":"https://api.github.com/repos/fxbox/app/compare/{base}...{head}","merges_url":"https://api.github.com/repos/fxbox/app/merges","archive_url":"https://api.github.com/repos/fxbox/app/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/fxbox/app/downloads","issues_url":"https://api.github.com/repos/fxbox/app/issues{/number}","pulls_url":"https://api.github.com/repos/fxbox/app/pulls{/number}","milestones_url":"https://api.github.com/repos/fxbox/app/milestones{/number}","notifications_url":"https://api.github.com/repos/fxbox/app/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/fxbox/app/labels{/name}","releases_url":"https://api.github.com/repos/fxbox/app/releases{/id}","deployments_url":"https://api.github.com/repos/fxbox/app/deployments","created_at":"2016-02-11T17:46:54Z","updated_at":"2016-03-04T13:20:59Z","pushed_at":"2016-03-30T15:49:07Z","git_url":"git://github.com/fxbox/app.git","ssh_url":"git@github.com:fxbox/app.git","clone_url":"https://github.com/fxbox/app.git","svn_url":"https://github.com/fxbox/app","homepage":"https://fxbox.github.io/app","size":1696,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":7,"mirror_url":null,"open_issues_count":10,"forks":7,"open_issues":10,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/fxbox/app/pulls/34"},"html":{"href":"https://github.com/fxbox/app/pull/34"},"issue":{"href":"https://api.github.com/repos/fxbox/app/issues/34"},"comments":{"href":"https://api.github.com/repos/fxbox/app/issues/34/comments"},"review_comments":{"href":"https://api.github.com/repos/fxbox/app/pulls/34/comments"},"review_comment":{"href":"https://api.github.com/repos/fxbox/app/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/fxbox/app/pulls/34/commits"},"statuses":{"href":"https://api.github.com/repos/fxbox/app/statuses/960ceb8e79dd044271d1e513113b7d1604b0a895"}}}}