prmd 0.4.0 → 0.5.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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YTBiMDJmODY4NDAwMTE1ZTMzZTJiY2I2MzA1M2VkMzM2OWZkYmI5NA==
4
+ YTg0MzdjNDU3M2MxOTk2ODE2MTYyMGU1YWM2OGNjMGQ5OWM5NWM4Mg==
5
5
  data.tar.gz: !binary |-
6
- MTc2MjY0MjgzMDEwYzhhYWVmYzZkY2VhNjI0YmM3ZmI4MThhYzFlZA==
6
+ ZDViYzZmYmIxODY3YzFhOTBjZWZhMzM2ODNhZGJkNjk4MjViNDRjMw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZDJkMzVkZTk4ZTI1YmU5M2Q2MGVmNzZhZjI0YzFmODYwNDE0YThlYjA1MTYw
10
- NDI0ZDNhYmFjOTczNTllOTBjZDgyNDYwZGQyMzZhYmVlMzZlMTFjZTIzYWVm
11
- MmNjOTA0MTIzMWRlNjNmMDU5YTQ3YjZkZjQyZGQ1MzY1YWNlNGI=
9
+ NDgwNWU1ODRmNzc1MjJlNzU3NmUxNzg4OTk2NGVlZmY3NDRkODg2ZTYyN2E3
10
+ NmU2ZjJkNjI0MWI1YmQ5MTFlYTZjZTdmNjJmZGE4MmExNzk5YjBlMDA5YjEy
11
+ ZDhhOGJjMzRjMjQ1MzY2ZWY4ZTYzOWExMTA5OTFiODNjZDBjZjA=
12
12
  data.tar.gz: !binary |-
13
- NmI3N2ViOGIyZDE1NTljODExNDhkY2E2MDlmZThhZTFiZWE2YmFhNmQyNTg0
14
- MTBjYzczMjg4NDI1OGVjODlkOTI5NzU5YjllYmMyZGJjYzQ2N2I1YWZlMGM2
15
- MzY3YzIwM2RjYTlkNzk5MzExNjU0NjAxZDUzYTIzODlkZDQ2YjY=
13
+ NWRmM2Y5NjQyYmUwYjVlNzI4ZDYxY2VjMmYwNjA4MTJiMTE4NGVhNTQ1MzZl
14
+ MzM1MjMwMjkzZGMwYWMzM2EwMDJiOTRjN2VlMjVhMzA5ZjczN2IzZjUyYmMx
15
+ MDNmYjc4YTgyOTE1ZWNiMWJhYzhjMGZiOWI2MjkwZTMyZGVkODY=
@@ -1,12 +1,19 @@
1
+ * Adel Qalieh <aqalieh95@gmail.com>
1
2
  * Brandur <brandur@heroku.com>
2
3
  * Brandur <brandur@mutelight.org>
3
4
  * Chris Continanza <christopher.continanza@gmail.com>
5
+ * Dan Peterson <dpiddy@gmail.com>
4
6
  * Dane Harrigan <dane.harrigan@gmail.com>
7
+ * Eric J. Holmes <eric@ejholmes.net>
5
8
  * Ernesto Jiménez <me@ernesto-jimenez.com>
9
+ * Justin Halsall <justin@juice10.com>
6
10
  * Kousuke Ebihara <Kosuke_Ebihara@voyagegroup.com>
7
11
  * Kousuke Ebihara <kousuke@co3k.org>
8
12
  * Mark McGranaghan <mmcgrana@gmail.com>
13
+ * Martin Seeler <developer@chasmo.de>
14
+ * Matthew Conway <himself@mattonrails.com>
9
15
  * Olivier Lance <olance@users.noreply.github.com>
16
+ * Scott Clasen <scott@heroku.com>
10
17
  * Timothée Peignier <timothee.peignier@tryphon.org>
11
18
  * Wesley Beary <geemus+github@gmail.com>
12
19
  * geemus <geemus@gmail.com>
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prmd (0.4.0)
4
+ prmd (0.5.0)
5
5
  erubis (~> 2.7)
6
6
  json_schema (~> 0.1)
7
7
 
data/README.md CHANGED
@@ -79,7 +79,10 @@ $ prmd doc schema.json > schema.md
79
79
  ```
80
80
 
81
81
  # Render from schema
82
+
83
+ ```console
82
84
  $ prmd render --template schemata.erb schema.json > schema.md
