22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
|
# File 'lib/doublesplat.rb', line 22
def start_watch phrase
directory = "#{`pwd`.strip}"
response = RestClient.get "#{ENDPOINT}/get/#{phrase}"
code = Base64.decode64(JSON.parse(response)['base64'])
File.open("#{directory}/#{phrase}.rb", "w") do |f|
f.puts code
end
puts Rainbow("Open #{directory}/#{phrase}.rb to complete the challenge.").green
puts "\n"
puts Rainbow("Comments within the file will explain your mission").green
listener = Listen.to(directory, only: /#{phrase}\.rb$/) do |modified, added, removed|
unless modified.empty?
new_text = String.new
File.open("#{directory}/#{phrase}.rb").each do |line|
unless line.strip.chars.empty?
new_text << "#{line.strip};"
end
end
new_base_64 = Base64.encode64(new_text)
print "\n--> Running Tests || "
response = RestClient.post "#{ENDPOINT}/test", :phrase => phrase, :code => new_base_64
response_hash = JSON.parse(response)
print Rainbow("#{response_hash['passed_count']} Passed").green
print Rainbow(" | ").yellow
print Rainbow("#{response_hash['failed_count']} Failed").red
if response_hash['failed_count'] > 0 || response_hash['passed_count'] < 1
puts "\n\n"
response_hash['failed_string'].split(",").each do |error|
puts Rainbow(error).red
end
puts Rainbow("Check your code for syntax errors or missing closing indicators.").red if response_hash["passed_count"] < 1
else
puts "\n\n"
puts Rainbow("You did it!!!").white
exit
end
end
end
listener.start
puts "\n"
puts Rainbow("Watching for file changes.").cyan
sleep
end
|