-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_app.rb
More file actions
97 lines (83 loc) · 1.85 KB
/
simple_app.rb
File metadata and controls
97 lines (83 loc) · 1.85 KB
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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# frozen_string_literal: true
require 'json'
require 'tilt'
require 'binding_of_caller'
class SimpleApp
HASH = {}
PARAMS = {}
@@not_found_response = '<h1>404 Not found</h1>'
def self.check_path(request_path, hash_key)
request_path_list = request_path.split('/')
hash_keys_list = hash_key[0].split('/')
request_path_list.zip(hash_keys_list) do |x, y|
if x != ''
if x != y
if y.start_with?(':')
y = y.sub(/:/, '')
PARAMS.store(y, x)
else
return false
end
end
end
end
return true
end
def call(env)
request_path = []
request_path.push(env['REQUEST_PATH'])
block = nil
HASH.each do |hash|
request_path.each do |request_path|
block = HASH[hash[0]] if SimpleApp.check_path(request_path, hash)
end
end
if block.nil?
return [
200,
{ 'Content-Type' => 'text/html' },
[@@not_found_response]
]
else
return block&.call
end
end
end
def params
SimpleApp::PARAMS
end
def get(route, &block)
SimpleApp::HASH.store(route, block)
end
def find_template(name)
File.open(name, 'r', &:read)
end
def compile_template(name, format)
variable_value = {}
for valiable in binding.of_caller(2).eval("instance_variables")
value = binding.of_caller(2).eval(valiable.to_s)
p valiable
valiable = valiable.to_s.gsub!(/@/, "")
variable_value[valiable] = value
end
tiltEngine = Tilt[name]
raise ".#{format} template engine not found!" if tiltEngine.nil?
erb_engin = Tilt.new(name)
text(erb_engin.render(Hash, variable_value))
end
def text(txt)
[
200,
{ 'Content-Type' => 'text/html' },
[txt]
]
end
def json(json)
text(JSON.generate(json))
end
def html(name)
compile_template(name, :html)
end
def erb(name)
compile_template(name, :erb)
end