lib/cli.rb in exercism-0.0.13 vs lib/cli.rb in exercism-0.0.14

- old
+ new

@@ -15,47 +15,34 @@ desc "fetch", "Fetch current assignment from exercism.io" method_option :host, aliases: '-h', default: 'http://exercism.io', desc: 'the url of the exercism application' def fetch require 'exercism' - api = Exercism::Api.new(options[:host], Exercism.user, Exercism.project_dir) assignments = api.fetch - if assignments.empty? - puts "No assignments fetched." - else - assignments.each do |assignment| - puts "Fetched #{File.join(assignment.assignment_dir)}" - end - end + report(assignments) end desc "peek", "Fetch upcoming assignment from exercism.io" method_option :host, aliases: '-h', default: 'http://exercism.io', desc: 'the url of the exercism application' def peek require 'exercism' - api = Exercism::Api.new(options[:host], Exercism.user, Exercism.project_dir) assignments = api.peek - if assignments.empty? - puts "No assignments fetched." - else - assignments.each do |assignment| - puts "Fetched #{File.join(assignment.assignment_dir)}" - end - end + report(assignments) end desc "submit FILE", "Submit code to exercism.io on your current assignment" method_option :host, aliases: '-h', default: 'http://exercism.io', desc: 'the url of the exercism application' def submit(file) require 'exercism' path = File.join(FileUtils.pwd, file) begin - Exercism::Api.new(options[:host], Exercism.user).submit(file) + response = Exercism::Api.new(options[:host], Exercism.user).submit(file) puts "Your assignment has been submitted." - puts "Check the website for feedback in a bit." + url = submission_url(response.body, options[:host]) + puts "For feedback on your submission visit #{url}." rescue Exception => e puts "There was an issue with your submission." puts e.message end end @@ -89,9 +76,29 @@ require 'exercism' puts Exercism.user.github_username rescue Errno::ENOENT puts "You are not logged in." + end + +private + def api(host = options[:host]) + Exercism::Api.new(host, Exercism.user, Exercism.project_dir) + end + + def submission_url(response_body, host) + body = JSON.parse(response_body) + "#{host}/user/#{body['language']}/#{body['exercise']}" + end + + def report(assignments) + if assignments.empty? + puts "No assignments fetched." + else + assignments.each do |assignment| + puts "Fetched #{File.join(assignment.assignment_dir)}" + end + end end end end