vimgolf 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/vimgolf.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'fileutils'
2
2
  require 'net/http'
3
3
  require 'strscan'
4
- require 'json'
4
+ require 'yaml'
5
5
  require 'thor'
6
6
 
7
7
  require 'vimgolf/version'
data/lib/vimgolf/cli.rb CHANGED
@@ -2,7 +2,8 @@ module VimGolf
2
2
 
3
3
  GOLFHOST = ENV['GOLFHOST'] || "http://vimgolf.com"
4
4
  GOLFDEBUG = ENV['GOLFDEBUG'].to_sym rescue false
5
- DIFF = ENV['DIFF'] || 'diff'
5
+ GOLFDIFF = ENV['GOFLDIFF'] || 'diff'
6
+ GOLFVIM = ENV['GOLFVIM'] || 'vim'
6
7
  PROXY = ENV['http_proxy'] || ''
7
8
 
8
9
  class Error
@@ -32,23 +33,23 @@ module VimGolf
32
33
  desc "setup", "configure your VimGolf credentials"
33
34
  long_desc <<-DESC
34
35
  To participate in the challenge please go to vimgolf.com and register an
35
- account. Once signed in, you will get your API token, which you need to
36
+ account. Once signed in, you will get your API key, which you need to
36
37
  setup the command client.
37
38
 
38
- If you have the token, simply run the setup and paste in your token.
39
+ If you have the key, simply run the setup and paste in your key.
39
40
  DESC
40
41
 
41
42
  def setup
42
- token = VimGolf.ui.ask "Please specify your VimGolf API token (register on vimgolf.com to get it):"
43
+ key = VimGolf.ui.ask "Please specify your VimGolf API key (register on vimgolf.com to get it):"
43
44
 
44
- if token =~ /[\w\d]{32}/
45
+ if key =~ /[\w\d]{32}/
45
46
  FileUtils.mkdir_p Config.path
46
47
  FileUtils.mkdir_p Config.put_path
47
- Config.save({:key => token})
48
+ Config.save({'key' => key})
48
49
 
49
50
  VimGolf.ui.info "Saved. Happy golfing!"
50
51
  else
51
- VimGolf.ui.error "Invalid token, please double check your token on vimgolf.com"
52
+ VimGolf.ui.error "Invalid key, please double check your key on vimgolf.com"
52
53
  end
53
54
  end
54
55
 
@@ -69,23 +70,23 @@ module VimGolf
69
70
  # - --noplugin - don't load any plugins, lets be fair!
70
71
  # -i NONE - don't load .viminfo (for saved macros and the like)
71
72
  # - u - load vimgolf .vimrc to level the playing field
72
- vimcmd = "vim -n --noplugin -i NONE +0 -u #{vimrc(id)} -W #{log(id)} #{input(id, type)}"
73
+ vimcmd = "#{GOLFVIM} -n --noplugin -i NONE +0 -u \"#{vimrc(id)}\" -W \"#{log(id)}\" \"#{input(id, type)}\""
73
74
  debug(vimcmd)
74
75
  system(vimcmd)
75
76
 
76
77
  if $?.exitstatus.zero?
77
- diff = `#{DIFF} #{input(id, type)} #{output(id)}`
78
+ diff = `#{GOLFDIFF} \"#{input(id, type)}\" \"#{output(id)}\"`
79
+ score = Keylog.score(IO.read(log(id)))
78
80
 
79
81
  if diff.size > 0
80
82
  VimGolf.ui.warn "Uh oh, looks like your entry does not match the desired output:"
81
83
  VimGolf.ui.warn "#"*50
82
84
  puts diff
83
85
  VimGolf.ui.warn "#"*50
84
- VimGolf.ui.warn "Please try again!"
86
+ VimGolf.ui.warn "Please try again! Your score for this (failed) attempt was: #{score}"
85
87
  return
86
88
  end
87
89
 
88
- score = Keylog.score(IO.read(log(id)))
89
90
  VimGolf.ui.info "Success! Your output matches. Your score: #{score}"
90
91
 
91
92
  if VimGolf.ui.yes? "Upload result to VimGolf? (yes / no)"
@@ -120,16 +121,19 @@ module VimGolf
120
121
  no_tasks do
121
122
  def download(id)
122
123
  begin
123
- url = URI.parse("#{GOLFHOST}/challenges/#{id}.json")
124
+ url = URI.parse("#{GOLFHOST}/challenges/#{id}.yaml")
124
125
  req = Net::HTTP::Get.new(url.path)
125
126
 
