yaml_ext 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5c50a167ebcd2e16535c639ffb075f669e5460f36f1529f115c0ed9da5147b51
4
+ data.tar.gz: 12bdbf3f9ee69f5dbdd331c013b2708c585a4de4ab692399fad4821fabef1ec0
5
+ SHA512:
6
+ metadata.gz: 34340ea31a332e792426961325dc2312dc6c2e643d0aea00f9bed21a0f12fba20823282af5181f205ea92991d407caba692c2dfbc26afaf8921f489e82b4ec7c
7
+ data.tar.gz: 9f5561dd431c156a93a71326e275f37458d25915c18d448b2611edcad207ab4dab83ed9a67fd1a121b6b9db9e0e90120e239b07c8da79a1019909f291334083c
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in yaml_ext.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Ken Iiboshi
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # YamlExt
2
+
3
+ Multiple YAML Loader.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "yaml_ext"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle install
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ pry(main)> YamlExt.load("example/schema.yml")
23
+ => {"openapi"=>"3.0.0",
24
+ "info"=>{"version"=>"1.0.0", "title"=>"Example API"},
25
+ "servers"=>[{"url"=>"http://example.com/v1"}],
26
+ "paths"=>
27
+ {"/users"=>
28
+ {"get"=>
29
+ {"summary"=>"List all users",
30
+ "operationId"=>"listUsers",
31
+ "tags"=>["users"],
32
+ "parameters"=>[{"name"=>"limit", "in"=>"query", "description"=>"How many items to return at one time (max 100)", "required"=>false, "schema"=>{"type"=>"integer"}}],
33
+ "responses"=>
34
+ {"200"=>
35
+ {"description"=>"A paged array of users",
36
+ "headers"=>{"x-next"=>{"description"=>"A link to the next page of responses", "schema"=>{"type"=>"string"}}},
37
+ "content"=>{"application/json"=>{"schema"=>{"type"=>"array", "items"=>{"$ref"=>"#/components/schemas/User"}}}}},
38
+ "default"=>{"description"=>"unexpected error", "content"=>{"application/json"=>{"schema"=>{"type"=>"object", "required"=>["code", "message"], "properties"=>{"code"=>{"type"=>"integer"}, "message"=>{"type"=>"string"}}}}}}}},
39
+ "post"=>
40
+ {"summary"=>"Create a user",
41
+ "operationId"=>"createUsers",
42
+ "tags"=>["users"],
43
+ "responses"=>
44
+ {"201"=>{"description"=>"Null response"},
45
+ "default"=>{"description"=>"unexpected error", "content"=>{"application/json"=>{"schema"=>{"type"=>"object", "required"=>["code", "message"], "properties"=>{"code"=>{"type"=>"integer"}, "message"=>{"type"=>"string"}}}}}}}}},
46
+ "/users/{userId}"=>
47
+ {"get"=>
48
+ {"get"=>
49
+ {"summary"=>"Info for a specific user",
50
+ "operationId"=>"showUserById",
51
+ "tags"=>["users"],
52
+ "parameters"=>[{"name"=>"userId", "in"=>"path", "description"=>"The id of the user", "required"=>true, "schema"=>{"type"=>"integer"}}],
53
+ "responses"=>
54
+ {"200"=>{"description"=>"Expected response to a valid request", "content"=>{"application/json"=>{"schema"=>{"type"=>"object", "required"=>["id", "name"], "properties"=>{"id"=>{"type"=>"integer"}, "name"=>{"type"=>"string"}, "tag"=>{"type"=>"string"}}}}}},
55
+ "default"=>{"description"=>"unexpected error", "content"=>{"application/json"=>{"schema"=>{"type"=>"object", "required"=>["code", "message"], "properties"=>{"code"=>{"type"=>"integer"}, "message"=>{"type"=>"string"}}}}}}}}}}},
56
+ "components"=>
57
+ {"schemas"=>
58
+ {"User"=>{"type"=>"object", "required"=>["id", "name"], "properties"=>{"id"=>{"type"=>"integer"}, "name"=>{"type"=>"string"}, "tag"=>{"type"=>"string"}}},
59
+ "Users"=>{"type"=>"array", "items"=>{"type"=>"object", "required"=>["id", "name"], "properties"=>{"id"=>{"type"=>"integer"}, "name"=>{"type"=>"string"}, "tag"=>{"type"=>"string"}}}},
60
+ "Error"=>{"type"=>"object", "required"=>["code", "message"], "properties"=>{"code"=>{"type"=>"integer"}, "message"=>{"type"=>"string"}}}}}}
61
+ ```
62
+
63
+ ## Development
64
+
65
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+
67
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/i2bskn/yaml_ext.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "yaml_ext"
5
+
6
+ require "pry"
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,2 @@
1
+ schemas:
2
+ $ref: ./schemas/index.yml
@@ -0,0 +1,9 @@
1
+ type: object
2
+ required:
3
+ - code
4
+ - message
5
+ properties:
6
+ code:
7
+ type: integer
8
+ message:
9
+ type: string
@@ -0,0 +1,11 @@
1
+ type: object
2
+ required:
3
+ - id
4
+ - name
5
+ properties:
6
+ id:
7
+ type: integer
8
+ name:
9
+ type: string
10
+ tag:
11
+ type: string
@@ -0,0 +1,3 @@
1
+ type: array
2
+ items:
3
+ $ref: "#/components/schemas/User"
@@ -0,0 +1,6 @@
1
+ User:
2
+ $ref: ./User.yml
3
+ Users:
4
+ $ref: ./Users.yml
5
+ Error:
6
+ $ref: ./Error.yml
@@ -0,0 +1,8 @@
1
+ /users:
2
+ get:
3
+ $ref: ./users/list_users.yml
4
+ post:
5
+ $ref: ./users/create_users.yml
6
+ /users/{userId}:
7
+ get:
8
+ $ref: ./users/show_user_by_id.yml
@@ -0,0 +1,13 @@
1
+ summary: Create a user
2
+ operationId: createUsers
3
+ tags:
4
+ - users
5
+ responses:
6
+ '201':
7
+ description: Null response
8
+ default:
9
+ description: unexpected error
10
+ content:
11
+ application/json:
12
+ schema:
13
+ $ref: "#/components/schemas/Error"
@@ -0,0 +1,29 @@
1
+ summary: List all users
2
+ operationId: listUsers
3
+ tags:
4
+ - users
5
+ parameters:
6
+ - name: limit
7
+ in: query
8
+ description: How many items to return at one time (max 100)
9
+ required: false
10
+ schema:
11
+ type: integer
12
+ responses:
13
+ '200':
14
+ description: A paged array of users
15
+ headers:
16
+ x-next:
17
+ description: A link to the next page of responses
18
+ schema:
19
+ type: string
20
+ content:
21
+ application/json:
22
+ schema:
23
+ $ref: "#/components/schemas/Users"
24
+ default:
25
+ description: unexpected error
26
+ content:
27
+ application/json:
28
+ schema:
29
+ $ref: "#/components/schemas/Error"
@@ -0,0 +1,25 @@
1
+ get:
2
+ summary: Info for a specific user
3
+ operationId: showUserById
4
+ tags:
5
+ - users
6
+ parameters:
7
+ - name: userId
8
+ in: path
9
+ description: The id of the user
10
+ required: true
11
+ schema:
12
+ type: integer
13
+ responses:
14
+ '200':
15
+ description: Expected response to a valid request
16
+ content:
17
+ application/json:
18
+ schema:
19
+ $ref: "#/components/schemas/User"
20
+ default:
21
+ description: unexpected error
22
+ content:
23
+ application/json:
24
+ schema:
25
+ $ref: "#/components/schemas/Error"
@@ -0,0 +1,10 @@
1
+ openapi: "3.0.0"
2
+ info:
3
+ version: 1.0.0
4
+ title: Example API
5
+ servers:
6
+ - url: http://example.com/v1
7
+ paths:
8
+ $ref: ./paths/index.yml
9
+ components:
10
+ $ref: ./components/index.yml
data/lib/yaml_ext.rb ADDED
@@ -0,0 +1,79 @@
1
+ require "erb"
2
+ require "yaml"
3
+ require "yaml_ext/version"
4
+
5
+ module YamlExt
6
+ class << self
7
+ def load(path)
8
+ resolve_ref_inner_json(load_file(path))
9
+ end
10
+
11
+ def load_file(path)
12
+ parse_extended_node(load_yaml_file(path), path)
13
+ end
14
+
15
+ private
16
+
17
+ def load_yaml_file(path)
18
+ YAML.load(ERB.new(File.read(path), nil, "-").result)
19
+ end
20
+
21
+ def parse_extended_node(node, path)
22
+ case node
23
+ when Hash
24
+ node.each_with_object({}) { |(k, v), obj|
25
+ if k == "$ref" && v.is_a?(String) && !v.start_with?("#/")
26
+ value = load_file(File.expand_path(v, File.dirname(path)))
27
+ case value
28
+ when Hash
29
+ obj = {} unless obj.is_a?(Hash)
30
+ obj.merge!(value)
31
+ when Array
32
+ obj = [] unless obj.is_a?(Array)
33
+ obj.concat(value)
34
+ else
35
+ obj = value
36
+ end
37
+ else
38
+ obj = {} unless obj.is_a?(Hash)
39
+ obj[k] = parse_extended_node(v, path)
40
+ end
41
+ }
42
+ when Array
43
+ node.map { |n| parse_extended_node(n, path) }
44
+ else
45
+ node
46
+ end
47
+ end
48
+
49
+ def resolve_ref_inner_json(node, node_tree = nil)
50
+ node_tree ||= node
51
+
52
+ case node
53
+ when Hash
54
+ node.each_with_object({}) { |(k, v), obj|
55
+ if k == "$ref" && v.is_a?(String) && v.start_with?("#/")
56
+ value = v.split("/")[1..-1].inject(node_tree) { |nodes, key| nodes.fetch(key) }
57
+ case value
58
+ when Hash
59
+ obj = {} unless obj.is_a?(Hash)
60
+ obj.merge!(value)
61
+ when Array
62
+ obj = [] unless obj.is_a?(Array)
63
+ obj.concat(value)
64
+ else
65
+ obj = value
66
+ end
67
+ else
68
+ obj = {} unless obj.is_a?(Hash)
69
+ obj[k] = resolve_ref_inner_json(v, node_tree)
70
+ end
71
+ }
72
+ when Array
73
+ node.map { |n| resolve_ref_inner_json(n, node_tree) }
74
+ else
75
+ node
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ module YamlExt
2
+ VERSION = "0.1.0"
3
+ end
data/yaml_ext.gemspec ADDED
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "yaml_ext/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yaml_ext"
8
+ spec.version = YamlExt::VERSION
9
+ spec.authors = ["i2bskn"]
10
+ spec.email = ["i2bskn@gmail.com"]
11
+
12
+ spec.summary = %q{Multiple YAML Loader.}
13
+ spec.description = %q{Multiple YAML Loader.}
14
+ spec.homepage = "https://github.com/i2bskn/yaml_ext"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "minitest"
28
+ spec.add_development_dependency "pry"
29
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaml_ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - i2bskn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
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: Multiple YAML Loader.
70
+ email:
71
+ - i2bskn@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - example/components/index.yml
85
+ - example/components/schemas/Error.yml
86
+ - example/components/schemas/User.yml
87
+ - example/components/schemas/Users.yml
88
+ - example/components/schemas/index.yml
89
+ - example/paths/index.yml
90
+ - example/paths/users/create_users.yml
91
+ - example/paths/users/list_users.yml
92
+ - example/paths/users/show_user_by_id.yml
93
+ - example/schema.yml
94
+ - lib/yaml_ext.rb
95
+ - lib/yaml_ext/version.rb
96
+ - yaml_ext.gemspec
97
+ homepage: https://github.com/i2bskn/yaml_ext
98
+ licenses: []
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubygems_version: 3.0.3
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Multiple YAML Loader.
119
+ test_files: []