Skip to content

Commit

Permalink
Add public demo endpoint. Fixes exercism#212
Browse files Browse the repository at this point in the history
Return the first assignment (README, test suite) for each of the active
languages.

No authentication required.
  • Loading branch information
kytrinyx committed Jul 23, 2013
1 parent 642fcd3 commit 6451442
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/app/api.rb
Original file line number Diff line number Diff line change
@@ -1,5 1,12 @@
class ExercismApp < Sinatra::Base

get '/api/v1/assignments/demo' do
assignments = Exercism.trails.map do |trail|
trail.first_assignment
end
pg :assignments, locals: {assignments: assignments}
end

get '/api/v1/user/assignments/current' do
unless params[:key]
halt 401, {error: "Please provide API key"}.to_json
Expand Down
4 changes: 4 additions & 0 deletions lib/exercism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 45,8 @@ def self.current_curriculum
@curriculum
end

def self.trails
@trails ||= current_curriculum.trails.values
end

end
4 changes: 4 additions & 0 deletions lib/exercism/trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 23,10 @@ def first
exercises.first
end

def first_assignment
assign(first.slug)
end

def successor(exercise)
exercises[exercises.index(exercise) 1] || CompletedExercise.new(language)
end
Expand Down
8 changes: 8 additions & 0 deletions test/app/api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 49,12 @@ def test_submit_beyond_end_of_trail
assert_equal 200, last_response.status
end

def test_fetch_demo
get '/api/v1/assignments/demo'
assert_equal 200, last_response.status

options = {format: :json, :name => 'api_demo'}
Approvals.verify(last_response.body, options)
end

