lib/vimgolf/cli.rb in vimgolf-0.1.3 vs lib/vimgolf/cli.rb in vimgolf-0.2.0

- old
+ new

@@ -1,9 +1,11 @@ module VimGolf - GOLFHOST = ENV['GOLFHOST'] || "http://vimgolf.com" + GOLFHOST = ENV['GOLFHOST'] || "http://vimgolf.com" GOLFDEBUG = ENV['GOLFDEBUG'].to_sym rescue false + DIFF = ENV['DIFF'] || 'diff' + PROXY = ENV['http_proxy'] || '' class Error end class UI @@ -70,32 +72,32 @@ vimcmd = "vim -n --noplugin -i NONE +0 -u #{vimrc(id)} -W #{log(id)} #{input(id, type)}" debug(vimcmd) system(vimcmd) if $?.exitstatus.zero? - diff = `diff --strip-trailing-cr #{input(id, type)} #{output(id)}` + diff = `#{DIFF} #{input(id, type)} #{output(id)}` if diff.size > 0 VimGolf.ui.warn "Uh oh, looks like your entry does not match the desired output:" VimGolf.ui.warn "#"*50 puts diff VimGolf.ui.warn "#"*50 VimGolf.ui.warn "Please try again!" return end - score = File.size(log(id)) + score = Keylog.score(IO.read(log(id))) VimGolf.ui.info "Success! Your output matches. Your score: #{score}" if VimGolf.ui.yes? "Upload result to VimGolf? (yes / no)" VimGolf.ui.warn "Uploading to VimGolf..." if upload(id) == :ok VimGolf.ui.info "Uploaded entry, thanks for golfing!" VimGolf.ui.info "View the leaderboard: #{GOLFHOST}/challenges/#{id}" else - VimGolf.ui.error "Uh oh, upload filed. You're not cheating are you? :-)" + VimGolf.ui.error "Uh oh, upload failed. You're not cheating are you? :-)" end else VimGolf.ui.warn "Skipping upload. Thanks for playing. Give it another shot!" end @@ -108,21 +110,25 @@ VimGolf.ui.error error end rescue Exception => e - VimGolf.ui.error "Uh oh, something wen't wrong! Error: #{e}" + VimGolf.ui.error "Uh oh, something went wrong! Error: #{e}" VimGolf.ui.error "If the error persists, please report it to github.com/igrigorik/vimgolf" end end no_tasks do def download(id) begin url = URI.parse("#{GOLFHOST}/challenges/#{id}.json") req = Net::HTTP::Get.new(url.path) - res = Net::HTTP.start(url.host, url.port) {|http| http.request(req)} + + proxy_url, proxy_user, proxy_pass = get_proxy + proxy = Net::HTTP::Proxy(proxy_url.host, proxy_url.port, proxy_user, proxy_pass) + res = proxy.start(url.host, url.port) { |http| http.request(req) } + data = JSON.parse(res.body) if data['client'] != Vimgolf::VERSION VimGolf.ui.error "Client version mismatch. Installed: #{Vimgolf::VERSION}, Required: #{data['client']}." VimGolf.ui.error "\t gem install vimgolf" @@ -142,19 +148,22 @@ end def upload(id) begin url = URI.parse("#{GOLFHOST}/entry.json") - http = Net::HTTP.new(url.host, url.port) - request = Net::HTTP::Post.new(url.request_uri) - request.set_form_data({"challenge_id" => id, "apikey" => Config.load['key'], "entry" => IO.read(log(id))}) - request["Accept"] = "application/json" + proxy_url, proxy_user, proxy_pass = get_proxy + proxy = Net::HTTP::Proxy(proxy_url.host, proxy_url.port, proxy_user, proxy_pass) - res = http.request(request) - JSON.parse(res.body)['status'].to_sym + proxy.start(url.host, url.port) do |http| + request = Net::HTTP::Post.new(url.request_uri) + request.set_form_data({"challenge_id" => id, "apikey" => Config.load['key'], "entry" => IO.read(log(id))}) + request["Accept"] = "application/json" + res = http.request(request) + JSON.parse(res.body)['status'].to_sym + end rescue Exception => e debug(e) raise "Uh oh, entry upload has failed, please check your key." end end @@ -166,9 +175,23 @@ def log(id); challenge(id) + ".log"; end def vimrc(id); challenge(id) + ".golfrc"; end def challenge(id) Config.put_path + "/#{id}" + end + + def get_proxy + begin + proxy_url = URI.parse(PROXY) + rescue Exception => e + VimGolf.ui.error "Invalid proxy uri in http_proxy environment variable - will try to run with out proxy" + proxy_url = URI.parse(""); + end + + proxy_url.port ||= 80 + proxy_user, proxy_pass = proxy_url.userinfo.split(/:/) if proxy_url.userinfo + + return proxy_url, proxy_user, proxy_pass end def debug(msg) p [caller.first, msg] if GOLFDEBUG end \ No newline at end of file