Skip to content
Browse files

json config, upload logic

  • Loading branch information...
1 parent 9f97188 commit 194ff61036a17167361bc07cc8b16fd113e4ef88 @igrigorik igrigorik committed Dec 27, 2010
Showing with 79 additions and 22 deletions.
  1. +1 −1 Gemfile
  2. +27 −0 Gemfile.lock
  3. +0 −1 lib/vimgolf.rb
  4. +47 −16 lib/vimgolf/cli.rb
  5. +4 −4 lib/vimgolf/config.rb
View
2 Gemfile
@@ -1,4 +1,4 @@
source "http://rubygems.org"
# Specify your gem's dependencies in vimgolf.gemspec
-gemspec
+gemspec
View
27 Gemfile.lock
@@ -0,0 +1,27 @@
+PATH
+ remote: .
+ specs:
+ vimgolf (0.0.1)
+ thor
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ diff-lcs (1.1.2)
+ rspec (2.3.0)
+ rspec-core (~> 2.3.0)
+ rspec-expectations (~> 2.3.0)
+ rspec-mocks (~> 2.3.0)
+ rspec-core (2.3.1)
+ rspec-expectations (2.3.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.3.0)
+ thor (0.14.6)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rspec
+ thor
+ vimgolf!
View
1 lib/vimgolf.rb
@@ -1,6 +1,5 @@
require 'fileutils'
require 'net/http'
-require 'yaml'
require 'json'
require 'thor'
View
63 lib/vimgolf/cli.rb
@@ -1,4 +1,7 @@
module VimGolf
+
+ GOLFHOST = ENV['GOLFHOST'] || "http://www.vimgolf.com"
+
class Error
end
@@ -57,27 +60,36 @@ def put(id = nil)
if download(id) == :ok
- # TODO:
- # 1. query vimgolf site for stated ID - download challenge + final
- # 2. ...
- # 3. diff the files, if same, then score
- # 4. upload to vimgolf.com
-
# - Z - start in restricted mode - no system commands
# - n - no swap file, memory only editing
# - --noplugin - don't load any plugins, lets be fair!
# - +0 - always start on line 0
- system("vim -Z -n --noplugin +0 -W #{challenge_log(id)} #{challenge(id)}")
+ system("vim -Z -n --noplugin +0 -W #{log(id)} #{input(id)}")
if $?.exitstatus.zero?
- score = File.size(challenge_log(id))
- VimGolf.ui.info "Session recorded, your score: #{score}"
+ diff = `diff --strip-trailing-cr #{input(id)} #{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))
+ VimGolf.ui.info "Success! Your output matches. Your score: #{score}"
if VimGolf.ui.yes? "Upload result to VimGolf? (yes / no)"
VimGolf.ui.info "Uploading to VimGolf..."
- # TODO
- VimGolf.ui.info "Uploaded, thanks for golfing!"
+ if upload(id) == :ok
+ VimGolf.ui.info "Uploaded entry, thanks for golfing!"
+ VimGolf.ui.info "View the leader board: http://vimgolf.com/challenges/#{id}"
+ else
+ VimGolf.ui.error "Uh oh, upload to VimGolf failed."
+ end
else
VimGolf.ui.warn "Skipping upload. Thanks for playing. Give it another shot!"
@@ -97,7 +109,7 @@ def put(id = nil)
private
def download(id)
begin
- url = URI.parse("http://localhost:3000/challenges/#{id}.json")
+ 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)}
data = JSON.parse(res.body)
@@ -111,12 +123,31 @@ def download(id)
end
end
- def challenge(name)
- Config.put_path + "/#{name}.input"
+ def upload(id)
+ begin
+ url = URI.parse("#{GOLFHOST}/entry.json")
+ http = Net::HTTP.new(url.host, url.port)
+ res = http.start do |conn|
+ key = Config.load['key']
+ data = "challenge_id=#{id}&entry=#{IO.read(log(id))}&apikey=#{key}"
+ head = {'Accept' => 'application/json'}
+
+ conn.post(url.path, data, head)
+ end
+
+ JSON.parse(res.body)['status'].to_sym
+
+ rescue Exception => e
+ VimGolf.ui.error "Uh oh, entry upload has failed, please check your key."
+ end
end
- def challenge_log(name)
- challenge(name) + ".log"
+ def input(id); challenge(id) + ".input"; end
+ def output(id); challenge(id) + ".output"; end
+ def log(id); challenge(id) + ".log"; end
+
+ def challenge(id)
+ Config.put_path + "/#{id}"
end
end
end
View
8 lib/vimgolf/config.rb
@@ -10,14 +10,14 @@ def put_path
end
def save(conf)
- File.open(path + '/config.yaml', 'w') do |f|
- YAML.dump(conf, f)
+ File.open(path + '/config.json', 'w') do |f|
+ f.puts JSON.generate(conf)
end
end
def load
- File.open(path + '/config.yaml', 'r') do |f|
- YAML.load(f)
+ File.open(path + '/config.json', 'r') do |f|
+ JSON.parse(f.read)
end
end
end

0 comments on commit 194ff61

Please sign in to comment.
Something went wrong with that request. Please try again.