forked from sendgrid/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiexamples.rb
78 lines (60 loc) · 2.24 KB
/
apiexamples.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
77
78
require 'rubygems'
require 'fileutils'
html_files = File.join("/Users/brandonwest/SendGrid/docs/source/API_Reference/", "**", "*.html")
modified = Array.new
unmodified = Array.new
Dir.glob html_files do |html_file|
next if html_file == '.' or html_file == '..' or html_file.match('_layouts') or html_file.match('_includes') or html_file.match('deprecated')
file = File.open(html_file, "r:UTF-8")
html = file.read
file.close
do_it = false
html.gsub!(/{% xmljsontabs (.*?)\s?%}.*?<h3>Call<\/h3>.*?{% codeblock %}(.*?){% endcodeblock %}.*?<h3>Response.*?<\/h3>(.*?)<\/div>.*?<h3>Response.*?<\/h3>.*?{% codeblock.*? %}(.*?)<\/div>.*?<\/div>/m) do |match|
identifier = $1.strip
http_call = $2.strip
first_response = $3.strip
second_response = $4.strip
#https://api.sendgrid.com/apiv2/customer.limit.json?api_user=your_sendgrid_username&api_key=your_sendgrid_password&[email protected]&task=retrieve
url = http_call.slice(0..(http_call.index('?')-1))
data = http_call.slice((http_call.index('?')+1)..-1)
json =$3.index('json') ? $3 : $4
xml = $3.index('json') ? $4 : $3
json.gsub!(/\s*<h3>Response: Success<\/h3>/,"//success")
json.gsub!(/\s*<h3>Response<\/h3>/,'')
json.gsub!(/\s*{% codeblock .*? %}/,"\n")
json.gsub!(/\s*{% endcodeblock %}/,'')
json.gsub!(/\s*<h3>Response: Error<\/h3>/,"\n\n//error")
xml.gsub!(/\s*<h3>Response: Success<\/h3>/,'<!-- success -->')
xml.gsub!(/\s*<h3>Response<\/h3>/,'')
xml.gsub!(/\s*{% codeblock .*? %}/,"\n")
xml.gsub!(/\s*{% endcodeblock %}/,'')
xml.gsub!(/\s*<h3>Response: Error<\/h3>/,"\n\n<!-- error -->")
output = <<-HTML
{% apiexample #{identifier} POST #{url} #{data} %}
{% response json %}
#{json.strip}
{% endresponse %}
{% response xml %}
#{xml.strip}
{% endresponse %}
{% endapiexample %}
HTML
do_it = true
output
end
output_path = html_file
if do_it
modified.push html_file
FileUtils.rm_f(output_path)
file = File.new(output_path,"w:UTF-8")
file.write(html)
file.close
else
unmodified.push html_file
end
end
puts "Modified"
modified.each {|i| p i}
puts "---------------------------------------------------------------------"
puts "Skipped"
unmodified.each {|i| p i}