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.
@@ -1,69 +1,14 @@
1
1
  <%-
2
2
  return unless schemata.has_key?('links') && !schemata['links'].empty?
3
3
 
4
- link_schema_properties_template = File.read(File.join(File.dirname(options[:template]), 'link_schema_properties.erb'))
5
- resource = schemata['id'].split('/').last
6
- title = schemata['title'].split(' - ', 2).last
7
-
8
- def extract_attributes(schema, properties)
9
- attributes = []
10
- properties = properties.sort_by {|k,v| k} # ensure consistent ordering
11
-
12
- properties.each do |key, value|
13
- # found a reference to another element:
14
- _, value = schema.dereference(value)
15
- if value.has_key?('anyOf')
16
- descriptions = []
17
- examples = []
18
-
19
- # sort anyOf! always show unique identifier first
20
- anyof = value['anyOf'].sort_by do |property|
21
- property['$ref'].split('/').last.gsub('id', 'a')
22
- end
23
-
24
- anyof.each do |ref|
25
- _, nested_field = schema.dereference(ref)
26
- descriptions << nested_field['description']
27
- examples << nested_field['example']
28
- end
4
+ Prmd::Template::render(File.join('schemata', 'helper.erb'), options[:template], {
5
+ options: options,
6
+ resource: resource,
7
+ schema: schema,
8
+ schemata: schemata
9
+ })
29
10
 
30
- # avoid repetition :}
31
- if descriptions.size > 1
32
- descriptions.first.gsub!(/ of (this )?.*/, "")
33
- descriptions[1..-1].map { |d| d.gsub!(/unique /, "") }
34
- end
35
-
36
- last = descriptions.pop
37
- description = [descriptions.join(", "), last].join(" or ")
38
-
39
- example = [*examples].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
40
- attributes << [key, "string", description, example]
41
-
42
- # found a nested object
43
- elsif value['type'] == ['object'] && value['properties']
44
- nested = extract_attributes(schema, value['properties'])
45
- nested.each do |attribute|
46
- attribute[0] = "#{key}:#{attribute[0]}"
47
- end
48
- attributes.concat(nested)
49
- # just a regular attribute
50
- else
51
- description = value['description']
52
- if value['enum']
53
- description += '<br/><b>one of:</b>' + [*value['enum']].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
54
- end
55
- example = [*value['example']].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
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
11
+ title = schemata['title'].split(' - ', 2).last
67
12
  %>
68
13
  ## <%= title %>
69
14
  <%= schemata['description'] %>
@@ -89,83 +34,14 @@
89
34
 
90
35
  <%- end %>
91
36
  <%- schemata['links'].each do |link, datum| %>
