{"action":"created","comment":{"url":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls/comments/34821516","id":34821516,"diff_hunk":"@@ -0,0 +1,111 @@\n+require 'sinatra/base'\n+require 'cdo/db'\n+require 'cdo/rack/request'\n+require 'csv'\n+\n+class NetSimApi < Sinatra::Base\n+\n+  helpers do\n+    [\n+        'core.rb',\n+        'storage_id.rb',\n+        'table.rb',\n+    ].each do |file|\n+      load(CDO.dir('shared', 'middleware', 'helpers', file))\n+    end\n+  end\n+\n+  TableType = CDO.use_dynamo_tables ? DynamoTable : Table\n+\n+  # Environment-dependent channel ID for NetSim.\n+  # Will no longer be needed when we detach from the channels API entirely.\n+  def netsim_channel_id\n+    rack_env?(:development) ? 'JGW2rHUp_UCMW_fQmRf6iQ==' : 'HQJ8GCCMGP7Yh8MrtDusIA=='\n+  end\n+\n+  def channels_table_name(shard_id, table_name)\n+    \"#{shard_id}_#{table_name}\"\n+  end\n+\n+  #\n+  # GET /v3/netsim/<shard-id>/<table-name>\n+  #\n+  # Returns all of the rows in the table.\n+  #\n+  get %r{/v3/netsim/([^/]+)/(\\w+)$} do |shard_id, table_name|\n+    dont_cache\n+    content_type :json\n+    TableType.new(netsim_channel_id, storage_id('shared'), channels_table_name(shard_id, table_name)).to_a.to_json\n+  end\n+\n+  #\n+  # GET /v3/netsim/<shard-id>/<table-name>/<row-id>\n+  #\n+  # Returns a single row by id.\n+  #\n+  get %r{/v3/netsim/([^/]+)/(\\w+)/(\\d+)$} do |shard_id, table_name, id|\n+    dont_cache\n+    content_type :json\n+    TableType.new(netsim_channel_id, storage_id('shared'), channels_table_name(shard_id, table_name)).fetch(id.to_i).to_json\n+  end\n+\n+  #\n+  # DELETE /v3/netsim/<shard-id>/<table-name>/<row-id>\n+  #\n+  # Deletes a row by id.\n+  #\n+  delete %r{/v3/netsim/([^/]+)/(\\w+)/(\\d+)$} do |shard_id, table_name, id|\n+    dont_cache\n+    TableType.new(netsim_channel_id, storage_id('shared'), channels_table_name(shard_id, table_name)).delete(id.to_i)\n+    no_content\n+  end\n+\n+  #\n+  # POST /v3/netsim/<shard-id>/<table-name>/<row-id>/delete\n+  #\n+  # This mapping exists for older browsers that don't support the DELETE verb.\n+  #\n+  post %r{/v3/netsim/([^/]+)/(\\w+)/(\\d+)/delete$} do |shard_id, table_name, id|\n+    call(env.merge('REQUEST_METHOD'=>'DELETE', 'PATH_INFO'=>File.dirname(request.path_info)))\n+  end\n+\n+  #\n+  # POST /v3/netsim/<shard-id>/<table-name>\n+  #\n+  # Insert a new row.\n+  #\n+  post %r{/v3/netsim/([^/]+)/(\\w+)$} do |shard_id, table_name|\n+    unsupported_media_type unless request.content_type.to_s.split(';').first == 'application/json'\n+    unsupported_media_type unless request.content_charset.to_s.downcase == 'utf-8'","path":"shared/middleware/net_sim_api.rb","position":79,"original_position":79,"commit_id":"b15c9f440a3f6dd9dc4978704d7fa1fcb62a70d7","original_commit_id":"03d8bd5351fcb995b2337f611ae757a548eeab5a","user":{"login":"philbogle","id":5198586,"avatar_url":"https://avatars.githubusercontent.com/u/5198586?v=3","gravatar_id":"","url":"https://api.github.com/users/philbogle","html_url":"https://github.com/philbogle","followers_url":"https://api.github.com/users/philbogle/followers","following_url":"https://api.github.com/users/philbogle/following{/other_user}","gists_url":"https://api.github.com/users/philbogle/gists{/gist_id}","starred_url":"https://api.github.com/users/philbogle/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/philbogle/subscriptions","organizations_url":"https://api.github.com/users/philbogle/orgs","repos_url":"https://api.github.com/users/philbogle/repos","events_url":"https://api.github.com/users/philbogle/events{/privacy}","received_events_url":"https://api.github.com/users/philbogle/received_events","type":"User","site_admin":false},"body":"How about just making this an \"and\" on the previous line.  ","created_at":"2015-07-16T18:32:24Z","updated_at":"2015-07-16T18:32:24Z","html_url":"https://github.com/code-dot-org/code-dot-org/pull/3134#discussion_r34821516","pull_request_url":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls/3134","_links":{"self":{"href":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls/comments/34821516"},"html":{"href":"https://github.com/code-dot-org/code-dot-org/pull/3134#discussion_r34821516"},"pull_request":{"href":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls/3134"}}},"pull_request":{"url":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls/3134","id":40085982,"html_url":"https://github.com/code-dot-org/code-dot-org/pull/3134","diff_url":"https://github.com/code-dot-org/code-dot-org/pull/3134.diff","patch_url":"https://github.com/code-dot-org/code-dot-org/pull/3134.patch","issue_url":"https://api.github.com/repos/code-dot-org/code-dot-org/issues/3134","number":3134,"state":"open","locked":false,"title":"Separate NetSim API routes from shared-tables API","user":{"login":"islemaster","id":1615761,"avatar_url":"https://avatars.githubusercontent.com/u/1615761?v=3","gravatar_id":"","url":"https://api.github.com/users/islemaster","html_url":"https://github.com/islemaster","followers_url":"https://api.github.com/users/islemaster/followers","following_url":"https://api.github.com/users/islemaster/following{/other_user}","gists_url":"https://api.github.com/users/islemaster/gists{/gist_id}","starred_url":"https://api.github.com/users/islemaster/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/islemaster/subscriptions","organizations_url":"https://api.github.com/users/islemaster/orgs","repos_url":"https://api.github.com/users/islemaster/repos","events_url":"https://api.github.com/users/islemaster/events{/privacy}","received_events_url":"https://api.github.com/users/islemaster/received_events","type":"User","site_admin":false},"body":"Gives NetSim its own API, where before it used our shared-tables API (which is also used for Applab).  For now this API is just a different path into the shared-tables API, but this is an incremental step toward specialized behavior for NetSim, like integrating PubSub functionality and swapping in a new storage layer.\r\n\r\nBefore, NetSim API URLs looked like this:\r\n\r\n```\r\n/v3/shared-tables/<channel-id>/<shard-id>_<table-name>\r\n/v3/shared-tables/JGW2rHUp_UCMW_fQmRf6iQ==/ns_s-netsim-stage-3-puzzle-4_c717e2bd-263a-476c-_n\r\n```\r\n\r\nNow they look like this:\r\n\r\n```\r\n/v3/netsim/<shard-id>/<table-name>\r\n/v3/netsim/ns_s-netsim-stage-3-puzzle-4_c717e2bd-263a-476c-/n\r\n```\r\n\r\nWe don't need to provide a channel ID anymore because NetSim always uses an environment-dependent reserved channel, and the server already knows the environment.  Eventually, we won't associate a channel with NetSim at all.\r\n\r\nI broke out the shard ID and table name (before they were concatenated to create a table name) for three reasons: 1. I like the way it looks better, 2. our PubSub system will use those values separately, so it's helpful for them to be broken up in the URL, and 3. It declutters the network log in the browser when you see only the one-letter table names:\r\n\r\n![screenshot from 2015-07-15 17 59 42](https://cloud.githubusercontent.com/assets/1615761/8713373/bed20ad6-2b1c-11e5-9c9b-60b81b45fd08.png)\r\n","created_at":"2015-07-16T01:11:08Z","updated_at":"2015-07-16T18:32:24Z","closed_at":null,"merged_at":null,"merge_commit_sha":"9c13408eabad24c345f1c9c96042937cd954e876","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls/3134/commits","review_comments_url":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls/3134/comments","review_comment_url":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls/comments{/number}","comments_url":"https://api.github.com/repos/code-dot-org/code-dot-org/issues/3134/comments","statuses_url":"https://api.github.com/repos/code-dot-org/code-dot-org/statuses/b15c9f440a3f6dd9dc4978704d7fa1fcb62a70d7","head":{"label":"code-dot-org:netsim-has-own-api","ref":"netsim-has-own-api","sha":"b15c9f440a3f6dd9dc4978704d7fa1fcb62a70d7","user":{"login":"code-dot-org","id":5124627,"avatar_url":"https://avatars.githubusercontent.com/u/5124627?v=3","gravatar_id":"","url":"https://api.github.com/users/code-dot-org","html_url":"https://github.com/code-dot-org","followers_url":"https://api.github.com/users/code-dot-org/followers","following_url":"https://api.github.com/users/code-dot-org/following{/other_user}","gists_url":"https://api.github.com/users/code-dot-org/gists{/gist_id}","starred_url":"https://api.github.com/users/code-dot-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/code-dot-org/subscriptions","organizations_url":"https://api.github.com/users/code-dot-org/orgs","repos_url":"https://api.github.com/users/code-dot-org/repos","events_url":"https://api.github.com/users/code-dot-org/events{/privacy}","received_events_url":"https://api.github.com/users/code-dot-org/received_events","type":"Organization","site_admin":false},"repo":{"id":24250009,"name":"code-dot-org","full_name":"code-dot-org/code-dot-org","owner":{"login":"code-dot-org","id":5124627,"avatar_url":"https://avatars.githubusercontent.com/u/5124627?v=3","gravatar_id":"","url":"https://api.github.com/users/code-dot-org","html_url":"https://github.com/code-dot-org","followers_url":"https://api.github.com/users/code-dot-org/followers","following_url":"https://api.github.com/users/code-dot-org/following{/other_user}","gists_url":"https://api.github.com/users/code-dot-org/gists{/gist_id}","starred_url":"https://api.github.com/users/code-dot-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/code-dot-org/subscriptions","organizations_url":"https://api.github.com/users/code-dot-org/orgs","repos_url":"https://api.github.com/users/code-dot-org/repos","events_url":"https://api.github.com/users/code-dot-org/events{/privacy}","received_events_url":"https://api.github.com/users/code-dot-org/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/code-dot-org/code-dot-org","description":"The code powering code.org and studio.code.org","fork":false,"url":"https://api.github.com/repos/code-dot-org/code-dot-org","forks_url":"https://api.github.com/repos/code-dot-org/code-dot-org/forks","keys_url":"https://api.github.com/repos/code-dot-org/code-dot-org/keys{/key_id}","collaborators_url":"https://api.github.com/repos/code-dot-org/code-dot-org/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/code-dot-org/code-dot-org/teams","hooks_url":"https://api.github.com/repos/code-dot-org/code-dot-org/hooks","issue_events_url":"https://api.github.com/repos/code-dot-org/code-dot-org/issues/events{/number}","events_url":"https://api.github.com/repos/code-dot-org/code-dot-org/events","assignees_url":"https://api.github.com/repos/code-dot-org/code-dot-org/assignees{/user}","branches_url":"https://api.github.com/repos/code-dot-org/code-dot-org/branches{/branch}","tags_url":"https://api.github.com/repos/code-dot-org/code-dot-org/tags","blobs_url":"https://api.github.com/repos/code-dot-org/code-dot-org/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/code-dot-org/code-dot-org/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/code-dot-org/code-dot-org/git/refs{/sha}","trees_url":"https://api.github.com/repos/code-dot-org/code-dot-org/git/trees{/sha}","statuses_url":"https://api.github.com/repos/code-dot-org/code-dot-org/statuses/{sha}","languages_url":"https://api.github.com/repos/code-dot-org/code-dot-org/languages","stargazers_url":"https://api.github.com/repos/code-dot-org/code-dot-org/stargazers","contributors_url":"https://api.github.com/repos/code-dot-org/code-dot-org/contributors","subscribers_url":"https://api.github.com/repos/code-dot-org/code-dot-org/subscribers","subscription_url":"https://api.github.com/repos/code-dot-org/code-dot-org/subscription","commits_url":"https://api.github.com/repos/code-dot-org/code-dot-org/commits{/sha}","git_commits_url":"https://api.github.com/repos/code-dot-org/code-dot-org/git/commits{/sha}","comments_url":"https://api.github.com/repos/code-dot-org/code-dot-org/comments{/number}","issue_comment_url":"https://api.github.com/repos/code-dot-org/code-dot-org/issues/comments{/number}","contents_url":"https://api.github.com/repos/code-dot-org/code-dot-org/contents/{+path}","compare_url":"https://api.github.com/repos/code-dot-org/code-dot-org/compare/{base}...{head}","merges_url":"https://api.github.com/repos/code-dot-org/code-dot-org/merges","archive_url":"https://api.github.com/repos/code-dot-org/code-dot-org/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/code-dot-org/code-dot-org/downloads","issues_url":"https://api.github.com/repos/code-dot-org/code-dot-org/issues{/number}","pulls_url":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls{/number}","milestones_url":"https://api.github.com/repos/code-dot-org/code-dot-org/milestones{/number}","notifications_url":"https://api.github.com/repos/code-dot-org/code-dot-org/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/code-dot-org/code-dot-org/labels{/name}","releases_url":"https://api.github.com/repos/code-dot-org/code-dot-org/releases{/id}","created_at":"2014-09-20T00:46:11Z","updated_at":"2015-07-11T11:09:37Z","pushed_at":"2015-07-16T18:25:57Z","git_url":"git://github.com/code-dot-org/code-dot-org.git","ssh_url":"git@github.com:code-dot-org/code-dot-org.git","clone_url":"https://github.com/code-dot-org/code-dot-org.git","svn_url":"https://github.com/code-dot-org/code-dot-org","homepage":"http://code.org","size":2326247,"stargazers_count":49,"watchers_count":49,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":34,"mirror_url":null,"open_issues_count":22,"forks":34,"open_issues":22,"watchers":49,"default_branch":"staging"}},"base":{"label":"code-dot-org:staging","ref":"staging","sha":"04da4a0c7bdf08244bd46733894a6fbcd84c229d","user":{"login":"code-dot-org","id":5124627,"avatar_url":"https://avatars.githubusercontent.com/u/5124627?v=3","gravatar_id":"","url":"https://api.github.com/users/code-dot-org","html_url":"https://github.com/code-dot-org","followers_url":"https://api.github.com/users/code-dot-org/followers","following_url":"https://api.github.com/users/code-dot-org/following{/other_user}","gists_url":"https://api.github.com/users/code-dot-org/gists{/gist_id}","starred_url":"https://api.github.com/users/code-dot-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/code-dot-org/subscriptions","organizations_url":"https://api.github.com/users/code-dot-org/orgs","repos_url":"https://api.github.com/users/code-dot-org/repos","events_url":"https://api.github.com/users/code-dot-org/events{/privacy}","received_events_url":"https://api.github.com/users/code-dot-org/received_events","type":"Organization","site_admin":false},"repo":{"id":24250009,"name":"code-dot-org","full_name":"code-dot-org/code-dot-org","owner":{"login":"code-dot-org","id":5124627,"avatar_url":"https://avatars.githubusercontent.com/u/5124627?v=3","gravatar_id":"","url":"https://api.github.com/users/code-dot-org","html_url":"https://github.com/code-dot-org","followers_url":"https://api.github.com/users/code-dot-org/followers","following_url":"https://api.github.com/users/code-dot-org/following{/other_user}","gists_url":"https://api.github.com/users/code-dot-org/gists{/gist_id}","starred_url":"https://api.github.com/users/code-dot-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/code-dot-org/subscriptions","organizations_url":"https://api.github.com/users/code-dot-org/orgs","repos_url":"https://api.github.com/users/code-dot-org/repos","events_url":"https://api.github.com/users/code-dot-org/events{/privacy}","received_events_url":"https://api.github.com/users/code-dot-org/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/code-dot-org/code-dot-org","description":"The code powering code.org and studio.code.org","fork":false,"url":"https://api.github.com/repos/code-dot-org/code-dot-org","forks_url":"https://api.github.com/repos/code-dot-org/code-dot-org/forks","keys_url":"https://api.github.com/repos/code-dot-org/code-dot-org/keys{/key_id}","collaborators_url":"https://api.github.com/repos/code-dot-org/code-dot-org/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/code-dot-org/code-dot-org/teams","hooks_url":"https://api.github.com/repos/code-dot-org/code-dot-org/hooks","issue_events_url":"https://api.github.com/repos/code-dot-org/code-dot-org/issues/events{/number}","events_url":"https://api.github.com/repos/code-dot-org/code-dot-org/events","assignees_url":"https://api.github.com/repos/code-dot-org/code-dot-org/assignees{/user}","branches_url":"https://api.github.com/repos/code-dot-org/code-dot-org/branches{/branch}","tags_url":"https://api.github.com/repos/code-dot-org/code-dot-org/tags","blobs_url":"https://api.github.com/repos/code-dot-org/code-dot-org/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/code-dot-org/code-dot-org/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/code-dot-org/code-dot-org/git/refs{/sha}","trees_url":"https://api.github.com/repos/code-dot-org/code-dot-org/git/trees{/sha}","statuses_url":"https://api.github.com/repos/code-dot-org/code-dot-org/statuses/{sha}","languages_url":"https://api.github.com/repos/code-dot-org/code-dot-org/languages","stargazers_url":"https://api.github.com/repos/code-dot-org/code-dot-org/stargazers","contributors_url":"https://api.github.com/repos/code-dot-org/code-dot-org/contributors","subscribers_url":"https://api.github.com/repos/code-dot-org/code-dot-org/subscribers","subscription_url":"https://api.github.com/repos/code-dot-org/code-dot-org/subscription","commits_url":"https://api.github.com/repos/code-dot-org/code-dot-org/commits{/sha}","git_commits_url":"https://api.github.com/repos/code-dot-org/code-dot-org/git/commits{/sha}","comments_url":"https://api.github.com/repos/code-dot-org/code-dot-org/comments{/number}","issue_comment_url":"https://api.github.com/repos/code-dot-org/code-dot-org/issues/comments{/number}","contents_url":"https://api.github.com/repos/code-dot-org/code-dot-org/contents/{+path}","compare_url":"https://api.github.com/repos/code-dot-org/code-dot-org/compare/{base}...{head}","merges_url":"https://api.github.com/repos/code-dot-org/code-dot-org/merges","archive_url":"https://api.github.com/repos/code-dot-org/code-dot-org/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/code-dot-org/code-dot-org/downloads","issues_url":"https://api.github.com/repos/code-dot-org/code-dot-org/issues{/number}","pulls_url":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls{/number}","milestones_url":"https://api.github.com/repos/code-dot-org/code-dot-org/milestones{/number}","notifications_url":"https://api.github.com/repos/code-dot-org/code-dot-org/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/code-dot-org/code-dot-org/labels{/name}","releases_url":"https://api.github.com/repos/code-dot-org/code-dot-org/releases{/id}","created_at":"2014-09-20T00:46:11Z","updated_at":"2015-07-11T11:09:37Z","pushed_at":"2015-07-16T18:25:57Z","git_url":"git://github.com/code-dot-org/code-dot-org.git","ssh_url":"git@github.com:code-dot-org/code-dot-org.git","clone_url":"https://github.com/code-dot-org/code-dot-org.git","svn_url":"https://github.com/code-dot-org/code-dot-org","homepage":"http://code.org","size":2326247,"stargazers_count":49,"watchers_count":49,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":34,"mirror_url":null,"open_issues_count":22,"forks":34,"open_issues":22,"watchers":49,"default_branch":"staging"}},"_links":{"self":{"href":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls/3134"},"html":{"href":"https://github.com/code-dot-org/code-dot-org/pull/3134"},"issue":{"href":"https://api.github.com/repos/code-dot-org/code-dot-org/issues/3134"},"comments":{"href":"https://api.github.com/repos/code-dot-org/code-dot-org/issues/3134/comments"},"review_comments":{"href":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls/3134/comments"},"review_comment":{"href":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/code-dot-org/code-dot-org/pulls/3134/commits"},"statuses":{"href":"https://api.github.com/repos/code-dot-org/code-dot-org/statuses/b15c9f440a3f6dd9dc4978704d7fa1fcb62a70d7"}}}}