-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
42 lines (35 loc) · 900 Bytes
/
app.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
require 'sinatra'
require 'cgi'
require 'json'
OPTMAP = {
'i' => Regexp::IGNORECASE,
'x' => Regexp::EXTENDED,
'm' => Regexp::MULTILINE
}
get '/' do
redirect to('/index.html')
end
post '/match' do
return { :result => false}.to_json if empty_fields? ['regex','test']
params.each{ |k,v| params[k] = CGI::unescape(v)}
begin
opts = params['opts'].split(//).map{ |c| OPTMAP[c] }.reduce{ |x, n| x | n }
regexp = Regexp.new "#{params['regex']}"
match = regexp.match(params['test'])
if match
{ :result => true, :match => match.to_s, :captures => params['test'].scan(regexp) }.to_json
else
{ :result => false}.to_json
end
rescue RegexpError => ex
{ :result => false, :error => ex.message}.to_json
end
end
helpers do
def empty_fields? fields
fields.each do |field|
return true if params[field].to_s.empty?
end
false
end
end