Skip to content

Commit fb0b2ad

Browse files
committed
Implement module loading
1 parent 30e8bdc commit fb0b2ad

7 files changed

Lines changed: 97 additions & 2 deletions

File tree

lib/syntropy/app.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'qeweney'
44
require 'syntropy/errors'
55
require 'json'
6+
require 'papercraft'
67

78
module Syntropy
89
class App
@@ -118,7 +119,7 @@ def render_entry(req, entry)
118119
entry[:mime_type] ||= Qeweney::MimeTypes[File.extname(entry[:fn])]
119120
req.respond(IO.read(entry[:fn]), 'Content-Type' => entry[:mime_type])
120121
when :markdown
121-
body = render_markdown(markdown)
122+
body = render_markdown(IO.read(entry[:fn]))
122123
req.respond(body, 'Content-Type' => 'text/html')
123124
when :module
124125
ctx = Syntropy::Context.new(req)
@@ -127,5 +128,42 @@ def render_entry(req, entry)
127128
raise "Invalid entry kind"
128129
end
129130
end
131+
132+
def call_module(entry, ctx)
133+
entry[:code] ||= load_module(entry)
134+
if entry[:code] == :invalid
135+
req.respond(nil, ':status' => Qeweney::Status::INTERNAL_SERVER_ERROR)
136+
return
137+
end
138+
139+
entry[:code].call(ctx)
140+
rescue StandardError => e
141+
p e
142+
p e.backtrace
143+
req.respond(nil, ':status' => Qeweney::S*tatus::INTERNAL_SERVER_ERROR)
144+
end
145+
146+
def load_module(entry)
147+
body = IO.read(entry[:fn])
148+
klass = Class.new
149+
o = klass.instance_eval(body, entry[:fn], 1)
150+
151+
if o.is_a?(Papercraft::HTML)
152+
return wrap_template(o)
153+
else
154+
return o
155+
end
156+
end
157+
158+
def wrap_template(templ)
159+
->(ctx) {
160+
body = templ.render
161+
req.respond(body, 'Content-Type' => 'text/html')
162+
}
163+
end
164+
165+
def render_markdown(str)
166+
Papercraft.markdown(str)
167+
end
130168
end
131169
end

test/app/_layout/default.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
h1 'Foo'
44
}
55
content {
6-
emit_yield **props
6+
emit_yield(**props)
77
}
88
}

test/app/about/foo.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello from Markdown

test/app/about/index.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
->(ctx) {
2+
ctx.request.respond('About')
3+
}

test/app/api+.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class API < Syntropy::RPCAPI
2+
def initialize
3+
@count = 0
4+
end
5+
6+
def get(ctx)
7+
@count
8+
end
9+
10+
def incr!(ctx)
11+
if ctx.request.path != '/test/api'
12+
raise Syntropy::Error.new(Qeweney::Status::TEAPOT, 'Teapot')
13+
end
14+
15+
@count += 1
16+
end
17+
end
18+
19+
API.new

test/app/bar.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
->(ctx) {
2+
ctx.request.respond('foobar')
3+
}

test/test_app.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ def test_find_route
4545
entry = @app.find_route('/test/api_1')
4646
assert_equal :not_found, entry[:kind]
4747

48+
entry = @app.find_route('/test/about/foo')
49+
assert_equal :markdown, entry[:kind]
50+
assert_equal full_path('about/foo.md'), entry[:fn]
51+
4852
pp @app.route_cache
4953
end
5054

@@ -73,5 +77,32 @@ def test_app_rendering
7377

7478
req = make_request(':method' => 'GET', ':path' => '/assets/style.css')
7579
assert_equal Qeweney::Status::NOT_FOUND, req.response_status
80+
81+
req = make_request(':method' => 'GET', ':path' => '/test/api?q=get')
82+
assert_equal({ status: 'OK', response: 0 }, req.response_json)
83+
84+
req = make_request(':method' => 'GET', ':path' => '/test/api/foo?q=get')
85+
assert_equal({ status: 'OK', response: 0 }, req.response_json)
86+
87+
req = make_request(':method' => 'POST', ':path' => '/test/api?q=incr')
88+
assert_equal({ status: 'OK', response: 1 }, req.response_json)
89+
90+
req = make_request(':method' => 'POST', ':path' => '/test/api/foo?q=incr')
91+
assert_equal({ status: 'Syntropy::Error', message: 'Teapot' }, req.response_json)
92+
assert_equal Qeweney::Status::TEAPOT, req.response_status
93+
94+
req = make_request(':method' => 'GET', ':path' => '/test/bar')
95+
assert_equal 'foobar', req.response_body
96+
97+
req = make_request(':method' => 'GET', ':path' => '/test/about')
98+
assert_equal 'About', req.response_body.chomp
99+
100+
req = make_request(':method' => 'GET', ':path' => '/test/about/foo')
101+
assert_equal '<p>Hello from Markdown</p>', req.response_body.chomp
102+
103+
req = make_request(':method' => 'GET', ':path' => '/test/about/foo/bar')
104+
assert_equal Qeweney::Status::NOT_FOUND, req.response_status
105+
106+
76107
end
77108
end

0 commit comments

Comments
 (0)