126
127
  proxy_url, proxy_user, proxy_pass = get_proxy
127
128
  proxy = Net::HTTP::Proxy(proxy_url.host, proxy_url.port, proxy_user, proxy_pass)
128
129
  res = proxy.start(url.host, url.port) { |http| http.request(req) }
129
130
 
130
- data = JSON.parse(res.body)
131
+ data = YAML.load(res.body)
132
+
133
+ if !data.is_a? Hash
134
+ raise
131
135
 
132
- if data['client'] != Vimgolf::VERSION
136
+ elsif data['client'] != Vimgolf::VERSION
133
137
  VimGolf.ui.error "Client version mismatch. Installed: #{Vimgolf::VERSION}, Required: #{data['client']}."
134
138
  VimGolf.ui.error "\t gem install vimgolf"
135
139
  raise "Bad Version"
@@ -149,7 +153,7 @@ module VimGolf
149
153
 
150
154
  def upload(id)
151
155
  begin
152
- url = URI.parse("#{GOLFHOST}/entry.json")
156
+ url = URI.parse("#{GOLFHOST}/entry.yaml")
153
157
 
154
158
  proxy_url, proxy_user, proxy_pass = get_proxy
155
159
  proxy = Net::HTTP::Proxy(proxy_url.host, proxy_url.port, proxy_user, proxy_pass)
@@ -157,10 +161,14 @@ module VimGolf
157
161
  proxy.start(url.host, url.port) do |http|
158
162
  request = Net::HTTP::Post.new(url.request_uri)
159
163
  request.set_form_data({"challenge_id" => id, "apikey" => Config.load['key'], "entry" => IO.read(log(id))})
160
- request["Accept"] = "application/json"
164
+ request["Accept"] = "text/yaml"
161
165
 
162
166
  res = http.request(request)
163
- JSON.parse(res.body)['status'].to_sym
167
+ res = YAML.load(res.body)
168
+
169
+ raise if !res.is_a? Hash
170
+ res['status'].to_sym
171
+
164
172
  end
165
173
  rescue Exception => e
166
174
  debug(e)
@@ -10,14 +10,14 @@ module VimGolf
10
10
  end
11
11
 
12
12
  def save(conf)
13
- File.open(path + '/config.json', 'w') do |f|
14
- f.puts JSON.generate(conf)
13
+ File.open(path + '/config.yaml', 'w') do |f|
14
+ YAML.dump(conf, f)
15
15
  end
16
16
  end
17
17
 
18
18
  def load
19
- File.open(path + '/config.json', 'r') do |f|
20
- JSON.parse(f.read)
19
+ File.open(path + '/config.yaml', 'r') do |f|
20
+ YAML.load(f)
21
21
  end
22
22
  end
23
23
  end
@@ -1,3 +1,3 @@
1
1
  module Vimgolf
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/vimgolf.gemspec CHANGED
@@ -30,6 +30,9 @@ Thank you for installing vimgolf-#{Vimgolf::VERSION}.
30
30
 
31
31
  0.1.3: custom vimgolf .vimrc file to help level the playing field
32
32
  0.2.0: proxy support, custom diffs + proper vimscript parser/scoring
33
+ 0.3.0: improve windows support, switch to YAML to remove c-ext dependency
34
+
35
+ *NOTE*: please re-run "vimgolf setup" prior to playing!
33
36
 
34
37
  For more information, rules & updates: http://vimgolf.com/about
35
38
  ------------------------------------------------------------------------------
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ilya Grigorik
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-31 00:00:00 -05:00
17
+ date: 2011-01-06 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -109,9 +109,11 @@ licenses: []
109
109
 
110
110
  post_install_message: "\n\
111
111
  ------------------------------------------------------------------------------\n\
112
- Thank you for installing vimgolf-0.2.0. \n\n\
112
+ Thank you for installing vimgolf-0.3.0. \n\n\
113
113
  0.1.3: custom vimgolf .vimrc file to help level the playing field\n\
114
- 0.2.0: proxy support, custom diffs + proper vimscript parser/scoring\n\n\
114
+ 0.2.0: proxy support, custom diffs + proper vimscript parser/scoring\n\
115
+ 0.3.0: improve windows support, switch to YAML to remove c-ext dependency\n\n\
116
+ *NOTE*: please re-run \"vimgolf setup\" prior to playing!\n\n\
115
117
  For more information, rules & updates: http://vimgolf.com/about\n\
116
118
  ------------------------------------------------------------------------------\n"
117
119
  rdoc_options: []