85
+ ```
83
86
 
84
87
  Typically you'll want to prepend header and overview information to
85
88
  your API documentation. You can do this with the `--prepend` flag:
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("-u", "--url-style json", String, "URL style to output (default/json)") do |s|
20
+ options[:style] = s
21
+ end
19
22
  opts.on("-c", "--content-type application/json", String, "Content-Type header") do |c|
20
23
  options[:content_type] = c
21
24
  end
@@ -12,6 +12,7 @@ require File.join(dir, 'prmd', 'commands', 'verify')
12
12
  require File.join(dir, 'prmd', 'schema')
13
13
  require File.join(dir, 'prmd', 'version')
14
14
  require File.join(dir, 'prmd', 'template')
15
+ require File.join(dir, 'prmd', 'url_generator')
15
16
 
16
17
  module Prmd
17
18
  end
@@ -3,6 +3,7 @@ module Prmd
3
3
  doc = ''
4
4
 
5
5
  options[:content_type] ||= 'application/json'
6
+ options[:style] ||= 'default'
6
7
 
7
8
  if options[:prepend]
8
9
  doc << options[:prepend].map {|path| File.read(path)}.join("\n") << "\n"
@@ -12,7 +13,6 @@ module Prmd
12
13
  if not File.directory?(template_dir) # to keep backward compatibility
13
14
  template_dir = File.dirname(options[:template])
14
15
  end
15
- options[:template] = template_dir
16
16
 
17
17
  doc << Prmd::Template::render('schema.erb', template_dir, {
18
18
  options: options,
@@ -57,7 +57,9 @@ module Prmd
57
57
  end
58
58
 
59
59
  def schema_value_example(value)
60
- if value.has_key?('properties') # nested properties
60
+ if value.has_key?('example')
61
+ return value['example']
62
+ elsif value.has_key?('properties') # nested properties
61
63
  return schema_example(value)
62
64
  elsif value.has_key?('items') # array of objects
63
65
  _, items = dereference(value['items'])
@@ -66,8 +68,6 @@ module Prmd
66
68
  else
67
69
  return [schema_example(items)]
68
70
  end
69
- else
70
- return value['example']
71
71
  end
72
72
  end
73
73
 
@@ -40,15 +40,30 @@
40
40
  attribute[0] = "#{key}:#{attribute[0]}"
41
41
  end
42
42
  attributes.concat(nested)
43
+ # found an array with nested objects
44
+ elsif value['type'] == ['array'] && value['items'] && value['items']['properties']
45
+ nested = extract_attributes(schema, value['items']['properties'])
46
+ nested.each do |attribute|
47
+ attribute[0] = "#{key}/#{attribute[0]}"
48
+ end
49
+ attributes.concat(nested)
43
50
  # just a regular attribute
44
51
  else
45
52
  description = value['description']
53
+ if value['default']
54
+ description += "<br/><b>default:</b> <code>#{value['default'].to_json}</code>"
55
+ end
56
+
46
57
  if value['enum']
47
58
  description += '<br/><b>one of:</b>' + [*value['enum']].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
48
59
  end
49
60
 
50
61
  if value.has_key?('example')
51
- example = [*value['example']].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
62
+ example = if value['example'].is_a?(Hash) && value['example'].has_key?('oneOf')
63
+ value['example']['oneOf'].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
64
+ else
65
+ "<code>#{value['example'].to_json}</code>"
66
+ end
52
67
  elsif value['type'] == ['array'] && value.has_key?('items')
53
68
  example = "<code>#{schema.schema_value_example(value)}</code>"
54
69
  end
@@ -48,7 +48,8 @@ HTTP/1.1 <%= case link['rel']
48
48
  end %>
49
49
  ```
