json-schema-generator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,3 @@
1
+ [submodule "tests"]
2
+ path = tests
3
+ url = git://github.com/maxlinc/json-schema-kata.git
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - jruby-19mode
6
+ - rbx-19mode
7
+ env:
8
+ EXCLUDE_SCHEMA_VERSION=draft4
9
+ JSON_GENERATOR="bundle exec json_schema_generator"
10
+ before_install:
11
+ - git submodule update --init --recursive
12
+ - cd tests && bundle install
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json-schema-generator.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Max Lincoln
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,103 @@
1
+ # JSON::SchemaGenerator
2
+
3
+ A very basic json-schema generator. Currently only has draft-03 support.
4
+ The tests, including draft-04 tests, are in the json-schema-kata submodule.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'json-schema-generator'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install json-schema-generator
19
+
20
+ ## Usage
21
+
22
+ JSON::SchemaGenerator.generate name, data, version
23
+
24
+ e.g.
25
+
26
+ JSON::SchemaGenerator.generate 'sample_file.json', File.read('sample_file.json'), 'draft3'
27
+
28
+ ## Testing
29
+
30
+ The tests are actually in a reusable submodule of platform agnostic json-schema tests.
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
39
+
40
+ TODO: Write a gem description
41
+
42
+ ## Installation
43
+
44
+ Add this line to your application's Gemfile:
45
+
46
+ gem 'json-schema-generator'
47
+
48
+ And then execute:
49
+
50
+ $ bundle
51
+
52
+ Or install it yourself as:
53
+
54
+ $ gem install json-schema-generator
55
+
56
+ ## Usage
57
+
58
+ TODO: Write usage instructions here
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create new Pull Request
67
+
68
+ TODO: Write a gem description
69
+
70
+ ## Installation
71
+
72
+ Add this line to your application's Gemfile:
73
+
74
+ gem 'json-schema-generator'
75
+
76
+ And then execute:
77
+
78
+ $ bundle
79
+
80
+ Or install it yourself as:
81
+
82
+ $ gem install json-schema-generator
83
+
84
+ ## Usage
85
+
86
+ TODO: Write usage instructions here
87
+
88
+ ## Contributing
89
+
90
+ 1. Fork it
91
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
92
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
93
+ 4. Push to the branch (`git push origin my-new-feature`)
94
+ 5. Create new Pull Request
95
+ TODO: Write usage instructions here
96
+
97
+ ## Contributing
98
+
99
+ 1. Fork it
100
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
101
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
102
+ 4. Push to the branch (`git push origin my-new-feature`)
103
+ 5. Create new Pull Request
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :test
4
+
5
+ task :test do
6
+ ENV['JSON_GENERATOR'] = 'bundle exec json_schema_generator'
7
+ ENV['PATH'] = ENV['PATH'] + File::PATH_SEPARATOR + File.expand_path('bin', Dir.pwd)
8
+ puts ENV['PATH']
9
+ system 'cd tests && bundle exec rake'
10
+ fail 'Tests did not pass!' unless $?.success?
11
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'json-schema-generator'
4
+ require 'json/schema_generator_cli'
5
+ JSON::SchemaGeneratorCLI.new(ARGV.dup).execute!
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'json/schema_generator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json-schema-generator"
8
+ spec.version = JSON::SchemaGenerator::VERSION
9
+ spec.authors = ["Max Lincoln"]
10
+ spec.email = ["max@devopsy.com"]
11
+ spec.description = %q{A very basic json-schema generator}
12
+ spec.summary = %q{A very basic json-schema generator}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "json-schema"
25
+ spec.add_development_dependency "jsonpath"
26
+ spec.add_development_dependency "travis"
27
+ end
@@ -0,0 +1,4 @@
1
+ require 'json'
2
+ require 'json/schema_generator'
3
+ require 'json/schema_generator_cli'
4
+ require 'stringio'
@@ -0,0 +1,100 @@
1
+ module JSON
2
+ class SchemaGenerator
3
+ class << self
4
+ def generate name, data, version
5
+ generator = JSON::SchemaGenerator.new name, version
6
+ generator.generate data
7
+ end
8
+ end
9
+
10
+ def initialize name, version = 'draft3'
11
+ @buffer = StringIO.new
12
+ @name = name
13
+ @version = 'draft-03' if version == 'draft3'
14
+ @version = 'draft-04' if version == 'draft4'
15
+ end
16
+
17
+ def generate raw_data
18
+ # Write header
19
+ @buffer.puts "{"
20
+ @buffer.puts "\"$schema\": \"http://json-schema.org/#{@version}/schema#\","
21
+ @buffer.print '"description": "Generated from '
22
+ @buffer.print @name
23
+ @buffer.print " with shasum "
24
+ @buffer.print Digest::SHA1.hexdigest raw_data
25
+ @buffer.puts '",'
26
+
27
+ #
28
+ data = JSON.load(raw_data)
29
+
30
+ @buffer.puts create_hash(data)
31
+ close
32
+ result
33
+ end
34
+
35
+ protected
36
+
37
+ def close
38
+ @buffer.puts "}"
39
+ end
40
+
41
+ def create_values(key, value)
42
+ buffer = StringIO.new
43
+ buffer.puts "\"#{key}\": {"
44
+ case value
45
+ when TrueClass, FalseClass
46
+ buffer.puts '"type": "boolean",'
47
+ buffer.puts '"required": true'
48
+
49
+ when String
50
+ buffer.puts '"type": "string",'
51
+ buffer.puts '"required": true'
52
+
53
+ when Integer
54
+ buffer.puts '"type": "integer",'
55
+ buffer.puts '"required": true'
56
+
57
+ when Float
58
+ buffer.puts '"type": "number",'
59
+ buffer.puts '"required": true'
60
+ when Array
61
+ buffer << create_array(value)
62
+ when Hash
63
+ buffer << create_hash(value)
64
+ else
65
+ raise "Unknown Type for #{key}! #{value.class}"
66
+ end
67
+ buffer.print "}"
68
+ buffer.string
69
+ end
70
+
71
+ def create_hash(data)
72
+ buffer = StringIO.new
73
+ buffer.puts '"type": "object",'
74
+ buffer.puts '"required": true,'
75
+ buffer.puts '"properties": {'
76
+ items = data.collect do |k,v|
77
+ create_values k,v
78
+ end
79
+ buffer << items.join(",\n")
80
+ buffer.puts '}'
81
+
82
+ buffer.string
83
+ end
84
+
85
+ def create_array(data)
86
+ buffer = StringIO.new
87
+ buffer.puts '"type": "array",'
88
+ buffer.puts '"required": true,'
89
+ buffer.puts '"minItems": 1,'
90
+ buffer.puts '"uniqueItems": true,'
91
+ buffer.puts create_values("items", data.first)
92
+
93
+ buffer.string
94
+ end
95
+
96
+ def result
97
+ @buffer.string
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,5 @@
1
+ module JSON
2
+ class SchemaGenerator
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ class JSON::SchemaGeneratorCLI
2
+ def initialize(argv, stdin=STDIN, stdout=STDOUT, stderr=STDERR, kernel=Kernel)
3
+ @argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel
4
+ end
5
+
6
+ def execute!
7
+ file = ARGV[0]
8
+ schema = JSON.parse(JSON::SchemaGenerator.generate file, File.read(file), ARGV[1])
9
+ @stdout.puts JSON.pretty_generate schema
10
+ @kernel.exit(0)
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json-schema-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Max Lincoln
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: json-schema
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: jsonpath
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: travis
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: A very basic json-schema generator
111
+ email:
112
+ - max@devopsy.com
113
+ executables:
114
+ - json_schema_generator
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .gitmodules
120
+ - .travis.yml
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/json_schema_generator
126
+ - json-schema-generator.gemspec
127
+ - lib/json-schema-generator.rb
128
+ - lib/json/schema_generator.rb
129
+ - lib/json/schema_generator/version.rb
130
+ - lib/json/schema_generator_cli.rb
131
+ homepage: ''
132
+ licenses:
133
+ - MIT
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 1.8.23
153
+ signing_key:
154
+ specification_version: 3
155
+ summary: A very basic json-schema generator
156
+ test_files: []
157
+ has_rdoc: