forked from adamlaska/circleci-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_endpoint.rb
More file actions
84 lines (75 loc) · 2.04 KB
/
api_endpoint.rb
File metadata and controls
84 lines (75 loc) · 2.04 KB
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
77
78
79
80
81
82
83
84
module Jekyll
module ApiEndpointFilter
def api_endpoint(endpoint)
if endpoint
"""<div>
<p>#{endpoint['description']}</p>
<h4>Method</h4>
<p>#{endpoint['method']}</p>
#{params(endpoint['params'])}
<h4>Example call</h4>
<pre><code>#{api_curl(endpoint)}</code></pre>
<h4>Example response</h4>
#{response_output(endpoint['response'])}
#{try_it(endpoint)}
</div>
"""
end
end
private
def response_output(resp)
if resp.to_s.strip.empty?
'[no response expected]'
else
"<pre><code>#{resp}</code></pre>"
end
end
def curl_args_padded(endpoint)
args = ['--header "Circle-Token: <circle-token>"']
args << "-X #{endpoint['method']}" if not endpoint['method'] == 'GET'
args << '--header "Content-Type: application/json"' if endpoint['body']
if endpoint['body']
subbed_body = endpoint['body'].gsub("'", "\\'")
args << "-d '#{subbed_body}'"
end
if args.empty?
''
else
args.join(' ') + ' '
end
end
def curl_params(endpoint)
params = endpoint['params']
if params and (endpoint['method'] == 'GET')
'?' + params.map {|param| "#{param['name']}=#{param['example']}"}.join('&')
end
end
def api_curl(endpoint)
"curl #{curl_args_padded(endpoint)}https://circleci.com#{endpoint['url']}#{curl_params(endpoint)}"
end
def params(params)
if params
"""<div>
<table class='table'>
<thead><tr><th>Parameter</th><th>Description</th></tr></thead>
<tbody>
#{param_rows(params)}
</tbody>
</table>
</div>
"""
end
end
def param_rows(params)
if params
params.map {|param| "<tr><td>#{param['name']}</td><td>#{param['description']}</td></tr>"}.join
end
end
def try_it(endpoint)
if endpoint['try_it']
"<p><a href=\"https://circleci.com#{endpoint['url']}\" target=\"blank\">Try it in your browser</a></p>"
end
end
end
end
Liquid::Template.register_filter(Jekyll::ApiEndpointFilter)