forked from jejacks0n/apitome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs_controller.rb
76 lines (60 loc) · 1.82 KB
/
docs_controller.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
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
class Apitome::DocsController < ActionController::Base
respond_to :json
layout Apitome.configuration.layout
helper_method :resources, :example, :formatted_body, :param_headers, :param_extras, :formatted_readme, :set_example, :id_for
def index
end
def show
end
def body
# make snake case for rails
params["path"].gsub!('-', '_')
# collect all the example bodies
resp = example["requests"].inject([]) {|a, e| a << e["response_body"]}
# return first response in case of index array
respond_with(resp.first)
end
private
def file_for(file)
file = Apitome.configuration.root.join(Apitome.configuration.doc_path, file)
raise Apitome::FileNotFound.new, "Unable to find #{file}" unless File.exists?(file)
File.read(file)
end
def resources
@resources ||= JSON.parse(file_for('index.json'))['resources']
end
def example
@example ||= JSON.parse(file_for("#{params[:path]}.json"))
end
def set_example(resource)
@example = JSON.parse(file_for("#{resource}.json"))
end
def formatted_readme
file = Apitome.configuration.root.join(Apitome.configuration.doc_path, Apitome.configuration.readme)
Kramdown::Document.new(file_for(file)).to_html
end
def formatted_body(body, type)
if type =~ /json/ && body.present?
JSON.pretty_generate(JSON.parse(body))
else
body
end
rescue JSON::ParserError
return body if body == " "
raise JSON::ParserError
end
def param_headers(params)
titles = %w{Name Description}
titles += param_extras(params)
end
def param_extras(params)
extras = []
for param in params
extras += param.reject{ |k,v| %w{name description required scope}.include?(k) }.keys
end
extras.uniq
end
def id_for(str)
str.gsub(/\.json$/, '').underscore.gsub(/[^0-9a-z]+/i, '-')
end
end