lib/exercism/api.rb in exercism-0.0.25 vs lib/exercism/api.rb in exercism-0.0.26

- old
+ new

@@ -30,26 +30,42 @@ end def submit(filename) path = File.join(filename) contents = File.read path - response = conn.post do |req| - req.url endpoint('user/assignments') - req.headers['Accept'] = 'application/json' - req.headers['Content-Type'] = 'application/json' - req.body = {:code => contents, :key => user.key, :path => path}.to_json - end - response + + json_request(:post, 'user/assignments', { + :key => user.key, + :code => contents, + :path => path + }) end + def unsubmit + json_request(:delete, 'user/assignments', { + :key => user.key + }) + end + private def get_and_save(action) response = conn.get do |req| req.url endpoint(action) req.params['key'] = user.key end save response.body + end + + def json_request(verb, path, body) + response = conn.send(verb) do |request| + request.url endpoint(path) + request.headers['Accept'] = 'application/json' + request.headers['Content-Type'] = 'application/json' + request.body = body.to_json + end + + response end def user_agent "github.com/kytrinyx/exercism CLI v#{Exercism::VERSION}" end