prmd 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/CONTRIBUTING.md +6 -6
- data/CONTRIBUTORS.md +3 -0
- data/Gemfile.lock +3 -1
- data/Rakefile +1 -1
- data/bin/prmd +6 -3
- data/docs/schemata.md +4 -2
- data/lib/prmd/commands/combine.rb +22 -9
- data/lib/prmd/commands/render.rb +9 -1
- data/lib/prmd/commands/verify.rb +38 -75
- data/lib/prmd/schema.rb +25 -15
- data/lib/prmd/template.rb +19 -0
- data/lib/prmd/templates/schema.erb +14 -8
- data/lib/prmd/templates/schemata/helper.erb +80 -0
- data/lib/prmd/templates/schemata/link.erb +58 -0
- data/lib/prmd/templates/schemata/link_curl_example.erb +25 -0
- data/lib/prmd/templates/schemata.erb +17 -141
- data/lib/prmd/version.rb +1 -1
- data/lib/prmd.rb +1 -0
- data/prmd.gemspec +1 -0
- data/schemas/hyper-schema.json +168 -0
- data/schemas/interagent-hyper-schema.json +175 -0
- data/schemas/schema.json +150 -0
- data/test/commands/verify_test.rb +187 -0
- data/test/helpers.rb +2 -1
- data/test/schema_test.rb +12 -12
- data/test/{schemas → schemata}/input/meta.json +1 -0
- data/test/{schemas → schemata}/input/user.json +1 -0
- metadata +29 -6
checksums.yaml
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
---
|
|
2
2
|
!binary "U0hBMQ==":
|
|
3
3
|
metadata.gz: !binary |-
|
|
4
|
-
|
|
4
|
+
YTBiMDJmODY4NDAwMTE1ZTMzZTJiY2I2MzA1M2VkMzM2OWZkYmI5NA==
|
|
5
5
|
data.tar.gz: !binary |-
|
|
6
|
-
|
|
6
|
+
MTc2MjY0MjgzMDEwYzhhYWVmYzZkY2VhNjI0YmM3ZmI4MThhYzFlZA==
|
|
7
7
|
SHA512:
|
|
8
8
|
metadata.gz: !binary |-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
ZDJkMzVkZTk4ZTI1YmU5M2Q2MGVmNzZhZjI0YzFmODYwNDE0YThlYjA1MTYw
|
|
10
|
+
NDI0ZDNhYmFjOTczNTllOTBjZDgyNDYwZGQyMzZhYmVlMzZlMTFjZTIzYWVm
|
|
11
|
+
MmNjOTA0MTIzMWRlNjNmMDU5YTQ3YjZkZjQyZGQ1MzY1YWNlNGI=
|
|
12
12
|
data.tar.gz: !binary |-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
NmI3N2ViOGIyZDE1NTljODExNDhkY2E2MDlmZThhZTFiZWE2YmFhNmQyNTg0
|
|
14
|
+
MTBjYzczMjg4NDI1OGVjODlkOTI5NzU5YjllYmMyZGJjYzQ2N2I1YWZlMGM2
|
|
15
|
+
MzY3YzIwM2RjYTlkNzk5MzExNjU0NjAxZDUzYTIzODlkZDQ2YjY=
|
data/CONTRIBUTING.md
CHANGED
|
@@ -5,14 +5,14 @@ New contributors are always welcome, when it doubt please ask questions. We stri
|
|
|
5
5
|
### Coding
|
|
6
6
|
|
|
7
7
|
* Pick a task:
|
|
8
|
-
* Offer feedback on open [pull requests](https://github.com/
|
|
9
|
-
* Review open [issues](https://github.com/
|
|
10
|
-
* [Create an issue](https://github.com/
|
|
8
|
+
* Offer feedback on open [pull requests](https://github.com/interagent/prmd/pulls).
|
|
9
|
+
* Review open [issues](https://github.com/interagent/prmd/issues) for things to help on.
|
|
10
|
+
* [Create an issue](https://github.com/interagent/prmd/issues/new) to start a discussion on additions or features.
|
|
11
11
|
* Fork the project, add your changes and tests to cover them in a topic branch.
|
|
12
|
-
* Commit your changes and rebase against `
|
|
13
|
-
* [Submit a pull request](https://github.com/
|
|
12
|
+
* Commit your changes and rebase against `interagent/prmd` to ensure everything is up to date.
|
|
13
|
+
* [Submit a pull request](https://github.com/interagent/prmd/compare/).
|
|
14
14
|
|
|
15
15
|
### Non-Coding
|
|
16
16
|
|
|
17
|
-
* Offer feedback on open [issues](https://github.com/
|
|
17
|
+
* Offer feedback on open [issues](https://github.com/interagent/prmd/issues).
|
|
18
18
|
* Organize or volunteer at events.
|
data/CONTRIBUTORS.md
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
* Brandur <brandur@heroku.com>
|
|
1
2
|
* Brandur <brandur@mutelight.org>
|
|
2
3
|
* Chris Continanza <christopher.continanza@gmail.com>
|
|
3
4
|
* Dane Harrigan <dane.harrigan@gmail.com>
|
|
5
|
+
* Ernesto Jiménez <me@ernesto-jimenez.com>
|
|
6
|
+
* Kousuke Ebihara <Kosuke_Ebihara@voyagegroup.com>
|
|
4
7
|
* Kousuke Ebihara <kousuke@co3k.org>
|
|
5
8
|
* Mark McGranaghan <mmcgrana@gmail.com>
|
|
6
9
|
* Olivier Lance <olance@users.noreply.github.com>
|
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
data/bin/prmd
CHANGED
|
@@ -16,6 +16,9 @@ commands = {
|
|
|
16
16
|
opts.on("-p", "--prepend header,overview", Array, "Prepend files to output") do |p|
|
|
17
17
|
options[:prepend] = p
|
|
18
18
|
end
|
|
19
|
+
opts.on("-c", "--content-type application/json", String, "Content-Type header") do |c|
|
|
20
|
+
options[:content_type] = c
|
|
21
|
+
end
|
|
19
22
|
end,
|
|
20
23
|
init: OptionParser.new do |opts|
|
|
21
24
|
opts.banner = "prmd init [options] <resource name>"
|
|
@@ -24,11 +27,11 @@ commands = {
|
|
|
24
27
|
end
|
|
25
28
|
end,
|
|
26
29
|
render: OptionParser.new do |opts|
|
|
27
|
-
opts.banner = "prmd
|
|
30
|
+
opts.banner = "prmd render [options] <combined schema>"
|
|
28
31
|
opts.on("-p", "--prepend header,overview", Array, "Prepend files to output") do |p|
|
|
29
32
|
options[:prepend] = p
|
|
30
33
|
end
|
|
31
|
-
opts.on("-t", "--template
|
|
34
|
+
opts.on("-t", "--template templates", String, "Use alternate template") do |t|
|
|
32
35
|
options[:template] = t
|
|
33
36
|
end
|
|
34
37
|
end,
|
|
@@ -82,7 +85,7 @@ case command
|
|
|
82
85
|
end
|
|
83
86
|
schema = Prmd::Schema.new(JSON.parse(data))
|
|
84
87
|
|
|
85
|
-
options[:template] = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'prmd', 'templates'
|
|
88
|
+
options[:template] = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'prmd', 'templates'))
|
|
86
89
|
|
|
87
90
|
puts Prmd.render(schema, options)
|
|
88
91
|
when :init
|
data/docs/schemata.md
CHANGED
|
@@ -81,7 +81,8 @@ The links array MUST include an object defining each action available. Each acti
|
|
|
81
81
|
Links that expect a json-encoded body as input MUST also include the following attributes:
|
|
82
82
|
* `schema` - an object with a `properties` object that MUST include JSON pointers to the definitions for each associated attribute
|
|
83
83
|
|
|
84
|
-
|
|
84
|
+
The `schema` object MAY also include a `required` array to define all attributes for this link, which can not be omitted.
|
|
85
|
+
If this field is not present, all attributes in this link are considered as optional.
|
|
85
86
|
|
|
86
87
|
```javascript
|
|
87
88
|
{
|
|
@@ -95,7 +96,8 @@ Schema properties MAY also include a `required` boolean to indicate if an attrib
|
|
|
95
96
|
"properties": {
|
|
96
97
|
"owner": { "$ref": "/schema/user#/definitions/identity" },
|
|
97
98
|
"url": { "$ref": "/schema/resource/definitions/url" }
|
|
98
|
-
}
|
|
99
|
+
},
|
|
100
|
+
"required": [ "owner", "url" ]
|
|
99
101
|
},
|
|
100
102
|
"title": "Create"
|
|
101
103
|
},
|
|
@@ -8,7 +8,17 @@ module Prmd
|
|
|
8
8
|
[path]
|
|
9
9
|
end
|
|
10
10
|
# sort for stable loading on any platform
|
|
11
|
-
schemata =
|
|
11
|
+
schemata = []
|
|
12
|
+
files.sort.each do |file|
|
|
13
|
+
begin
|
|
14
|
+
schemata << [file, YAML.load(File.read(file))]
|
|
15
|
+
rescue
|
|
16
|
+
$stderr.puts "unable to parse #{file}"
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
unless schemata.length == files.length
|
|
20
|
+
exit(1) # one or more files failed to parse
|
|
21
|
+
end
|
|
12
22
|
|
|
13
23
|
data = {
|
|
14
24
|
'$schema' => 'http://json-schema.org/draft-04/hyper-schema',
|
|
@@ -25,34 +35,37 @@ module Prmd
|
|
|
25
35
|
end
|
|
26
36
|
|
|
27
37
|
schemata.each do |schema_file, schema_data|
|
|
28
|
-
|
|
38
|
+
id = schema_data['id'].split('/').last
|
|
29
39
|
|
|
30
40
|
if file = schemata_map[schema_data['id']]
|
|
31
41
|
$stderr.puts "`#{schema_data['id']}` (from #{schema_file}) was already defined in `#{file}` and will overwrite the first definition"
|
|
32
42
|
end
|
|
33
43
|
schemata_map[schema_data['id']] = schema_file
|
|
34
44
|
|
|
35
|
-
|
|
36
|
-
|
|
45
|
+
# schemas are now in a single scope by combine
|
|
46
|
+
schema_data.delete('id')
|
|
47
|
+
|
|
48
|
+
data['definitions']
|
|
49
|
+
data['definitions'][id] = schema_data
|
|
37
50
|
reference_localizer = lambda do |datum|
|
|
38
51
|
case datum
|
|
39
52
|
when Array
|
|
40
53
|
datum.map {|element| reference_localizer.call(element)}
|
|
41
54
|
when Hash
|
|
42
55
|
if datum.has_key?('$ref')
|
|
43
|
-
datum['$ref'] = '#/definitions' + datum['$ref'].gsub('#', '')
|
|
56
|
+
datum['$ref'] = '#/definitions' + datum['$ref'].gsub('#', '').gsub('/schemata', '')
|
|
44
57
|
end
|
|
45
|
-
if datum.has_key?('href')
|
|
46
|
-
datum['href'] = datum['href'].gsub('%23', '').gsub(%r{
|
|
58
|
+
if datum.has_key?('href') && datum['href'].is_a?(String)
|
|
59
|
+
datum['href'] = datum['href'].gsub('%23', '').gsub(%r{%2Fschemata(%2F[^%]*%2F)}, '%23%2Fdefinitions\1')
|
|
47
60
|
end
|
|
48
61
|
datum.each { |k,v| datum[k] = reference_localizer.call(v) }
|
|
49
62
|
else
|
|
50
63
|
datum
|
|
51
64
|
end
|
|
52
65
|
end
|
|
53
|
-
reference_localizer.call(data['definitions'][
|
|
66
|
+
reference_localizer.call(data['definitions'][id])
|
|
54
67
|
|
|
55
|
-
data['properties'][id] = { '$ref' => "#/definitions/#{
|
|
68
|
+
data['properties'][id] = { '$ref' => "#/definitions/#{id}" }
|
|
56
69
|
end
|
|
57
70
|
|
|
58
71
|
Prmd::Schema.new(data)
|
data/lib/prmd/commands/render.rb
CHANGED
|
@@ -2,11 +2,19 @@ module Prmd
|
|
|
2
2
|
def self.render(schema, options={})
|
|
3
3
|
doc = ''
|
|
4
4
|
|
|
5
|
+
options[:content_type] ||= 'application/json'
|
|
6
|
+
|
|
5
7
|
if options[:prepend]
|
|
6
8
|
doc << options[:prepend].map {|path| File.read(path)}.join("\n") << "\n"
|
|
7
9
|
end
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
template_dir = File::expand_path(options[:template])
|
|
12
|
+
if not File.directory?(template_dir) # to keep backward compatibility
|
|
13
|
+
template_dir = File.dirname(options[:template])
|
|
14
|
+
end
|
|
15
|
+
options[:template] = template_dir
|
|
16
|
+
|
|
17
|
+
doc << Prmd::Template::render('schema.erb', template_dir, {
|
|
10
18
|
options: options,
|
|
11
19
|
schema: schema
|
|
12
20
|
})
|
data/lib/prmd/commands/verify.rb
CHANGED
|
@@ -1,88 +1,51 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "json_schema"
|
|
3
|
+
|
|
1
4
|
module Prmd
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
# These schemas are listed manually and in order because they reference each
|
|
6
|
+
# other.
|
|
7
|
+
SCHEMAS = [
|
|
8
|
+
"schema.json",
|
|
9
|
+
"hyper-schema.json",
|
|
10
|
+
"interagent-hyper-schema.json"
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
def self.verify(schema_data)
|
|
14
|
+
store = init_document_store
|
|
15
|
+
|
|
16
|
+
if !(schema_uri = schema_data["$schema"])
|
|
17
|
+
return ["Missing $schema."]
|
|
10
18
|
end
|
|
11
|
-
errors.flatten!
|
|
12
|
-
end
|
|
13
19
|
|
|
14
|
-
|
|
15
|
-
|
|
20
|
+
# for good measure, make sure that the schema parses and that its
|
|
21
|
+
# references can be expanded
|
|
22
|
+
schema, errors = JsonSchema.parse!(schema_data)
|
|
23
|
+
return JsonSchema::SchemaError.aggregate(errors) if !schema
|
|
16
24
|
|
|
17
|
-
|
|
25
|
+
valid, errors = schema.expand_references(store: store)
|
|
26
|
+
return JsonSchema::SchemaError.aggregate(errors) if !valid
|
|
18
27
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
unless schema.has_key?(requirement)
|
|
22
|
-
missing_requirements << requirement
|
|
23
|
-
end
|
|
28
|
+
if !(meta_schema = store.lookup_schema(schema_uri))
|
|
29
|
+
return ["Unknown $schema: #{schema_uri}."]
|
|
24
30
|
end
|
|
25
|
-
missing_requirements.each do |missing_requirement|
|
|
26
|
-
errors << "Missing `#{id}#/#{missing_requirement}`"
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
errors
|
|
30
|
-
end
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
errors
|
|
32
|
+
valid, errors = meta_schema.validate(schema_data)
|
|
33
|
+
return JsonSchema::SchemaError.aggregate(errors) if !valid
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
[]
|
|
36
|
+
end
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
unless schema['definitions'].has_key?('identity')
|
|
39
|
-
errors << "Missing `#{id}#/definitions/identity`"
|
|
40
|
-
end
|
|
41
|
-
schema['definitions'].each do |key, value|
|
|
42
|
-
missing_requirements = []
|
|
43
|
-
unless key == 'identity'
|
|
44
|
-
%w{description type}.each do |requirement|
|
|
45
|
-
unless schema['definitions'][key].has_key?(requirement)
|
|
46
|
-
missing_requirements << requirement
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
# check for example, unless they are nested in array/object
|
|
51
|
-
type = schema['definitions'][key]['type']
|
|
52
|
-
unless type.nil? || type.include?('array') || type.include?('object')
|
|
53
|
-
unless schema['definitions'][key].has_key?('example')
|
|
54
|
-
missing_requirements << 'example'
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
missing_requirements.each do |missing_requirement|
|
|
58
|
-
errors << "Missing `#{id}#/definitions/#{key}/#{missing_requirement}`"
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
end
|
|
38
|
+
private
|
|
62
39
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if link.has_key?('schema')
|
|
72
|
-
%w{properties type}.each do |requirement|
|
|
73
|
-
unless link['schema'].has_key?(requirement)
|
|
74
|
-
missing_requirements << "schema/#{requirement}"
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
missing_requirements.each do |missing_requirement|
|
|
79
|
-
errors << "Missing #{missing_requirement} in `#{link}` link for `#{id}`"
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
else
|
|
83
|
-
errors << "Missing `#{id}/links`"
|
|
40
|
+
def self.init_document_store
|
|
41
|
+
store = JsonSchema::DocumentStore.new
|
|
42
|
+
SCHEMAS.each do |file|
|
|
43
|
+
file = File.expand_path("../../../../schemas/#{file}", __FILE__)
|
|
44
|
+
data = JSON.parse(File.read(file))
|
|
45
|
+
schema = JsonSchema::Parser.new.parse!(data)
|
|
46
|
+
schema.expand_references!(store: store)
|
|
47
|
+
store.add_schema(schema)
|
|
84
48
|
end
|
|
85
|
-
|
|
86
|
-
errors
|
|
49
|
+
store
|
|
87
50
|
end
|
|
88
51
|
end
|
data/lib/prmd/schema.rb
CHANGED
|
@@ -56,26 +56,36 @@ module Prmd
|
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
58
|
|
|
59
|
+
def schema_value_example(value)
|
|
60
|
+
if value.has_key?('properties') # nested properties
|
|
61
|
+
return schema_example(value)
|
|
62
|
+
elsif value.has_key?('items') # array of objects
|
|
63
|
+
_, items = dereference(value['items'])
|
|
64
|
+
if value['items'].has_key?('example')
|
|
65
|
+
return [items['example']]
|
|
66
|
+
else
|
|
67
|
+
return [schema_example(items)]
|
|
68
|
+
end
|
|
69
|
+
else
|
|
70
|
+
return value['example']
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
59
74
|
def schema_example(schema)
|
|
60
|
-
|
|
75
|
+
_, _schema = dereference(schema)
|
|
61
76
|
|
|
62
|
-
if
|
|
63
|
-
|
|
64
|
-
elsif
|
|
65
|
-
|
|
77
|
+
if _schema.has_key?('example')
|
|
78
|
+
_schema['example']
|
|
79
|
+
elsif _schema.has_key?('properties')
|
|
80
|
+
example = {}
|
|
81
|
+
_schema['properties'].each do |key, value|
|
|
66
82
|
_, value = dereference(value)
|
|
67
|
-
|
|
68
|
-
example[key] = {}
|
|
69
|
-
value['properties'].each do |k,v|
|
|
70
|
-
example[key][k] = dereference(v).last['example']
|
|
71
|
-
end
|
|
72
|
-
else
|
|
73
|
-
example[key] = value['example']
|
|
74
|
-
end
|
|
83
|
+
example[key] = schema_value_example(value)
|
|
75
84
|
end
|
|
85
|
+
example
|
|
86
|
+
elsif _schema.has_key?('items')
|
|
87
|
+
schema_value_example(_schema)
|
|
76
88
|
end
|
|
77
|
-
|
|
78
|
-
example
|
|
79
89
|
end
|
|
80
90
|
|
|
81
91
|
def schemata_example(schemata_id)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module Prmd
|
|
2
|
+
class Template
|
|
3
|
+
def self.load(path, base)
|
|
4
|
+
fallback = File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
|
5
|
+
|
|
6
|
+
resolved = File.join(base, path)
|
|
7
|
+
if not File.exist?(resolved)
|
|
8
|
+
resolved = File.join(fallback, path)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
return File.read(resolved)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.render(path, base, *args)
|
|
15
|
+
template = self.load(path, base)
|
|
16
|
+
Erubis::Eruby.new(template).result(*args)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
<%=
|
|
2
|
-
schemata_template =
|
|
2
|
+
schemata_template = Prmd::Template::load('schemata.erb', options[:template])
|
|
3
3
|
|
|
4
|
-
schema['properties'].map do |
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
schema['properties'].map do |resource, property|
|
|
5
|
+
begin
|
|
6
|
+
_, schemata = schema.dereference(property)
|
|
7
|
+
Erubis::Eruby.new(schemata_template).result({
|
|
8
|
+
options: options,
|
|
9
|
+
resource: resource,
|
|
10
|
+
schema: schema,
|
|
11
|
+
schemata: schemata
|
|
12
|
+
})
|
|
13
|
+
rescue => e
|
|
14
|
+
$stdout.puts("Error in resource: #{resource}")
|
|
15
|
+
raise e
|
|
16
|
+
end
|
|
11
17
|
end.join("\n") << "\n"
|
|
12
18
|
%>
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
<%-
|
|
2
|
+
def extract_attributes(schema, properties)
|
|
3
|
+
attributes = []
|
|
4
|
+
properties = properties.sort_by {|k,v| k} # ensure consistent ordering
|
|
5
|
+
|
|
6
|
+
properties.each do |key, value|
|
|
7
|
+
# found a reference to another element:
|
|
8
|
+
_, value = schema.dereference(value)
|
|
9
|
+
if value.has_key?('anyOf')
|
|
10
|
+
descriptions = []
|
|
11
|
+
examples = []
|
|
12
|
+
|
|
13
|
+
# sort anyOf! always show unique identifier first
|
|
14
|
+
anyof = value['anyOf'].sort_by do |property|
|
|
15
|
+
property['$ref'].split('/').last.gsub('id', 'a')
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
anyof.each do |ref|
|
|
19
|
+
_, nested_field = schema.dereference(ref)
|
|
20
|
+
descriptions << nested_field['description']
|
|
21
|
+
examples << nested_field['example']
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# avoid repetition :}
|
|
25
|
+
description = if descriptions.size > 1
|
|
26
|
+
descriptions.first.gsub!(/ of (this )?.*/, "")
|
|
27
|
+
descriptions[1..-1].map { |d| d.gsub!(/unique /, "") }
|
|
28
|
+
[descriptions[0...-1].join(", "), descriptions.last].join(" or ")
|
|
29
|
+
else
|
|
30
|
+
description = descriptions.last
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
example = [*examples].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
|
|
34
|
+
attributes << [key, "string", description, example]
|
|
35
|
+
|
|
36
|
+
# found a nested object
|
|
37
|
+
elsif value['type'] == ['object'] && value['properties']
|
|
38
|
+
nested = extract_attributes(schema, value['properties'])
|
|
39
|
+
nested.each do |attribute|
|
|
40
|
+
attribute[0] = "#{key}:#{attribute[0]}"
|
|
41
|
+
end
|
|
42
|
+
attributes.concat(nested)
|
|
43
|
+
# just a regular attribute
|
|
44
|
+
else
|
|
45
|
+
description = value['description']
|
|
46
|
+
if value['enum']
|
|
47
|
+
description += '<br/><b>one of:</b>' + [*value['enum']].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
if value.has_key?('example')
|
|
51
|
+
example = [*value['example']].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
|
|
52
|
+
elsif value['type'] == ['array'] && value.has_key?('items')
|
|
53
|
+
example = "<code>#{schema.schema_value_example(value)}</code>"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
type = if value['type'].include?('null')
|
|
57
|
+
'nullable '
|
|
58
|
+
else
|
|
59
|
+
''
|
|
60
|
+
end
|
|
61
|
+
type += (value['format'] || (value['type'] - ['null']).first)
|
|
62
|
+
attributes << [key, type, description, example]
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
return attributes
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def build_link_path(schema, link)
|
|
69
|
+
link['href'].gsub(%r|(\{\([^\)]+\)\})|) do |ref|
|
|
70
|
+
ref = ref.gsub('%2F', '/').gsub('%23', '#').gsub(%r|[\{\(\)\}]|, '')
|
|
71
|
+
ref_resource = ref.split('#/definitions/').last.split('/').first.gsub('-','_')
|
|
72
|
+
identity_key, identity_value = schema.dereference(ref)
|
|
73
|
+
if identity_value.has_key?('anyOf')
|
|
74
|
+
'{' + ref_resource + '_' + identity_value['anyOf'].map {|r| r['$ref'].split('/').last}.join('_or_') + '}'
|
|
75
|
+
else
|
|
76
|
+
'{' + ref_resource + '_' + identity_key.split('/').last + '}'
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
%>
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
<%-
|
|
2
|
+
path = build_link_path(schema, link)
|
|
3
|
+
link_schema_properties_template = Prmd::Template::load('link_schema_properties.erb', options[:template])
|
|
4
|
+
-%>
|
|
5
|
+
### <%= title %> <%= link['title'] %>
|
|
6
|
+
<%= link['description'] %>
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
<%= link['method'] %> <%= path %>
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
<%- if link.has_key?('schema') && link['schema'].has_key?('properties') %>
|
|
13
|
+
<%-
|
|
14
|
+
required, optional = link['schema']['properties'].partition do |k, v|
|
|
15
|
+
(link['schema']['required'] || []).include?(k)
|
|
16
|
+
end.map { |partition| Hash[partition] }
|
|
17
|
+
%>
|
|
18
|
+
<%- unless required.empty? %>
|
|
19
|
+
#### Required Parameters
|
|
20
|
+
<%= Erubis::Eruby.new(link_schema_properties_template).result(params: required, schema: schema) %>
|
|
21
|
+
|
|
22
|
+
<%- end %>
|
|
23
|
+
<%- unless optional.empty? %>
|
|
24
|
+
#### Optional Parameters
|
|
25
|
+
<%= Erubis::Eruby.new(link_schema_properties_template).result(params: optional, schema: schema) %>
|
|
26
|
+
<%- end %>
|
|
27
|
+
<%- end %>
|
|
28
|
+
|
|
29
|
+
#### Curl Example
|
|
30
|
+
<%=
|
|
31
|
+
Prmd::Template::render(File.join('schemata', 'link_curl_example.erb'), File.dirname(options[:template]), {
|
|
32
|
+
options: options,
|
|
33
|
+
resource: resource,
|
|
34
|
+
schema: schema,
|
|
35
|
+
schemata: schemata,
|
|
36
|
+
link: link,
|
|
37
|
+
path: path
|
|
38
|
+
})
|
|
39
|
+
%>
|
|
40
|
+
|
|
41
|
+
#### Response Example
|
|
42
|
+
```
|
|
43
|
+
HTTP/1.1 <%= case link['rel']
|
|
44
|
+
when 'create'
|
|
45
|
+
'201 Created'
|
|
46
|
+
else
|
|
47
|
+
'200 OK'
|
|
48
|
+
end %>
|
|
49
|
+
```
|
|
50
|
+
```json
|
|
51
|
+
<%- if link.has_key?('targetSchema') %>
|
|
52
|
+
<%= JSON.pretty_generate(schema.schema_example(link['targetSchema'])) %>
|
|
53
|
+
<%- elsif link['rel'] == 'instances' %>
|
|
54
|
+
<%= JSON.pretty_generate([schema.schemata_example(resource)]) %>
|
|
55
|
+
<%- else %>
|
|
56
|
+
<%= JSON.pretty_generate(schema.schemata_example(resource)) %>
|
|
57
|
+
<%- end %>
|
|
58
|
+
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
```term
|
|
2
|
+
<%-
|
|
3
|
+
data = {}
|
|
4
|
+
path = path.gsub(/{([^}]*)}/) {|match| '$' + match.gsub(/[{}]/, '').upcase}
|
|
5
|
+
|
|
6
|
+
if link.has_key?('schema')
|
|
7
|
+
data.merge!(schema.schema_example(link['schema']))
|
|
8
|
+
|
|
9
|
+
if link['method'].upcase == 'GET' && !data.empty?
|
|
10
|
+
path << '?'
|
|
11
|
+
data.sort_by {|k,_| k.to_s }.each do |key, values|
|
|
12
|
+
[values].flatten.each do |value|
|
|
13
|
+
path << [key.to_s, CGI.escape(value.to_s)].join('=') << '&'
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
path.chop! # remove trailing '&'
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
%>
|
|
20
|
+
$ curl -n -X <%= link['method'] %> <%= schema.href %><%= path %>
|
|
21
|
+
<%- unless data.empty? || link['method'].upcase == 'GET' %>
|
|
22
|
+
-H "Content-Type: <%= options[:content_type] %>" \
|
|
23
|
+
-d '<%= data.to_json %>'
|
|
24
|
+
<%- end %>
|
|
25
|
+
```
|