end
32 changes: 32 additions & 0 deletions test/fixtures/approvals/api_demo.approved.json
Original file line number Diff line number Diff line change
@@ -0,0 1,32 @@
{
"assignments": [
{
"track": "ruby",
"slug": "bob",
"readme": "# Bob\n\nBob is a lackadaisical teenager. In conversation, his responses are very limited.\n\nBob answers 'Sure.' if you ask him a question.\n\nHe answers 'Whatever.' if you tell him something.\n\nHe answers 'Woah, chill out!' if you yell at him (ALL CAPS).\n\nHe says 'Fine. Be that way!' if you address him without actually saying anything.\n\n## Instructions\n\nRun the test file, and fix each of the errors in turn. When you get the first test to pass, go to the first pending or skipped test, and make that pass as well. When all of the tests are passing, feel free to submit.\n\nRemember that passing code is just the first step. The goal is to work towards a solution that is as readable and expressive as you can make it.\n\nHave fun!\n\n\n\n## Source\n\nInspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. [view source](http://pine.fm/LearnToProgram/?Chapter=06)\n",
"test_file": "bob_test.rb",
"tests": "require 'minitest/autorun'\nrequire 'minitest/pride'\n\nbegin\n require_relative 'bob'\n\n class TeenagerTest < MiniTest::Unit::TestCase\n attr_reader :teenager\n\n def setup\n @teenager = Bob.new\n end\n\n def test_stating_something\n assert_equal 'Whatever.', teenager.hey('Tom-ay-to, tom-aaaah-to.')\n end\n\n def test_shouting\n skip\n assert_equal 'Woah, chill out!', teenager.hey('WATCH OUT!')\n end\n\n def test_asking_a_question\n skip\n assert_equal 'Sure.', teenager.hey('Does this cryogenic chamber make me look fat?')\n end\n\n def test_talking_forcefully\n skip\n assert_equal 'Whatever.', teenager.hey(\"Let's go make out behind the gym!\")\n end\n\n def test_shouting_numbers\n skip\n assert_equal 'Woah, chill out!', teenager.hey('1, 2, 3 GO!')\n end\n\n def test_shouting_with_special_characters\n skip\n assert_equal 'Woah, chill out!', teenager.hey('ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!')\n end\n\n def test_shouting_with_no_exclamation_mark\n skip\n assert_equal 'Woah, chill out!', teenager.hey('I HATE YOU')\n end\n\n def test_statement_containing_question_mark\n skip\n assert_equal 'Whatever.', teenager.hey('Ending with ? means a question.')\n end\n\n def test_silence\n skip\n assert_equal 'Fine. Be that way.', teenager.hey('')\n end\n\n def test_more_silence\n skip\n assert_equal 'Fine. Be that way.', teenager.hey(nil)\n end\n end\n\nrescue LoadError => e\n\n def explain(something)\n 7.times { puts }\n puts \"Hit enter to continue...\"\n gets\n 7.times { puts }\n puts something\n puts\n end\n\n zomg = <<-ERROR\n######## ######## ######## ####### ######## \n## ## ## ## ## ## ## ## ## \n## ## ## ## ## ## ## ## ## \n###### ######## ######## ## ## ######## \n## ## ## ## ## ## ## ## ## \n## ## ## ## ## ## ## ## ## \n######## ## ## ## ## ####### ## ## \n\n ERROR\n puts\n puts zomg\n\n explain \"I'm going to\\n* show you an error message,\\n* then explain what you're seeing\\n* then tell you how to fix it.\"\n explain \"Seriously, don't freak out. It's not that bad.\"\n explain \"OK, this is it:\\n\\n#{e.backtrace.first} #{e.message}\"\n explain \"First it tells you the name of the file where the error is occurring.\\n\\n\\n\\tbob_test.rb\"\n explain \"Then it tells you which line that error is on.\\n\\n\\n\\tbob_test.rb:5\"\n\n explain \"After that, it tells you the name of the method where the error is occurring.\\n\\n\\n\\tin `require_relative'.\"\n explain \"Next, it tells you exactly what the error is.\\n\\n\\n\\tcannot load such file\"\n explain \"Finally, it tells you which file is missing.\\n\\n\\n\\t/path/to/your/code/ruby/bob/bob\"\n explain \"So the error is that on line 5. What's on line 5?\\n\\n\\n\\trequire_relative 'bob'\"\n explain \"Essentially, when we try to require the file, it says it's not there. You can fix the problem by creating an empty file named bob.rb inside of the ruby/bob directory.\"\n explain \"Now take another look at the error message.\\nDoes it make more sense?\\n\\n\\n#{e.backtrace.first} #{e.message}\"\n 10.times { puts }\n exit!\nend\n"
},
{
"track": "javascript",
"slug": "bob",
"readme": "# Bob\n\nBob is a lackadaisical teenager. In conversation, his responses are very limited.\n\nBob answers 'Sure.' if you ask him a question.\n\nHe answers 'Whatever.' if you tell him something.\n\nHe answers 'Woah, chill out!' if you yell at him (ALL CAPS).\n\nHe says 'Fine. Be that way!' if you address him without actually saying anything.\n\n## Instructions\n\nRun the test file, and fix each of the errors in turn. When you get the first test to pass, go to the first pending or skipped test, and make that pass as well. When all of the tests are passing, feel free to submit.\n\nRemember that passing code is just the first step. The goal is to work towards a solution that is as readable and expressive as you can make it.\n\nHave fun!\n\n\n\n## Source\n\nInspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. [view source](http://pine.fm/LearnToProgram/?Chapter=06)\n",
"test_file": "bob_test.spec.js",
"tests": "require('./bob');\n\ndescribe(\"Bob\", function() {\n var bob = new Bob();\n\n it(\"stating something\", function() {\n var result = bob.hey('Tom-ay-to, tom-aaaah-to.');\n expect(result).toEqual('Whatever');\n });\n\n xit(\"shouting\", function() {\n var result = bob.hey('WATCH OUT!');\n expect(result).toEqual('Woah, chill out!');\n });\n\n xit(\"asking a question\", function() {\n var result = bob.hey('Does this cryogenic chamber make me look fat?');\n expect(result).toEqual('Sure');\n });\n\n xit(\"talking forcefully\", function() {\n var result = bob.hey(\"Let's go make out behind the gym!\");\n expect(result).toEqual('Whatever');\n });\n\n xit(\"shouting numbers\", function() {\n var result = bob.hey('1, 2, 3 GO!');\n expect(result).toEqual('Woah, chill out!');\n });\n\n xit(\"shouting with special characters\", function() {\n var result = bob.hey('ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!');\n expect(result).toEqual('Woah, chill out!');\n });\n\n xit(\"silence\", function() {\n var result = bob.hey('');\n expect(result).toEqual('Fine, be that way.');\n });\n\n});\n"
},
{
"track": "elixir",
"slug": "bob",
"readme": "# Bob\n\nBob is a lackadaisical teenager. In conversation, his responses are very limited.\n\nBob answers 'Sure.' if you ask him a question.\n\nHe answers 'Whatever.' if you tell him something.\n\nHe answers 'Woah, chill out!' if you yell at him (ALL CAPS).\n\nHe says 'Fine. Be that way!' if you address him without actually saying anything.\n\n## Instructions\n\nRun the test file, and fix each of the errors in turn. When you get the first test to pass, go to the first pending or skipped test, and make that pass as well. When all of the tests are passing, feel free to submit.\n\nRemember that passing code is just the first step. The goal is to work towards a solution that is as readable and expressive as you can make it.\n\nHave fun!\n\n\n\n## Source\n\nInspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. [view source](http://pine.fm/LearnToProgram/?Chapter=06)\n",
"test_file": "bob_test.exs",
"tests": "Code.load_file(\"bob.exs\")\nExUnit.start\n\ndefmodule TeenagerTest do\n use ExUnit.Case, async: true\n doctest Teenager\n\n test \"stating something\" do\n assert Teenager.hey(\"Tom-ay-to, tom-aaaah-to.\") == \"Whatever.\"\n end\n\n test \"shouting\" do\n # assert Teenager.hey(\"WATCH OUT!\") == \"Woah, chill out!\"\n end\n\n test \"asking a question\" do\n # assert Teenager.hey(\"Does this cryogenic chamber make me look fat?\") == \"Sure.\"\n end\n\n test \"talking forcefully\" do\n # assert Teenager.hey(\"Let's go make out behind the gym!\") == \"Whatever.\"\n end\n\n test \"shouting numbers\" do\n # assert Teenager.hey(\"1, 2, 3 GO!\") == \"Woah, chill out!\"\n end\n\n test \"shouting with special characters\" do\n # assert Teenager.hey(\"ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!\") == \"Woah, chill out!\"\n end\n\n test \"shouting with no exclamation mark\" do\n # assert Teenager.hey(\"I HATE YOU\") == \"Woah, chill out!\"\n end\n\n test \"statement containing question mark\" do\n # assert Teenager.hey(\"Ending with ? means a question.\") == \"Whatever.\"\n end\n\n test \"silence\" do\n # assert Teenager.hey(\"\") == \"Fine. Be that way.\"\n end\nend\n"
},
{
"track": "clojure",
"slug": "bob",
"readme": "# Bob\n\nBob is a lackadaisical teenager. In conversation, his responses are very limited.\n\nBob answers 'Sure.' if you ask him a question.\n\nHe answers 'Whatever.' if you tell him something.\n\nHe answers 'Woah, chill out!' if you yell at him (ALL CAPS).\n\nHe says 'Fine. Be that way!' if you address him without actually saying anything.\n\n## Instructions\n\nRun the test file, and fix each of the errors in turn. When you get the first test to pass, go to the first pending or skipped test, and make that pass as well. When all of the tests are passing, feel free to submit.\n\nRemember that passing code is just the first step. The goal is to work towards a solution that is as readable and expressive as you can make it.\n\nHave fun!\n\n\n\n## Source\n\nInspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. [view source](http://pine.fm/LearnToProgram/?Chapter=06)\n",
"test_file": "bob_test.clj",
"tests": "(ns bob.test (:use clojure.test))\n(load-file \"bob.clj\")\n\n(deftest responds-to-shouts\n (is (= \"Woah, chill out!\" (bob/response-for \"SHOUTING\"))))\n\n(deftest responds-to-questions\n (is (= \"Sure.\" (bob/response-for \"A question?\"))))\n\n(deftest responds-to-statements\n (is (= \"Whatever.\" (bob/response-for \"A statement.\"))))\n\n(deftest responds-to-silence\n (is (= \"Fine, be that way.\" (bob/response-for \"\"))))\n\n(run-tests)\n"
}
]
}

0 comments on commit 6451442

Please sign in to comment.