Class: VimGolf::Challenge

Inherits:
Object
  • Object
show all
Defined in:
lib/vimgolf/challenge.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Challenge

Returns a new instance of Challenge.



10
11
12
# File 'lib/vimgolf/challenge.rb', line 10

def initialize(id)
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/vimgolf/challenge.rb', line 3

def id
  @id
end

#input_pathObject (readonly)

Returns the value of attribute input_path.



110
111
112
# File 'lib/vimgolf/challenge.rb', line 110

def input_path
  @input_path
end

#log_pathObject (readonly)

Returns the value of attribute log_path.



113
114
115
# File 'lib/vimgolf/challenge.rb', line 113

def log_path
  @log_path
end

#otypeObject (readonly)

Returns the value of attribute otype.



3
4
5
# File 'lib/vimgolf/challenge.rb', line 3

def otype
  @otype
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



112
113
114
# File 'lib/vimgolf/challenge.rb', line 112

def output_path
  @output_path
end

#remoteObject (readonly)

Returns the value of attribute remote.



3
4
5
# File 'lib/vimgolf/challenge.rb', line 3

def remote
  @remote
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/vimgolf/challenge.rb', line 3

def type
  @type
end

#vimrc_pathObject (readonly)

Returns the value of attribute vimrc_path.



114
115
116
# File 'lib/vimgolf/challenge.rb', line 114

def vimrc_path
  @vimrc_path
end

#work_pathObject (readonly)

Returns the value of attribute work_path.



111
112
113
# File 'lib/vimgolf/challenge.rb', line 111

def work_path
  @work_path
end

Class Method Details

.path(path) ⇒ Object



5
6
7
8
# File 'lib/vimgolf/challenge.rb', line 5

def self.path(path)
  @@path = path if path
  @@path
end

Instance Method Details

#correct?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/vimgolf/challenge.rb', line 116

def correct?
  FileUtils.compare_file(@work_path, @output_path)
end

#downloadObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vimgolf/challenge.rb', line 38

def download
  @remote = true
  begin
    url = URI.parse("#{GOLFHOST}/challenges/#{@id}.json")
    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)

    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

    @data['in']['data'].gsub!(/\r\n/, "\n")
    @data['out']['data'].gsub!(/\r\n/, "\n")

    # be sure to sanitize the types
    @type = @data['in']['type'].gsub(/[^\w-]/, '.')
    @otype = @data['out']['type'].gsub(/[^\w-]/, '.')
    @input_path = path + ".input.#{@type}"
    @output_path = path + ".output.#{@otype}"

    save
    work_files
  rescue Exception => e
    debug(e)
    raise "Uh oh, couldn't download or parse challenge, please verify your challenge id & client version."
  end
end

#local(infile, outfile) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/vimgolf/challenge.rb', line 14

def local(infile, outfile)
  @remote = false
  @input_path = File.expand_path(infile)
  @output_path = File.expand_path(outfile)
  @type = File.basename(@input_path) # extension? use the whole thing
  @otype = File.basename(@output_path)

  work_files
end

#pathObject



120
121
122
# File 'lib/vimgolf/challenge.rb', line 120

def path
  @@path + "/#{@id}"
end

#saveObject



80
81
82
83
# File 'lib/vimgolf/challenge.rb', line 80

def save
  File.open(input_path, "w")  {|f| f.puts @data['in']['data']}
  File.open(output_path, "w") {|f| f.puts @data['out']['data']}
end

#startObject



76
77
78
# File 'lib/vimgolf/challenge.rb', line 76

def start
  FileUtils.cp(@input_path, @work_path)
end

#uploadObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vimgolf/challenge.rb', line 85

def upload
  begin
    url = URI.parse("#{GOLFHOST}/entry.json")

    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_path)})
      request["Accept"] = "application/json"

      res = http.request(request)
      res = JSON.parse(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
end

#work_filesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vimgolf/challenge.rb', line 24

def work_files
  @vimrc_path = File.expand_path('../vimgolf.vimrc', __FILE__)

  # keep these Tempfile's around so they don't unlink
  @work = Tempfile.new(['vimgolf', ".#{@type}"])
  @log = Tempfile.new('golflog')
  # close tmp files, but don't unlink
  @work.close
  @log.close

  @work_path = @work.path()
  @log_path = @log.path()
end