-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathregenerate.rb
52 lines (43 loc) · 1.49 KB
/
regenerate.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
require 'net/http'
require 'json'
def add_simple_schemas(http)
objects = {}
response = http.send_request('GET', 'http://demo.wp-api.org/wp-json/?context=help')
parsed_data = JSON.parse(response.body)
parsed_data['routes'].each {
|route|
if not route[1].key?('schema')
next
end
# make a readable route name from the route regex
route_nicename = route[0].gsub(/\(\?P<(\w+?)>.*?\)/, '<\1>')
# escape the <> as this value is only used in HTML
route[1]['nicename'] = route_nicename.gsub( /</, '<' ).gsub( />/, '>' )
# group the objects by unique schema titles [ post => ..., term => ..., etc ]
if not objects.key?( route[1]['schema']['title'] )
objects[ route[1]['schema']['title'] ] = {
"name" => route[1]['schema']['title'], "routes" => { route_nicename => route[1] },
"schema" => route[1]['schema']
}
else
objects[ route[1]['schema']['title'] ]["routes"][ route_nicename ] = route[1]
end
}
objects.each {
|object|
file = File.new('_data/' + object[0] + '.json', 'w')
file.write(JSON.pretty_generate(object[1]))
}
end
def add_terms_schema(http)
response = http.send_request('OPTIONS', 'http://demo.wp-api.org/wp-json/wp/v2/terms/category')
file = File.new('_data/terms.json', 'w')
parsed_data = JSON.parse(response.body)
# puts data
file.write(JSON.pretty_generate(parsed_data))
end
res = Net::HTTP.start("demo.wp-api.org", 80) {
|http|
add_simple_schemas(http)
#add_terms_schema(http)
}