forked from Earendil95/test-maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.rb
47 lines (39 loc) · 1.24 KB
/
service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
module Service
require './services/converter'
require './services/question'
def convert(quiz, options = {})
strings = quiz.split(/[\n\r]/).map do |string|
string.gsub(/[\n\r]/, '')
end
Converter.convert(strings, options)
end
def escape_regexp_chars(str)
pattern = /[\.\+\*\?\(\)]/
str.gsub(pattern) { |match| '\\' + match }
end
def escape_path_chars(str)
pattern = /[\/\s\\\"]/
str.gsub(pattern, '_')
end
def to_suffix(str)
'((' + escape_regexp_chars(str) + ')?)'
end
def set_quiz_options(quiz)
quiz["theme"] = "result" if quiz["theme"] == ""
quiz["option_correct"] = "+|*" if quiz["option_correct"] == ""
quiz["option_correct"] = to_suffix quiz["option_correct"]
end
def set_path(name)
session[:dirname] ||= SecureRandom.hex(4)
session[:path] = "/upload/#{session[:dirname]}/#{escape_path_chars name}.txt"
session[:filename] = "public" + session[:path]
FileUtils.rm_rf "public/upload/#{session[:dirname]}" if File.exist? "public/upload/" + session[:dirname]
FileUtils.mkdir_p "public/upload/" + session[:dirname]
end
def write_result(questions)
File.write(
session[:filename],
questions.inject("") { |acc, q| acc + q.to_gift_str }
)
end
end