schai 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 62bb3d9288d6298c4ec34c3d020c90b10efb49ae
4
+ data.tar.gz: 6e1e49eaac72c458456befbe296248dc7dca0444
5
+ SHA512:
6
+ metadata.gz: 6dfcef93f017aa493767493a0f80a0cd5d11903232f35bd075ca7c79f48cf651ed34cae397a19051410d20ec7357e1ab55055fdca7960bde91f854b4cc860367
7
+ data.tar.gz: 17463af517546e75ed0e6594a745f406eddd05e9b057f0af65f68ee2fb75922e3f53db5e300d91f6b21db4f42f1af357c666f5ed96e859e52900619c2892e8f4
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in schai.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 gin0606
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # schai
2
+ ## Installation
3
+
4
+ Add this line to your application's Gemfile:
5
+
6
+ ```ruby
7
+ gem 'schai'
8
+ ```
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install schai
17
+
18
+ ## Usage
19
+
20
+ ```yaml
21
+ # foo.yaml
22
+ type: object
23
+ properties:
24
+ foo:
25
+ type: string
26
+ bar:
27
+ type: string
28
+ optional: true # default value is `false`
29
+ baz_list:
30
+ type: array
31
+ items:
32
+ include: baz.yaml
33
+
34
+ # baz.yaml
35
+ type: object
36
+ properties:
37
+ baz:
38
+ type: string
39
+ ```
40
+
41
+ ```sh
42
+ $ bundle exec schai --yaml foo.yaml --to foo.json
43
+ ```
44
+
45
+ ```json
46
+ {
47
+ "$schema": "http://json-schema.org/draft-04/schema#",
48
+ "type": "object",
49
+ "properties": {
50
+ "foo": {
51
+ "type": "string"
52
+ },
53
+ "bar": {
54
+ "type": "string"
55
+ },
56
+ "baz_list": {
57
+ "type": "array",
58
+ "items": {
59
+ "type": "object",
60
+ "properties": {
61
+ "baz": {
62
+ "type": "string"
63
+ }
64
+ },
65
+ "required": [
66
+ "baz"
67
+ ]
68
+ }
69
+ }
70
+ },
71
+ "required": [
72
+ "foo",
73
+ "baz_list"
74
+ ]
75
+ }
76
+ ```
77
+
78
+ ## Contributing
79
+ 1. Fork it ( https://github.com/[my-github-username]/schai/fork )
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/schai ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "schai"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ Schai::CLI.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/schai/cli.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'schai'
2
+ require 'thor'
3
+
4
+ module Schai
5
+ class CLI < Thor
6
+ desc "hello", "say 'hello world!'."
7
+ def hello
8
+ puts "Hello World!"
9
+ end
10
+
11
+ desc "gen", "generate json schema from yaml"
12
+ option :yaml, :require => true
13
+ option :to, :require => false
14
+ def gen
15
+ schai = Schai.parse_file options[:yaml]
16
+
17
+ json = JSON.pretty_generate(schai.to_schema)
18
+ if dist_file = options[:to]
19
+ File.open(dist_file, "w") {|f| f.puts json}
20
+ else
21
+ puts json
22
+ end
23
+ end
24
+ end
25
+ end
data/lib/schai/hoge.rb ADDED
@@ -0,0 +1,156 @@
1
+ require 'yaml'
2
+ require 'json'
3
+
4
+ module Schai
5
+ module Optional
6
+ attr_accessor :optional
7
+ end
8
+
9
+ module Metadata
10
+ attr_accessor :description, :example
11
+ end
12
+
13
+ def self.parse params
14
+ Root.parse params
15
+ end
16
+
17
+ def self.parse_file path
18
+ @@path ||=[]
19
+ if @@path.empty?
20
+ @@path << path
21
+ ret = parse YAML.load_file(path)
22
+ @@path.pop
23
+ else
24
+ expand_path = File.expand_path("../#{path}", @@path.last)
25
+ @@path << expand_path
26
+ ret = parse YAML.load_file(expand_path)
27
+ @@path.pop
28
+ end
29
+ ret
30
+ end
31
+
32
+ class Root
33
+ attr_accessor :schema
34
+
35
+ def self.parse params
36
+ ret = self.new
37
+ ret.schema = parse_components params
38
+ ret
39
+ end
40
+
41
+ def self.parse_components params
42
+ case
43
+ when params.has_key?('include')
44
+ included_schema = Schai.parse_file(params.delete('include')).schema
45
+ params.each do |k, v|
46
+ setter = "#{k}=".to_sym
47
+ included_schema.send(setter, v)
48
+ end
49
+ included_schema
50
+ when params['type'] == 'object'
51
+ Object.parse params
52
+ when params['type'] == 'array'
53
+ Array.parse params
54
+ when !params.has_key?('type')
55
+ raise "typeは必須(#{params})"
56
+ else
57
+ Schai::Property.parse params
58
+ end
59
+ end
60
+
61
+ def to_schema
62
+ schema = {
63
+ '$schema': "http://json-schema.org/draft-04/schema#"
64
+ }
65
+ schema.merge @schema.to_schema
66
+ end
67
+ end
68
+
69
+ class Object
70
+ include Optional
71
+ include Metadata
72
+
73
+ attr_accessor :all
74
+
75
+ def self.parse params
76
+ self.new params
77
+ end
78
+
79
+ def initialize params
80
+ @all = Hash[params["properties"].map {|k, v|
81
+ [k || 'null', Root.parse_components(v)]
82
+ }]
83
+ @description = params["description"]
84
+ @example = params["example"]
85
+ @optional = params["optional"]
86
+ end
87
+
88
+ def to_schema
89
+ schema = {}
90
+ schema[:type] = :object
91
+ schema[:description] = @description if @description
92
+ schema[:properties] = Hash[@all.map {|k, p| [k, p.to_schema]}]
93
+ schema[:required] = @all.select{|k, p| !p.optional}.map{|k, p| k || 'null'}
94
+ schema[:example] = @example if @example
95
+ schema
96
+ end
97
+ end
98
+
99
+ class Array
100
+ include Optional
101
+ include Metadata
102
+
103
+ attr_accessor :items
104
+ def self.parse params
105
+ self.new params
106
+ end
107
+
108
+ def initialize params
109
+ @items = Property.parse params['items']
110
+ @description = params["description"]
111
+ @example = params["example"]
112
+ @optional = params["optional"]
113
+ end
114
+
115
+ def to_schema
116
+ schema = {
117
+ type: :array,
118
+ }
119
+ schema[:description] = @description if @description
120
+ schema[:items] = @items.to_schema
121
+ schema[:example] = @example if @example
122
+ schema
123
+ end
124
+ end
125
+
126
+ class Property
127
+ include Optional
128
+ include Metadata
129
+
130
+ attr_accessor :type, :format
131
+
132
+ def self.parse params
133
+ if params.has_key? 'include'
134
+ Schai.parse_file(params['include']).schema
135
+ else
136
+ self.new params
137
+ end
138
+ end
139
+
140
+ def initialize params
141
+ params.each do |k, v|
142
+ setter = "#{k}=".to_sym
143
+ self.send(setter, v)
144
+ end
145
+ end
146
+
147
+ def to_schema
148
+ ret = {}
149
+ ret[:type] = (@type || 'null').to_sym
150
+ ret[:format] = @format if @format
151
+ ret[:description] = @description if @description
152
+ ret[:example] = @example if @example
153
+ ret
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,3 @@
1
+ module Schai
2
+ VERSION = "0.1.0"
3
+ end
data/lib/schai.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "schai/version"
2
+ require "schai/hoge"
3
+ require "schai/cli"
4
+
5
+ module Schai
6
+ # Your code goes here...
7
+ end
data/schai.gemspec ADDED
@@ -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 'schai/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "schai"
8
+ spec.version = Schai::VERSION
9
+ spec.authors = ["gin0606"]
10
+ spec.email = ["kkgn06@gmail.com"]
11
+
12
+ spec.summary = "json schema tsurai"
13
+ spec.description = "json schema tsurai"
14
+ spec.homepage = "https://github.com/gin0606/schai"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "bin"
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'thor'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: schai
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - gin0606
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: json schema tsurai
70
+ email:
71
+ - kkgn06@gmail.com
72
+ executables:
73
+ - schai
74
+ - setup
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/schai
87
+ - bin/setup
88
+ - lib/schai.rb
89
+ - lib/schai/cli.rb
90
+ - lib/schai/hoge.rb
91
+ - lib/schai/version.rb
92
+ - schai.gemspec
93
+ homepage: https://github.com/gin0606/schai
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.5
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: json schema tsurai
117
+ test_files: []