Skip to content
This repository has been archived by the owner on Jan 17, 2019. It is now read-only.

Commit

Permalink
Implement restore endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kytrinyx committed Mar 1, 2014
1 parent 5c12996 commit 3c93af7
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 10 deletions.
8 changes: 8 additions & 0 deletions lib/app/routes/exercises.rb
Expand Up @@ -9,6 +9,14 @@ class Exercises < Core
pg :exercises, locals: {exercises: Xapi::UserHomework.exercises_for(params[:key])}
end

get '/exercises/restore' do
unless params[:key]
halt 401, {error: "Please provide your Exercism.io API key"}.to_json
end

pg :exercises, locals: {exercises: Xapi::UserHomework.restore(params[:key])}
end

get '/exercises/:language/:slug' do |language, slug|
exercise = Exercise.new(language, slug)
if exercise.unknown_language?
Expand Down
2 changes: 1 addition & 1 deletion lib/xapi/exercism_io.rb
Expand Up @@ -20,7 +20,7 @@ def self.exercises_for(key)
end

def self.code_for(key)
request '/api/v1/iterations/latest', key
request('/api/v1/iterations/latest', key)["assignments"]
end

def self.conn
Expand Down
3 changes: 3 additions & 0 deletions lib/xapi/iteration.rb
@@ -0,0 +1,3 @@
module Xapi
Iteration = OpenStruct
end
15 changes: 14 additions & 1 deletion lib/xapi/user_homework.rb
Expand Up @@ -2,7 +2,20 @@ module Xapi
module UserHomework
def self.exercises_for(key)
data = ExercismIO.exercises_for(key)
Homework.new(data, Xapi::Config.languages, Progression).exercises.sort_by {|exercise| [exercise.language, exercise.slug]}
Homework.new(data, Xapi::Config.languages, Progression).exercises.sort_by {|exercise|
[exercise.language, exercise.slug]
}
end

def self.code_for(key)
iterations = ExercismIO.code_for(key)
iterations.map {|iteration| Iteration.new(iteration)}.sort_by {|iteration|
[iteration.language, iteration.slug]
}
end

def self.restore(key)
exercises_for(key) + code_for(key)
end
end
end
11 changes: 10 additions & 1 deletion test/app/routes/exercises_test.rb
Expand Up @@ -32,13 +32,22 @@ def test_require_key_to_fetch_exercises
assert_equal 401, last_response.status
end

# Acceptance test. Relies on real language-specific data.
# Acceptance tests. Relies on real language-specific data.
# Expect it to fail regularly, since exercises get updated fairly frequently.

def test_get_all_exercises
VCR.use_cassette('exercism_api_exercises') do
get '/exercises', :key => 'abc123'
options = {:format => :json, :name => 'get_all_exercises'}
Approvals.verify(last_response.body, options)
end
end

def test_restore_exercises_and_solutions
VCR.use_cassette('exercism_api_restore') do
get '/exercises/restore', :key => 'abc123'
options = {:format => :json, :name => 'restore'}
Approvals.verify(last_response.body, options)
end
end
end
203 changes: 203 additions & 0 deletions test/fixtures/approvals/restore.approved.json

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions test/fixtures/vcr_cassettes/exercism_api_restore.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions test/xapi/exercism_io_test.rb
Expand Up @@ -18,13 +18,11 @@ def test_exercises

def test_code
VCR.use_cassette('exercism_api_code') do
expected = {
"assignments" => [
{"slug" => "leap", "track" => "go", "files" => {"one.go" => "// iteration 2 (done)"}},
{"slug" => "anagram", "track" => "ruby", "files" => {"one.rb" => "// iteration 1 (pending)"}},
{"slug" => "word-count", "track" => "ruby", "files" => {"two.rb" => "// iteration 1 (hibernating)"}}
]
}
expected = [
{"slug" => "leap", "track" => "go", "files" => {"one.go" => "// iteration 2 (done)"}},
{"slug" => "anagram", "track" => "ruby", "files" => {"one.rb" => "// iteration 1 (pending)"}},
{"slug" => "word-count", "track" => "ruby", "files" => {"two.rb" => "// iteration 1 (hibernating)"}}
]
assert_equal expected, Xapi::ExercismIO.code_for('abc123')
end
end
Expand Down

0 comments on commit 3c93af7

Please sign in to comment.