-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rb
47 lines (41 loc) · 848 Bytes
/
lib.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
class Palaver
class << self
attr_accessor :knoedel
def define(name=nil, &b)
self.knoedel = {
name: name,
code: b
}
end
end
def define(name=nil, &b)
query = Rack::Utils.parse_query(@env['QUERY_STRING'])
.inject({}) do |obj, (key, value)|
obj[key.to_sym] = value
obj
end
yield(query)
end
def on(segment, &b)
if b.arity > 0
yield(@path.shift) unless @path.empty?
else
if @path.first == segment
@path.shift
yield
end
end
end
def get(&b)
b[@req, @res] if @path.empty?
end
def call(env)
@env = env
@req = Rack::Request.new(env)
@res = Rack::Response.new
@path = env.fetch(Rack::PATH_INFO).split('/')
@path.shift
instance_eval(&self.class.knoedel[:code])
@res.finish
end
end