prmd 0.6.1 → 0.7.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.
Files changed (63) hide show
  1. checksums.yaml +5 -13
  2. data/CONTRIBUTORS.md +8 -0
  3. data/Gemfile.lock +7 -7
  4. data/README.md +57 -8
  5. data/Rakefile +4 -4
  6. data/bin/prmd +3 -117
  7. data/docs/schemata.md +11 -11
  8. data/lib/prmd/cli/base.rb +151 -0
  9. data/lib/prmd/cli/combine.rb +42 -0
  10. data/lib/prmd/cli/doc.rb +69 -0
  11. data/lib/prmd/cli/generate.rb +44 -0
  12. data/lib/prmd/cli/render.rb +48 -0
  13. data/lib/prmd/cli/verify.rb +49 -0
  14. data/lib/prmd/cli.rb +108 -0
  15. data/lib/prmd/commands/combine.rb +85 -58
  16. data/lib/prmd/commands/init.rb +30 -98
  17. data/lib/prmd/commands/render.rb +30 -17
  18. data/lib/prmd/commands/verify.rb +78 -35
  19. data/lib/prmd/commands.rb +4 -0
  20. data/lib/prmd/core/combiner.rb +91 -0
  21. data/lib/prmd/core/generator.rb +27 -0
  22. data/lib/prmd/core/renderer.rb +56 -0
  23. data/lib/prmd/core/schema_hash.rb +47 -0
  24. data/lib/prmd/core_ext/optparse.rb +6 -0
  25. data/lib/prmd/hash_helpers.rb +38 -0
  26. data/lib/prmd/load_schema_file.rb +25 -0
  27. data/lib/prmd/rake_tasks/base.rb +33 -0
  28. data/lib/prmd/rake_tasks/combine.rb +50 -0
  29. data/lib/prmd/rake_tasks/doc.rb +73 -0
  30. data/lib/prmd/rake_tasks/verify.rb +60 -0
  31. data/lib/prmd/schema.rb +86 -34
  32. data/lib/prmd/template.rb +65 -8
  33. data/lib/prmd/templates/combine_head.json +6 -0
  34. data/lib/prmd/templates/init_default.json +9 -0
  35. data/lib/prmd/templates/init_resource.json.erb +90 -0
  36. data/lib/prmd/templates/link_schema_properties.md.erb +5 -0
  37. data/lib/prmd/templates/schema.erb +2 -2
  38. data/lib/prmd/templates/schemata/helper.erb +29 -15
  39. data/lib/prmd/templates/schemata/link.md.erb +74 -0
  40. data/lib/prmd/templates/schemata/{link_curl_example.erb → link_curl_example.md.erb} +9 -2
  41. data/lib/prmd/templates/schemata.md.erb +37 -0
  42. data/lib/prmd/url_generator.rb +11 -69
  43. data/lib/prmd/url_generators/generators/default.rb +66 -0
  44. data/lib/prmd/url_generators/generators/json.rb +30 -0
  45. data/lib/prmd/version.rb +10 -1
  46. data/lib/prmd.rb +7 -18
  47. data/prmd.gemspec +15 -15
  48. data/test/cli/combine_test.rb +23 -0
  49. data/test/cli/doc_test.rb +25 -0
  50. data/test/cli/generate_test.rb +23 -0
  51. data/test/cli/render_test.rb +25 -0
  52. data/test/cli/verify_test.rb +21 -0
  53. data/test/commands/init_test.rb +7 -0
  54. data/test/commands/render_test.rb +93 -0
  55. data/test/commands/verify_test.rb +60 -60
  56. data/test/helpers.rb +61 -6
  57. data/test/schema_test.rb +17 -11
  58. data/test/schemata/input/doc-settings.json +4 -0
  59. metadata +73 -28
  60. data/lib/prmd/commands/expand.rb +0 -108
  61. data/lib/prmd/templates/link_schema_properties.erb +0 -16
  62. data/lib/prmd/templates/schemata/link.erb +0 -61
  63. data/lib/prmd/templates/schemata.erb +0 -47
