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.



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

def input_path
  @input_path
end

#log_pathObject (readonly)

Returns the value of attribute log_path.



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

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.



118
119
120
# File 'lib/vimgolf/challenge.rb', line 118

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.



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

def vimrc_path
  @vimrc_path
end

#work_pathObject (readonly)

Returns the value of attribute work_path.



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

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)


122
123
124
# File 'lib/vimgolf/challenge.rb', line 122

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
75
76
# File 'lib/vimgolf/challenge.rb', line 38

def download
  @remote = true
  begin
    url = URI("#{GOLFHOST}/challenges/#{@id}.json")
    res = Net::HTTP.start(
      url.hostname,
      url.port,
      use_ssl: url.scheme == 'https'
    ) do |http|
      http.request_get(url)
    end

    @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



126
127
128
# File 'lib/vimgolf/challenge.rb', line 126

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

#saveObject



82
83
84
85
# File 'lib/vimgolf/challenge.rb', line 82

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



78
79
80
# File 'lib/vimgolf/challenge.rb', line 78

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

#uploadObject



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

def upload
  begin
    url = URI("#{GOLFHOST}/entry.json")
    res = Net::HTTP.start(
      url.hostname,
      url.port,
      use_ssl: url.scheme == 'https'
    ) do |http|
      http.request_post(
        url,
        URI.encode_www_form(
          "challenge_id" => @id,
          "apikey" => Config.load['key'],
          "entry" => IO.binread(log_path)
        ),
        "Accept" => "application/json"
      )
    end

    res = JSON.parse(res.body)

    raise if !res.is_a? Hash
    res['status'].to_sym
  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