50
50
  ```json
51
- <%- if link.has_key?('targetSchema') %>
51
+ <%- if link['rel'] == 'empty' %>
52
+ <%- elsif link.has_key?('targetSchema') && link['targetSchema'].has_key?('properties') %>
52
53
  <%= JSON.pretty_generate(schema.schema_example(link['targetSchema'])) %>
53
54
  <%- elsif link['rel'] == 'instances' %>
54
55
  <%= JSON.pretty_generate([schema.schemata_example(resource)]) %>
@@ -1,25 +1,23 @@
1
- ```term
1
+ ```bash
2
2
  <%-
3
3
  data = {}
4
4
  path = path.gsub(/{([^}]*)}/) {|match| '$' + match.gsub(/[{}]/, '').upcase}
5
+ get_params = []
5
6
 
6
7
  if link.has_key?('schema')
7
8
  data.merge!(schema.schema_example(link['schema']))
8
9
 
9
10
  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 '&'
11
+ get_params << Prmd::UrlGenerator.new({schema: schema, link: link, options: options}).url_params
17
12
  end
18
13
  end
19
14
  %>
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 %>
15
+ $ curl -n -X <%= link['method'] %> <%= schema.href %><%= path -%>
16
+ <%- unless data.empty? || link['method'].upcase == 'GET' %> \
17
+ -H "Content-Type: <%= options[:content_type] %>" \
18
+ -d '<%= data.to_json %>'
19
+ <%- end %>
20
+ <%- unless get_params.empty? || link['method'].upcase != 'GET' %> -G \
21
+ -d <%= get_params.join(" \\\n -d ") %>
22
+ <%- end %>
25
23
  ```
@@ -0,0 +1,85 @@
1
+ module Prmd
2
+ class UrlGenerator
3
+
4
+ def initialize(params)
5
+ @schema = params[:schema]
6
+ @link = params[:link]
7
+ @options = params[:options]
8
+ end
9
+
10
+ def url_params
11
+ if @options[:style].downcase == 'json'
12
+ klass = Generators::JSON
13
+ else
14
+ klass = Generators::Default
15
+ end
16
+
17
+ klass.generate({schema: @schema, link: @link})
18
+ end
19
+
20
+ private
21
+
22
+ module Generators
23
+ class Default
24
+ class << self
25
+ def generate(params)
26
+ data = {}
27
+ data.merge!(params[:schema].schema_example(params[:link]['schema']))
28
+ generate_params(data)
29
+ end
30
+
31
+ private
32
+
33
+ def param_name(key, prefix, array = false)
34
+ result = if prefix
35
+ "#{prefix}[#{key}]"
36
+ else
37
+ key
38
+ end
39
+
40
+ result += "[]" if array
41
+ result
42
+ end
43
+
44
+ def generate_params(obj, prefix = nil)
45
+ result = []
46
+ obj.each do |key,value|
47
+ if value.is_a?(Hash)
48
+ newprefix = if prefix
49
+ "#{prefix}[#{key}]"
50
+ else
51
+ key
52
+ end
53
+ result << generate_params(value, newprefix)
54
+ elsif value.is_a?(Array)
55
+ value.each do |val|
56
+ result << [param_name(key, prefix, true), CGI.escape(val.to_s)].join('=')
57
+ end
58
+ else
59
+ next unless value # ignores parameters with empty examples
60
+ result << [param_name(key, prefix), CGI.escape(value.to_s)].join('=')
61
+ end
62
+ end
63
+ result.flatten
64
+ end
65
+ end
66
+ end
67
+
68
+ class JSON
69
+ def self.generate(params)
70
+ data = {}
71
+ data.merge!(params[:schema].schema_example(params[:link]['schema']))
72
+
73
+ result = []
74
+ data.sort_by {|k,_| k.to_s }.each do |key, values|
75
+ [values].flatten.each do |value|
76
+ result << [key.to_s, CGI.escape(value.to_s)].join('=')
77
+ end
78
+ end
79
+
80
+ result
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -1,3 +1,3 @@
1
1
  module Prmd
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prmd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - geemus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-03 00:00:00.000000000 Z
11
+ date: 2014-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubis
@@ -113,6 +113,7 @@ files:
113
113
  - lib/prmd/templates/schemata/helper.erb
114
114
  - lib/prmd/templates/schemata/link.erb
115
115
  - lib/prmd/templates/schemata/link_curl_example.erb
116
+ - lib/prmd/url_generator.rb
116
117
  - lib/prmd/version.rb
117
118
  - prmd.gemspec
118
119
  - schemas/hyper-schema.json
@@ -143,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
144
  version: '0'
144
145
  requirements: []
145
146
  rubyforge_project:
146
- rubygems_version: 2.2.2
147
+ rubygems_version: 2.3.0
147
148
  signing_key:
148
149
  specification_version: 4
149
150
  summary: JSON Schema tooling