@@ -1,51 +1,94 @@
1
- require "json"
2
- require "json_schema"
1
+ require 'json'
2
+ require 'json_schema'
3
3
 
4
+ # :nodoc:
4
5
  module Prmd
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
- ]
6
+ # Schema Verification
7
+ module Verification
8
+ # These schemas are listed manually and in order because they reference each
9
+ # other.
10
+ SCHEMAS = [
11
+ 'schema.json',
12
+ 'hyper-schema.json',
13
+ 'interagent-hyper-schema.json'
14
+ ]
12
15
 
13
- def self.verify(schema_data)
14
- store = init_document_store
16
+ # @return [JsonSchema::DocumentStore]
17
+ def self.init_document_store
18
+ store = JsonSchema::DocumentStore.new
19
+ SCHEMAS.each do |file|
20
+ file = File.expand_path("../../../../schemas/#{file}", __FILE__)
21
+ data = JSON.parse(File.read(file))
22
+ schema = JsonSchema::Parser.new.parse!(data)
23
+ schema.expand_references!(store: store)
24
+ store.add_schema(schema)
25
+ end
26
+ store
27
+ end
15
28
 
16
- if !(schema_uri = schema_data["$schema"])
17
- return ["Missing $schema."]
29
+ # @return [JsonSchema::DocumentStore]
30
+ def self.document_store
31
+ @document_store ||= init_document_store
18
32
  end
19
33
 
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
34
+ # @param [Hash] schema_data
35
+ # @return [Array<String>] errors from failed verfication
36
+ def self.verify_parsable(schema_data)
37
+ # for good measure, make sure that the schema parses and that its
38
+ # references can be expanded
39
+ schema, errors = JsonSchema.parse!(schema_data)
40
+ return JsonSchema::SchemaError.aggregate(errors) unless schema
24
41
 
25
- valid, errors = schema.expand_references(store: store)
26
- return JsonSchema::SchemaError.aggregate(errors) if !valid
42
+ valid, errors = schema.expand_references(store: document_store)
43
+ return JsonSchema::SchemaError.aggregate(errors) unless valid
27
44
 
28
- if !(meta_schema = store.lookup_schema(schema_uri))
29
- return ["Unknown $schema: #{schema_uri}."]
45
+ []
30
46
  end
31
47
 
32
- valid, errors = meta_schema.validate(schema_data)
33
- return JsonSchema::SchemaError.aggregate(errors) if !valid
48
+ # @param [Hash] schema_data
49
+ # @return [Array<String>] errors from failed verfication
50
+ def self.verify_meta_schema(meta_schema, schema_data)
51
+ valid, errors = meta_schema.validate(schema_data)
52
+ return JsonSchema::SchemaError.aggregate(errors) unless valid
34
53
 
35
- []
36
- end
54
+ []
55
+ end
56
+
57
+ # @param [Hash] schema_data
58
+ # @return [Array<String>] errors from failed verfication
59
+ def self.verify_schema(schema_data)
60
+ schema_uri = schema_data['$schema']
61
+ return ['Missing $schema key.'] unless schema_uri
62
+
63
+ meta_schema = document_store.lookup_schema(schema_uri)
64
+ return ["Unknown $schema: #{schema_uri}."] unless meta_schema
37
65
 
38
- private
66
+ verify_meta_schema(meta_schema, schema_data)
67
+ end
68
+
69
+ # Verfies that a given schema is valid
70
+ #
71
+ # @param [Hash] schema_data
72
+ # @return [Array<String>] errors from failed verification
73
+ def self.verify(schema_data)
74
+ a = verify_schema(schema_data)
75
+ return a unless a.empty?
76
+ b = verify_parsable(schema_data)
77
+ return b unless b.empty?
78
+ []
79
+ end
39
80
 
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)
81
+ class << self
82
+ private :init_document_store
83
+ private :document_store
84
+ private :verify_parsable
85
+ private :verify_schema
86
+ private :verify_meta_schema
48
87
  end