92
- <%-
93
- path = link['href'].gsub(%r|(\{\([^\)]+\)\})|) do |ref|
94
- ref = ref.gsub('%2F', '/').gsub('%23', '#').gsub(%r|[\{\(\)\}]|, '')
95
- ref_resource = ref.split('#/definitions/schemata/').last.split('/').first.gsub('-','_')
96
- identity_key, identity_value = schema.dereference(ref)
97
- if identity_value.has_key?('anyOf')
98
- '{' + ref_resource + '_' + identity_value['anyOf'].map {|r| r['$ref'].split('/').last}.join('_or_') + '}'
99
- else
100
- '{' + ref_resource + '_' + identity_key.split('/').last + '}'
101
- end
102
- end
103
- -%>
104
- ### <%= title %> <%= link['title'] %>
105
- <%= link['description'] %>
106
-
107
- ```
108
- <%= link['method'] %> <%= path %>
109
- ```
110
-
111
- <%- if link.has_key?('schema') && link['schema'].has_key?('properties') %>
112
- <%-
113
- required, optional = link['schema']['properties'].partition do |k, v|
114
- (link['schema']['required'] || []).include?(k)
115
- end.map { |partition| Hash[partition] }
116
- %>
117
- <%- unless required.empty? %>
118
- #### Required Parameters
119
- <%= Erubis::Eruby.new(link_schema_properties_template).result(params: required, schema: schema) %>
120
-
121
- <%- end %>
122
- <%- unless optional.empty? %>
123
- #### Optional Parameters
124
- <%= Erubis::Eruby.new(link_schema_properties_template).result(params: optional, schema: schema) %>
125
- <%- end %>
126
- <%- end %>
127
-
128
- #### Curl Example
129
- ```term
130
- <%-
131
- data = {}
132
- path = path.gsub(/{([^}]*)}/) {|match| '$' + match.gsub(/[{}]/, '').upcase}
133
-
134
- if link.has_key?('schema')
135
- data.merge!(schema.schema_example(link['schema']))
136
-
137
- if link['method'].upcase == 'GET' && !data.empty?
138
- path << '?'
139
- data.sort_by {|k,_| k.to_s }.each do |key, values|
140
- [values].flatten.each do |value|
141
- path << [key.to_s, CGI.escape(value.to_s)].join('=') << '&'
142
- end
143
- end
144
- path.chop! # remove trailing '&'
145
- end
146
- end
147
- %>
148
- $ curl -n -X <%= link['method'] %> <%= schema.href %><%= path %>
149
- <%- unless data.empty? || link['method'].upcase == 'GET' %>
150
- -H "Content-Type: application/json" \
151
- -d '<%= data.to_json %>'
152
- <%- end %>
153
- ```
154
-
155
- #### Response Example
156
- ```
157
- HTTP/1.1 <%= case link['rel']
158
- when 'create'
159
- '201 Created'
160
- else
161
- '200 OK'
162
- end %>
163
- ```
164
- ```javascript```
165
- <%- if link['rel'] == 'instances' %>
166
- <%= JSON.pretty_generate([schema.schemata_example(schemata['id'])]) %>
167
- <%- else %>
168
- <%= JSON.pretty_generate(schema.schemata_example(schemata['id'])) %>
169
- <%- end %>
170
- ```
37
+ <%=
38
+ Prmd::Template::render(File.join('schemata', 'link.erb'), options[:template], {
39
+ options: options,
40
+ resource: resource,
41
+ schema: schema,
42
+ schemata: schemata,
43
+ link: link,
44
+ title: title
45
+ })
46
+ %>
171
47
  <%- end -%>
data/lib/prmd/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Prmd
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/prmd.rb CHANGED
@@ -11,6 +11,7 @@ require File.join(dir, 'prmd', 'commands', 'render')
11
11
  require File.join(dir, 'prmd', 'commands', 'verify')
12
12
  require File.join(dir, 'prmd', 'schema')
13
13
  require File.join(dir, 'prmd', 'version')
14
+ require File.join(dir, 'prmd', 'template')
14
15
 
15
16
  module Prmd
16
17
  end
data/prmd.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "erubis", "~> 2.7"
22
+ spec.add_dependency "json_schema", "~> 0.1"
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.3"
24
25
  spec.add_development_dependency "rake", "~> 10.2"
@@ -0,0 +1,168 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/hyper-schema#",
3
+ "id": "http://json-schema.org/draft-04/hyper-schema#",
4
+ "title": "JSON Hyper-Schema",
5
+ "allOf": [
6
+ {
7
+ "$ref": "http://json-schema.org/draft-04/schema#"
8
+ }
9
+ ],
10
+ "properties": {
11
+ "additionalItems": {
12
+ "anyOf": [
13
+ {
14
+ "type": "boolean"
15
+ },
16
+ {
17
+ "$ref": "#"
18
+ }
19
+ ]
20
+ },
21
+ "additionalProperties": {
22
+ "anyOf": [
23
+ {
24
+ "type": "boolean"
25
+ },
26
+ {
27
+ "$ref": "#"
28
+ }
29
+ ]
30
+ },
31
+ "dependencies": {
32
+ "additionalProperties": {
33
+ "anyOf": [
34
+ {
35
+ "$ref": "#"
36
+ },
37
+ {
38
+ "type": "array"
39
+ }
40
+ ]
41
+ }
42
+ },
43
+ "items": {
44
+ "anyOf": [
45
+ {
46
+ "$ref": "#"
47
+ },
48
+ {
49
+ "$ref": "#/definitions/schemaArray"
50
+ }
51
+ ]
52
+ },
53
+ "definitions": {
54
+ "additionalProperties": {
55
+ "$ref": "#"
56
+ }
57
+ },
58
+ "patternProperties": {
59
+ "additionalProperties": {
60
+ "$ref": "#"
61
+ }
62
+ },
63
+ "properties": {
64
+ "additionalProperties": {
65
+ "$ref": "#"
66
+ }
67
+ },
68
+ "allOf": {
69
+ "$ref": "#/definitions/schemaArray"
70
+ },
71
+ "anyOf": {
72
+ "$ref": "#/definitions/schemaArray"
73
+ },
74
+ "oneOf": {
75
+ "$ref": "#/definitions/schemaArray"
76
+ },
77
+ "not": {
78
+ "$ref": "#"
79
+ },
80
+
81
+ "links": {
82
+ "type": "array",
83
+ "items": {
84
+ "$ref": "#/definitions/linkDescription"
85
+ }
86
+ },
87
+ "fragmentResolution": {
88
+ "type": "string"
89
+ },
90
+ "media": {
91
+ "type": "object",
92
+ "properties": {
93
+ "type": {
94
+ "description": "A media type, as described in RFC 2046",
95
+ "type": "string"
96
+ },
97
+ "binaryEncoding": {
98
+ "description": "A content encoding scheme, as described in RFC 2045",
99
+ "type": "string"
100
+ }
101
+ }
102
+ },
103
+ "pathStart": {
104
+ "description": "Instances' URIs must start with this value for this schema to apply to them",
105
+ "type": "string",
106
+ "format": "uri"
107
+ }
108
+ },
109
+ "definitions": {
110
+ "schemaArray": {
111
+ "type": "array",
112
+ "items": {
113
+ "$ref": "#"
114
+ }
115
+ },
116
+ "linkDescription": {
117
+ "title": "Link Description Object",
118
+ "type": "object",
119
+ "required": [ "href", "rel" ],
120
+ "properties": {
121
+ "href": {
122
+ "description": "a URI template, as defined by RFC 6570, with the addition of the $, ( and ) characters for pre-processing",
123
+ "type": "string"
124
+ },
125
+ "rel": {
126
+ "description": "relation to the target resource of the link",
127
+ "type": "string"
128
+ },
129
+ "title": {
130
+ "description": "a title for the link",
131
+ "type": "string"
132
+ },
133
+ "targetSchema": {
134
+ "description": "JSON Schema describing the link target",
135
+ "$ref": "#"
136
+ },
137
+ "mediaType": {
138
+ "description": "media type (as defined by RFC 2046) describing the link target",
139
+ "type": "string"
140
+ },
141
+ "method": {
142
+ "description": "method for requesting the target of the link (e.g. for HTTP this might be \"GET\" or \"DELETE\")",
143
+ "type": "string"
144
+ },
145
+ "encType": {
146
+ "description": "The media type in which to submit data along with the request",
147
+ "type": "string",
148
+ "default": "application/json"
149
+ },
150
+ "schema": {
151
+ "description": "Schema describing the data to submit along with the request",
152
+ "$ref": "#"
153
+ }
154
+ }
155
+ }
156
+ },
157
+ "links": [
158
+ {
159
+ "rel": "self",
160
+ "href": "{+id}"
161
+ },
162
+ {
163
+ "rel": "full",
164
+ "href": "{+($ref)}"
165
+ }
166
+ ]
167
+ }
168
+
@@ -0,0 +1,175 @@
1
+ {
2
+ "$schema": "http://interagent.github.io/interagent-hyper-schema#",
3
+ "id": "http://interagent.github.io/interagent-hyper-schema#",
4
+ "title": "Heroku JSON Hyper-Schema",
5
+ "allOf": [
6
+ {
7
+ "$ref": "http://json-schema.org/draft-04/hyper-schema#"
8
+ }
9
+ ],
10
+ "definitions": {
11
+ "identity": {
12
+ "anyOf": [
13
+ {
14
+ "additionalProperties": false,
15
+ "properties": {
16
+ "anyOf": {
17
+ "additionalProperties": {
18
+ "$ref": "#/definitions/ref"
19
+ },
20
+ "minProperties": 1
21
+ }
22
+ },
23
+ "required": ["anyOf"]
24
+ },
25
+ {
26
+ "properties": {},
27
+ "strictProperties": true
28
+ }
29
+ ]
30
+ },
31
+ "ref": {
32
+ "additionalProperties": false,
33
+ "properties": {
34
+ "$ref": {
35
+ "type": "string"
36
+ }
37
+ },
38
+ "required": ["$ref"]
39
+ },
40
+ "resource": {
41
+ "dependencies": {
42
+ "properties": {
43
+ "properties": {
44
+ "definitions": {
45
+ "additionalProperties": {
46
+ "$ref": "#/definitions/resourceDefinition"
47
+ },
48
+ "properties": {
49
+ "identity": {
50
+ "$ref": "#/definitions/identity"
51
+ }
52
+ },
53
+ "required": ["identity"]
54
+ }
55
+ }
56
+ }
57
+ },
58
+ "properties": {
59
+ "links": {
60
+ "items": {
61
+ "$ref": "#/definitions/resourceLink"
62
+ }
63
+ },
64
+ "properties": {
65
+ "patternProperties": {
66
+ "^[a-z][a-z_]*[a-z]$": {
67
+ "$ref": "#/definitions/resourceProperty"
68
+ }
69
+ },
70
+ "strictProperties": true
71
+ },
72
+ "strictProperties": {
73
+ "enum": [true],
74
+ "type": "boolean"
75
+ }
76
+ },
77
+ "required": [
78
+ "definitions",
79
+ "description",
80
+ "links",
81
+ "title",
82
+ "type"
83
+ ]
84
+ },
85
+ "resourceDefinition": {
86
+ "anyOf": [
87
+ {
88
+ "required": ["example", "type"]
89
+ },
90
+ {
91
+ "required": ["type"],
92
+ "type": ["array"]
93
+ },
94
+ {
95
+ "required": ["type"],
96
+ "type": ["object"]
97
+ }
98
+ ],
99
+ "not": {
100
+ "required": ["links"]
101
+ },
102
+ "required": ["description"]
103
+ },
104
+ "resourceLink": {
105
+ "properties": {
106
+ "href": {
107
+ "pattern": "^(/([a-z][a-z\\-]*[a-z]|\\{\\(.*\\)\\}))+$"
108
+ },
109
+ "schema": {
110
+ "anyOf": [
111
+ {
112
+ "required": ["properties"]
113
+ },
114
+ {
115
+ "required": ["patternProperties"]
116
+ }
117
+ ],
118
+ "required": ["type"]
119
+ }
120
+ },
121
+ "required": ["description", "href", "method", "rel", "title"]
122
+ },
123
+ "resourceProperty": {
124
+ "anyOf": [
125
+ {
126
+ "$ref": "#/definitions/ref"
127
+ },
128
+ {
129
+ "properties": {
130
+ "properties": {
131
+ "additionalProperties": {
132
+ "$ref": "#/definitions/resourceProperty"
133
+ }
134
+ },
135
+ "strictProperties": {
136
+ "enum": [true],
137
+ "type": "boolean"
138
+ }
139
+ },
140
+ "required": ["description", "properties", "type"]
141
+ },
142
+ {
143
+ "properties": {
144
+ "items": {
145
+ "$ref": "#/definitions/ref"
146
+ }
147
+ },
148
+ "required": ["description", "items", "type"]
149
+ }
150
+ ]
151
+ }
152
+ },
153
+ "properties": {
154
+ "definitions": {
155
+ "additionalProperties": {
156
+ "$ref": "#/definitions/resource"
157
+ }
158
+ },
159
+ "properties": {
160
+ "additionalProperties": {
161
+ "$ref": "#/definitions/ref"
162
+ }
163
+ }
164
+ },
165
+ "required": [
166
+ "$schema",
167
+ "definitions",
168
+ "description",
169
+ "id",
170
+ "links",
171
+ "properties",
172
+ "title",
173
+ "type"
174
+ ]
175
+ }
@@ -0,0 +1,150 @@
1
+ {
2
+ "id": "http://json-schema.org/draft-04/schema#",
3
+ "$schema": "http://json-schema.org/draft-04/schema#",
4
+ "description": "Core schema meta-schema",
5
+ "definitions": {
6
+ "schemaArray": {
7
+ "type": "array",
8
+ "minItems": 1,
9
+ "items": { "$ref": "#" }
10
+ },
11
+ "positiveInteger": {
12
+ "type": "integer",
13
+ "minimum": 0
14
+ },
15
+ "positiveIntegerDefault0": {
16
+ "allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
17
+ },
18
+ "simpleTypes": {
19
+ "enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
20
+ },
21
+ "stringArray": {
22
+ "type": "array",
23
+ "items": { "type": "string" },
24
+ "minItems": 1,
25
+ "uniqueItems": true
26
+ }
27
+ },
28
+ "type": "object",
29
+ "properties": {
30
+ "id": {
31
+ "type": "string",
32
+ "format": "uri"
33
+ },
34
+ "$schema": {
35
+ "type": "string",
36
+ "format": "uri"
37
+ },
38
+ "title": {
39
+ "type": "string"
40
+ },
41
+ "description": {
42
+ "type": "string"
43
+ },
44
+ "default": {},
45
+ "multipleOf": {
46
+ "type": "number",
47
+ "minimum": 0,
48
+ "exclusiveMinimum": true
49
+ },
50
+ "maximum": {
51
+ "type": "number"
52
+ },
53
+ "exclusiveMaximum": {
54
+ "type": "boolean",
55
+ "default": false
56
+ },
57
+ "minimum": {
58
+ "type": "number"
59
+ },
60
+ "exclusiveMinimum": {
61
+ "type": "boolean",
62
+ "default": false
63
+ },
64
+ "maxLength": { "$ref": "#/definitions/positiveInteger" },
65
+ "minLength": { "$ref": "#/definitions/positiveIntegerDefault0" },
66
+ "pattern": {
67
+ "type": "string",
68
+ "format": "regex"
69
+ },
70
+ "additionalItems": {
71
+ "anyOf": [
72
+ { "type": "boolean" },
73
+ { "$ref": "#" }
74
+ ],
75
+ "default": {}
76
+ },
77
+ "items": {
78
+ "anyOf": [
79
+ { "$ref": "#" },
80
+ { "$ref": "#/definitions/schemaArray" }
81
+ ],
82
+ "default": {}
83
+ },
84
+ "maxItems": { "$ref": "#/definitions/positiveInteger" },
85
+ "minItems": { "$ref": "#/definitions/positiveIntegerDefault0" },
86
+ "uniqueItems": {
87
+ "type": "boolean",
88
+ "default": false
89
+ },
90
+ "maxProperties": { "$ref": "#/definitions/positiveInteger" },
91
+ "minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
92
+ "required": { "$ref": "#/definitions/stringArray" },
93
+ "additionalProperties": {
94
+ "anyOf": [
95
+ { "type": "boolean" },
96
+ { "$ref": "#" }
97
+ ],
98
+ "default": {}
99
+ },
100
+ "definitions": {
101
+ "type": "object",
102
+ "additionalProperties": { "$ref": "#" },
103
+ "default": {}
104
+ },
105
+ "properties": {
106
+ "type": "object",
107
+ "additionalProperties": { "$ref": "#" },
108
+ "default": {}
109
+ },
110
+ "patternProperties": {
111
+ "type": "object",
112
+ "additionalProperties": { "$ref": "#" },
113
+ "default": {}
114
+ },
115
+ "dependencies": {
116
+ "type": "object",
117
+ "additionalProperties": {
118
+ "anyOf": [
119
+ { "$ref": "#" },
120
+ { "$ref": "#/definitions/stringArray" }
121
+ ]
122
+ }
123
+ },
124
+ "enum": {
125
+ "type": "array",
126
+ "minItems": 1,
127
+ "uniqueItems": true
128
+ },
129
+ "type": {
130
+ "anyOf": [
131
+ { "$ref": "#/definitions/simpleTypes" },
132
+ {
133
+ "type": "array",
134
+ "items": { "$ref": "#/definitions/simpleTypes" },
135
+ "minItems": 1,
136
+ "uniqueItems": true
137
+ }
138
+ ]
139
+ },
140
+ "allOf": { "$ref": "#/definitions/schemaArray" },
141
+ "anyOf": { "$ref": "#/definitions/schemaArray" },
142
+ "oneOf": { "$ref": "#/definitions/schemaArray" },
143
+ "not": { "$ref": "#" }
144
+ },
145
+ "dependencies": {
146
+ "exclusiveMaximum": [ "maximum" ],
147
+ "exclusiveMinimum": [ "minimum" ]
148
+ },
149
+ "default": {}
150
+ }