Class: RazyK::WebApp
- Inherits:
-
Object
- Object
- RazyK::WebApp
- Defined in:
- lib/razyk/webapp.rb
Defined Under Namespace
Classes: InputStream
Instance Method Summary collapse
- #call(env) ⇒ Object
- #main_page(req) ⇒ Object
- #not_found(req) ⇒ Object
- #parse(req) ⇒ Object
- #reduce(req) ⇒ Object
- #template(name) ⇒ Object
Instance Method Details
#call(env) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/razyk/webapp.rb', line 99 def call(env) req = Rack::Request.new(env) case req.path when "/" res = main_page(req) when "/parse" res = parse(req) when "/reduce" res = reduce(req) else res = not_found(req) end res.finish end |
#main_page(req) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/razyk/webapp.rb', line 39 def main_page(req) res = Rack::Response.new res.status = 200 res.write File.read(template("main.html")) res end |
#not_found(req) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/razyk/webapp.rb', line 46 def not_found(req) res = Rack::Response.new res.status = 404 res.write(<<-EOF) <html><head><title>#{req.path} is not found</title></head> <body>#{req.path} is not found</body></html> EOF res end |
#parse(req) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/razyk/webapp.rb', line 56 def parse(req) expression = req.params["expression"] || "(OUT (I IN))" memory = {} tree = RazyK::Parser.parse(expression, memory: memory) res = Rack::Response.new res.header["Content-Type"] = "application/json" json_state = JSON::State.from_state(nil) json_state.max_nesting = 0 res.write({ expression: tree.inspect, nodes: tree.as_json, }.to_json(json_state)) res end |
#reduce(req) ⇒ Object
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 |
# File 'lib/razyk/webapp.rb', line 72 def reduce(req) stdin_read = req.params["stdin_read"] || "" stdin_remain = req.params["stdin_remain"] || "" stdout = req.params["stdout"] || "" expression = req.params["expression"] || "(OUT (I IN))" recursive = (req.params["recursive"] == "true") port_in = InputStream.new(stdin_remain) port_out = StringIO.new("") memory = {} tree = RazyK::Parser.parse(expression, memory: memory) vm = VM.new(tree, port_in, port_out, recursive: recursive) vm.reduce res = Rack::Response.new res.header["Content-Type"] = "application/json" json_state = JSON::State.from_state(nil) json_state.max_nesting = 0 res.write({ expression: vm.tree.inspect, nodes: vm.tree.as_json, stdin_read: stdin_read + port_in.wrote, stdin_remain: port_in.remain, stdout: stdout + port_out.string, }.to_json(json_state)) res end |
#template(name) ⇒ Object
35 36 37 |
# File 'lib/razyk/webapp.rb', line 35 def template(name) File.join(File.dirname(File.(__FILE__)), "webapp", "templates", name) end |