49
- store
88
+ end
89
+
90
+ # (see Prmd::Verification.verify)
91
+ def self.verify(schema_data)
92
+ Verification.verify(schema_data)
50
93
  end
51
94
  end
@@ -0,0 +1,4 @@
1
+ require 'prmd/commands/combine'
2
+ require 'prmd/commands/init'
3
+ require 'prmd/commands/render'
4
+ require 'prmd/commands/verify'
@@ -0,0 +1,91 @@
1
+ require 'prmd/schema'
2
+ require 'prmd/core/schema_hash'
3
+
4
+ # :nodoc:
5
+ module Prmd
6
+ # Schema combiner
7
+ class Combiner
8
+ #
9
+ # @param [Hash<Symbol, Object>] properties
10
+ def initialize(properties = {})
11
+ @properties = properties
12
+ @schema = properties.fetch(:schema)
13
+ @base = properties.fetch(:base, {})
14
+ @meta = properties.fetch(:meta, {})
15
+ end
16
+
17
+ # @param [Array] array
18
+ # @return [Array]
19
+ def reference_localizer_array(array)
20
+ array.map { |element| reference_localizer(element) }
21
+ end
22
+
23
+ # @param [Hash] hash
24
+ # @return [Hash]
25
+ def reference_localizer_hash(hash)
26
+ if hash.key?('$ref')
27
+ hash['$ref'] = '#/definitions' + hash['$ref'].gsub('#', '')
28
+ .gsub('/schemata', '')
29
+ end
30
+ if hash.key?('href') && hash['href'].is_a?(String)
31
+ hash['href'] = hash['href'].gsub('%23', '')
32
+ .gsub(/%2Fschemata(%2F[^%]*%2F)/,
33
+ '%23%2Fdefinitions\1')
34
+ end
35
+ hash.each_with_object({}) { |(k, v), r| r[k] = reference_localizer(v) }
36
+ end
37
+
38
+ #
39
+ # @param [Object] datum
40
+ # @return [Object]
41
+ def reference_localizer(datum)
42
+ case datum
43
+ when Array
44
+ reference_localizer_array(datum)
45
+ when Hash
46
+ reference_localizer_hash(datum)
47
+ else
48
+ datum
49
+ end
50
+ end
51
+
52
+ #
53
+ # @param [Prmd::SchemaHash] schemata
54
+ # @return [Prmd::Schema]
55
+ def combine(*schemata)
56
+ # tracks which entities where defined in which file
57
+ schemata_map = {}
58
+
59
+ data = {}
60
+ data.merge!(@base)
61
+ data.merge!(@meta)
62
+
63
+ schemata.each do |schema|
64
+ id = schema.fetch('id')
65
+ id_ary = id.split('/').last
66
+
67
+ if s = schemata_map[id]
68
+ $stderr.puts "`#{id}` (from #{schema.filename}) was already defined" \
69
+ "in `#{s.filename}` and will overwrite the first" \
70
+ "definition"
71
+ end
72
+ # avoinding damaging the original schema
73
+ embedded_schema = schema.dup
74
+ # schemas are now in a single scope by combine
75
+ embedded_schema.delete('id')
76
+ schemata_map[id] = embedded_schema
77
+
78
+ data['definitions'][id_ary] = embedded_schema.to_h
79
+ data['properties'][id_ary] = { '$ref' => "#/definitions/#{id_ary}" }
80
+
81
+ reference_localizer(data['definitions'][id_ary])
82
+ end
83
+
84
+ Prmd::Schema.new(data)
85
+ end
86
+
87
+ private :reference_localizer_array
88
+ private :reference_localizer_hash
89
+ private :reference_localizer
90
+ end
91
+ end
@@ -0,0 +1,27 @@
1
+ require 'json'
2
+ require 'prmd/schema'
3
+
4
+ # :nodoc:
5
+ module Prmd
6
+ # Schema generator
7
+ class Generator
8
+ #
9
+ # @param [Hash<Symbol, Object>] properties
10
+ def initialize(properties = {})
11
+ @properties = properties
12
+ @base = properties.fetch(:base, {})
13
+ @template = properties.fetch(:template)
14
+ end
15
+
16
+ #
17
+ # @param [Hash<Symbol, Object>] options
18
+ def generate(options = {})
19
+ res = @template.result(options)
20
+ resource_schema = JSON.parse(res)
21
+ schema = Prmd::Schema.new
22
+ schema.merge!(@base)
23
+ schema.merge!(resource_schema)
24
+ schema
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,56 @@
1
+ require 'prmd/template'
2
+
3
+ # :nodoc:
4
+ module Prmd
5
+ # Schema Generator
6
+ class Renderer
7
+ #
8
+ # @param [Hash<Symbol, Object>] properties
9
+ def initialize(properties = {})
10
+ @properties = properties
11
+ @template = @properties.fetch(:template)
12
+ end
13
+
14
+ #
15
+ # @return [Hash<Symbol, Object>]
16
+ def default_options
17
+ {
18
+ http_header: {},
19
+ content_type: 'application/json',
20
+ doc: {},
21
+ prepend: nil
22
+ }
23
+ end
24
+
25
+ #
26
+ # @param [Hash<Symbol, Object>] options
27
+ # @return [void]
28
+ def append_default_options(options)
29
+ options[:doc] = {
30
+ url_style: 'default',
31
+ disable_title_and_description: false
32
+ }.merge(options[:doc])
33
+ end
34
+
35
+ #
36
+ # @param [Hash<Symbol, Object>] options
37
+ # @return [Hash<Symbol, Object>]
38
+ def setup_options(options)
39
+ opts = default_options
40
+ opts.merge!(options)
41
+ append_default_options(opts)
42
+ opts
43
+ end
44
+
45
+ #
46
+ # @param [Prmd::Schema] schema
47
+ # @param [Hash<Symbol, Object>] options
48
+ def render(schema, options = {})
49
+ @template.result(schema: schema, options: setup_options(options))
50
+ end
51
+
52
+ private :default_options
53
+ private :append_default_options
54
+ private :setup_options
55
+ end
56
+ end
@@ -0,0 +1,47 @@
1
+ require 'forwardable'
2
+
3
+ # :nodoc:
4
+ module Prmd
5
+ # Specialized Hash for handling loaded Schema data
6
+ class SchemaHash
7
+ extend Forwardable
8
+
9
+ # @return [Hash]
10
+ attr_reader :data
11
+ # @return [String]
12
+ attr_reader :filename
13
+
14
+ def_delegator :@data, :[]
15
+ def_delegator :@data, :[]=
16
+ def_delegator :@data, :delete
17
+ def_delegator :@data, :each
18
+
19
+ # @param [Hash] data
20
+ # @param [Hash<Symbol, Object>] options
21
+ def initialize(data, options = {})
22
+ @data = data
23
+ @filename = options.fetch(:filename, '')
24
+ end
25
+
26
+ # @param [Prmd::SchemaHash] other
27
+ # @return [self]
28
+ def initialize_copy(other)
29
+ super
30
+ @data = other.data.dup
31
+ @filename = other.filename.dup
32
+ end
33
+
34
+ # @param [String] key
35
+ # @return [self]
36
+ def fetch(key)
37
+ @data.fetch(key) { abort "Missing key #{key} in #{filename}" }
38
+ end
39
+
40
+ # @return [Hash]
41
+ def to_h
42
+ @data.dup
43
+ end
44
+
45
+ protected :data
46
+ end
47
+ end
@@ -0,0 +1,6 @@
1
+ require 'optparse'
2
+
3
+ # Extension of the standard library OptionParser
4
+ class OptionParser
5
+ alias :to_str :to_s
6
+ end
@@ -0,0 +1,38 @@
1
+ # :nodoc:
2
+ module Prmd
3
+ # Hash helper methods
4
+ #
5
+ # @api private
6
+ module HashHelpers
7
+ # Attempts to convert all keys in the hash to a Symbol.
8
+ # This operation is recursive with subhashes
9
+ #
10
+ # @param [Hash] hash
11
+ # @return [Hash]
12
+ def self.deep_symbolize_keys(hash)
13
+ deep_transform_keys(hash) do |key|
14
+ if key.respond_to?(:to_sym)
15
+ key.to_sym
16
+ else
17
+ key
18
+ end
19
+ end
20
+ end
21
+
22
+ # Think of this as hash.keys.map! { |key| }, that actually maps recursively.
23
+ #
24
+ # @param [Hash] hash
25
+ # @return [Hash]
26
+ # @yield [Object] key
27
+ def self.deep_transform_keys(hash, &block)
28
+ result = {}
29
+ hash.each do |key, value|
30
+ new_key = yield(key)
31
+ new_value = value
32
+ new_value = deep_transform_keys(value, &block) if value.is_a?(Hash)
33
+ result[new_key] = new_value
34
+ end
35
+ result
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ require 'yaml'
2
+ require 'json'
3
+
4
+ # :nodoc:
5
+ module Prmd
6
+ # Attempts to load either a json or yaml file, the type is determined by
7
+ # filename extension.
8
+ #
9
+ # @param [String] filename
10
+ # @return [Object] data
11
+ def self.load_schema_file(filename)
12
+ extname = File.extname(filename)
13
+ File.open(filename) do |file|
14
+ case extname.downcase
15
+ when '.yaml', '.yml'
16
+ YAML.load(file.read)
17
+ when '.json'
18
+ JSON.load(file.read)
19
+ else
20
+ abort "Cannot load schema file #{filename}" \
21
+ "(unsupported file extension #{extname})"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,33 @@
1
+ require 'rake'
2
+ require 'rake/tasklib'
3
+
4
+ # :nodoc:
5
+ module Prmd
6
+ # :nodoc:
7
+ module RakeTasks
8
+ # Common class for Prmd rake tasks
9
+ #
10
+ # @api private
11
+ class Base < Rake::TaskLib
12
+ # The name of the task
13
+ # @return [String] the task name
14
+ attr_accessor :name
15
+
16
+ # Options to pass to command
17
+ # @return [Hash<Symbol, Object>] the options passed to the command
18
+ attr_accessor :options
19
+
20
+ # Creates a new task with name +name+.
21
+ #
22
+ # @param [String, Symbol] name the name of the rake task
23
+ def initialize(name)
24
+ @name = name
25
+ @options = {}
26
+
27
+ yield self if block_given?
28
+
29
+ define
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,50 @@
1
+ require 'prmd/commands/combine'
2
+ require 'prmd/rake_tasks/base'
3
+
4
+ # :nodoc:
5
+ module Prmd
6
+ # :nodoc:
7
+ module RakeTasks
8
+ # Schema combine rake task
9
+ #
10
+ # @example
11
+ # Prmd::RakeTasks::Combine.new do |t|
12
+ # t.options[:meta] = 'schema/meta.json'
13
+ # t.paths << 'schema/schemata/api'
14
+ # t.output_file = 'schema/api.json'
15
+ # end
16
+ class Combine < Base
17
+ #
18
+ # @return [Array<String>] list of paths
19
+ attr_accessor :paths
20
+
21
+ # target file the combined result should be written
22
+ # @return [String>] target filename
23
+ attr_accessor :output_file
24
+
25
+ # Creates a new task with name +name+.
26
+ #
27
+ # @param [String, Symbol] name the name of the rake task
28
+ def initialize(name = :combine)
29
+ @paths = []
30
+ super
31
+ end
32
+
33
+ protected
34
+
35
+ # Defines the rake task
36
+ # @return [void]
37
+ def define
38
+ desc 'Combine schemas' unless Rake.application.last_comment
39
+ task(name) do
40
+ result = Prmd.combine(paths, options)
41
+ if output_file
42
+ File.open(output_file, 'w') do |file|
43
+ file.write(result)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,73 @@
1
+ require 'prmd/commands/render'
2
+ require 'prmd/rake_tasks/base'
3
+ require 'prmd/load_schema_file'
4
+ require 'prmd/template'
5
+ require 'prmd/schema'
6
+
7
+ # :nodoc:
8
+ module Prmd
9
+ # :nodoc:
10
+ module RakeTasks
11
+ # Documentation rake task
12
+ #
13
+ # @example
14
+ # Prmd::RakeTasks::Doc.new do |t|
15
+ # t.files = { 'schema/api.json' => 'schema/api.md' }
16
+ # end
17
+ class Doc < Base
18
+ # Schema files that should be verified
19
+ # @return [Array<String>, Hash<String, String>] list of files
20
+ attr_accessor :files
21
+
22
+ # Creates a new task with name +name+.
23
+ #
24
+ # @param [String, Symbol] name the name of the rake task
25
+ def initialize(name = :doc, &block)
26
+ @files = []
27
+ super name, &block
28
+ @options[:template] ||= Prmd::Template.template_dirname
29
+ end
30
+
31
+ private
32
+
33
+ # Render file to markdown
34
+ #
35
+ # @param [String] filename
36
+ # @return (see Prmd.render)
37
+ def render_file(filename)
38
+ data = Prmd.load_schema_file(filename)
39
+ schema = Prmd::Schema.new(data)
40
+ Prmd.render(schema, options)
41
+ end
42
+
43
+ # @param [String] infile
44
+ # @param [String] outfile
45
+ # @return [void]
46
+ def render_to_file(infile, outfile)
47
+ result = render_file(infile)
48
+ File.open(outfile, 'w') do |file|
49
+ file.write(result)
50
+ end
51
+ end
52
+
53
+ protected
54
+
55
+ # Defines the rake task
56
+ # @return [void]
57
+ def define
58
+ desc 'Verifying schemas' unless Rake.application.last_comment
59
+ task(name) do
60
+ if files.is_a?(Hash)
61
+ files.each do |infile, outfile|
62
+ render_to_file(infile, outfile)
63
+ end
64
+ else
65
+ files.each do |infile|
66
+ render_to_file(infile, infile.ext('md'))
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,60 @@
1
+ require 'prmd/commands/verify'
2
+ require 'prmd/rake_tasks/base'
3
+ require 'prmd/load_schema_file'
4
+
5
+ # :nodoc:
6
+ module Prmd
7
+ # :nodoc:
8
+ module RakeTasks
9
+ # Schema Verify rake task
10
+ #
11
+ # @example
12
+ # Prmd::RakeTasks::Verify.new do |t|
13
+ # t.files << 'schema/api.json'
14
+ # end
15
+ class Verify < Base
16
+ # Schema files that should be verified
17
+ # @return [Array<String>] list of files
18
+ attr_accessor :files
19
+
20
+ # Creates a new task with name +name+.
21
+ #
22
+ # @param [String, Symbol] name the name of the rake task
23
+ def initialize(name = :verify)
24
+ @files = []
25
+ super
26
+ end
27
+
28
+ private
29
+
30
+ # Defines the rake task
31
+ #
32
+ # @param [String] filename
33
+ # @return [Array<String>] list of errors produced
34
+ def verify_file(filename)
35
+ data = Prmd.load_schema_file(filename)
36
+ errors = Prmd.verify(data)
37
+ unless errors.empty?
38
+ errors.map! { |error| "#{filename}: #{error}" } if filename
39
+ errors.each { |error| $stderr.puts(error) }
40
+ end
41
+ errors
42
+ end
43
+
44
+ protected
45
+
46
+ # Defines the rake task
47
+ # @return [void]
48
+ def define
49
+ desc 'Verifying schemas' unless Rake.application.last_comment
50
+ task(name) do
51
+ all_errors = []
52
+ files.each do |filename|
53
+ all_errors.concat(verify_file(filename))
54
+ end
55
+ fail unless all_errors.empty?
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end