lib/vimgolf/cli.rb in vimgolf-0.2.0 vs lib/vimgolf/cli.rb in vimgolf-0.3.0
- old
+ new
@@ -1,10 +1,11 @@
module VimGolf
GOLFHOST = ENV['GOLFHOST'] || "http://vimgolf.com"
GOLFDEBUG = ENV['GOLFDEBUG'].to_sym rescue false
- DIFF = ENV['DIFF'] || 'diff'
+ GOLFDIFF = ENV['GOFLDIFF'] || 'diff'
+ GOLFVIM = ENV['GOLFVIM'] || 'vim'
PROXY = ENV['http_proxy'] || ''
class Error
end
@@ -30,27 +31,27 @@
end
desc "setup", "configure your VimGolf credentials"
long_desc <<-DESC
To participate in the challenge please go to vimgolf.com and register an
- account. Once signed in, you will get your API token, which you need to
+ account. Once signed in, you will get your API key, which you need to
setup the command client.
- If you have the token, simply run the setup and paste in your token.
+ If you have the key, simply run the setup and paste in your key.
DESC
def setup
- token = VimGolf.ui.ask "Please specify your VimGolf API token (register on vimgolf.com to get it):"
+ key = VimGolf.ui.ask "Please specify your VimGolf API key (register on vimgolf.com to get it):"
- if token =~ /[\w\d]{32}/
+ if key =~ /[\w\d]{32}/
FileUtils.mkdir_p Config.path
FileUtils.mkdir_p Config.put_path
- Config.save({:key => token})
+ Config.save({'key' => key})
VimGolf.ui.info "Saved. Happy golfing!"
else
- VimGolf.ui.error "Invalid token, please double check your token on vimgolf.com"
+ VimGolf.ui.error "Invalid key, please double check your key on vimgolf.com"
end
end
desc "put [ID]", "launch Vim session"
long_desc <<-DESC
@@ -67,27 +68,27 @@
# - n - no swap file, memory only editing
# - +0 - always start on line 0
# - --noplugin - don't load any plugins, lets be fair!
# -i NONE - don't load .viminfo (for saved macros and the like)
# - u - load vimgolf .vimrc to level the playing field
- vimcmd = "vim -n --noplugin -i NONE +0 -u #{vimrc(id)} -W #{log(id)} #{input(id, type)}"
+ vimcmd = "#{GOLFVIM} -n --noplugin -i NONE +0 -u \"#{vimrc(id)}\" -W \"#{log(id)}\" \"#{input(id, type)}\""
debug(vimcmd)
system(vimcmd)
if $?.exitstatus.zero?
- diff = `#{DIFF} #{input(id, type)} #{output(id)}`
+ diff = `#{GOLFDIFF} \"#{input(id, type)}\" \"#{output(id)}\"`
+ score = Keylog.score(IO.read(log(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!"
+ VimGolf.ui.warn "Please try again! Your score for this (failed) attempt was: #{score}"
return
end
- 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..."
@@ -118,20 +119,23 @@
end
no_tasks do
def download(id)
begin
- url = URI.parse("#{GOLFHOST}/challenges/#{id}.json")
+ url = URI.parse("#{GOLFHOST}/challenges/#{id}.yaml")
req = Net::HTTP::Get.new(url.path)
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)
+ data = YAML.load(res.body)
- if data['client'] != Vimgolf::VERSION
+ if !data.is_a? Hash
+ raise
+
+ elsif data['client'] != Vimgolf::VERSION
VimGolf.ui.error "Client version mismatch. Installed: #{Vimgolf::VERSION}, Required: #{data['client']}."
VimGolf.ui.error "\t gem install vimgolf"
raise "Bad Version"
end
@@ -147,21 +151,25 @@
end
end
def upload(id)
begin
- url = URI.parse("#{GOLFHOST}/entry.json")
+ url = URI.parse("#{GOLFHOST}/entry.yaml")
proxy_url, proxy_user, proxy_pass = get_proxy
proxy = Net::HTTP::Proxy(proxy_url.host, proxy_url.port, proxy_user, proxy_pass)
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"
+ request["Accept"] = "text/yaml"
res = http.request(request)
- JSON.parse(res.body)['status'].to_sym
+ res = YAML.load(res.body)
+
+ raise if !res.is_a? Hash
+ res['status'].to_sym
+
end
rescue Exception => e
debug(e)
raise "Uh oh, entry upload has failed, please check your key."
end
\ No newline at end of file