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
|
# File 'lib/qdocs/server.rb', line 5
def call(env)
req = Rack::Request.new(env)
params = req.params
case env["REQUEST_PATH"]
when "/"
resp = Qdocs.lookup(params["input"])
format = req.env.fetch("HTTP_ACCEPT", "")
body, content_type = case format
when %r{text/html}
require "erb"
template = case resp[:query_type]
when :methods
"constant/show"
when :instance_method, :singleton_method, :class_method, :method
"method/show"
when :active_record_attribute
"method/active_record_show"
when :constant, :active_record_class
"constant/show"
end
[render_html(resp, template), "text/html"]
else
[JSON.pretty_generate(resp), "application/json"]
end
[200, { "Content-Type" => "#{content_type}" }, [body]]
else
[404, { "Content-Type" => "text/plain; charset=utf-8" }, ["Not Found"]]
end
rescue Qdocs::UnknownClassError,
Qdocs::UnknownMethodTypeError,
Qdocs::UnknownMethodError,
Qdocs::UnknownPatternError => e
[404, { "Content-Type" => "text/plain; charset=utf-8" }, ["Not found: #{e.message}"]]
rescue => e
pp e.backtrace
[500, { "Content-Type" => "text/plain; charset=utf-8" }, ["Error: #{e.message}"]]
end
|