schema_serializer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE +21 -0
- data/README.md +55 -0
- data/Rakefile +10 -0
- data/bin/console +7 -0
- data/bin/setup +6 -0
- data/lib/schema_serializer.rb +56 -0
- data/lib/schema_serializer/definition.rb +15 -0
- data/lib/schema_serializer/definition/from_openapi3_parser.rb +33 -0
- data/lib/schema_serializer/errors.rb +3 -0
- data/lib/schema_serializer/railtie.rb +9 -0
- data/lib/schema_serializer/schema.rb +39 -0
- data/lib/schema_serializer/serializable.rb +7 -0
- data/lib/schema_serializer/version.rb +3 -0
- data/schema_serializer.gemspec +31 -0
- metadata +145 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4785bf7f03533889d217d20dfcc5df091139e9b4ee28951463840fabcff64e42
|
4
|
+
data.tar.gz: eb160896dc5eb11d2984819098ba26972b8f89e12e0850e43f095b99c5f9ea77
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fecfa5fa50a51d4cc321b994ff37a3c5374b4697cf27828aabc6324d0947e3932f3abfeea1d29887420bb46ed25e79e54aaa6d7f14fbfa87e3bafa2174b75dc4
|
7
|
+
data.tar.gz: 320be8c96cb3bba902c22407fdf24547e53905511fa36ca7904cd9b3fc25dba4f8a4ac958a945658ba361cea92c18c96c8e11664db083b86dce99a5f19837140
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
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,55 @@
|
|
1
|
+
# SchemaSerializer
|
2
|
+
|
3
|
+
SchemaSerializer is provide serialization from schema definition to JSON.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "schema_serializer"
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```
|
22
|
+
SchemaSerializer.definition = {
|
23
|
+
"User" => {
|
24
|
+
"required" => ["id", "name"],
|
25
|
+
"properties" => {
|
26
|
+
"id" => {
|
27
|
+
"type" => "integer",
|
28
|
+
},
|
29
|
+
"name" => {
|
30
|
+
"type" => "string",
|
31
|
+
},
|
32
|
+
"age" => {
|
33
|
+
"type" => "number",
|
34
|
+
"nullable" => true,
|
35
|
+
},
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
User.take.as_json
|
41
|
+
```
|
42
|
+
|
43
|
+
## Development
|
44
|
+
|
45
|
+
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.
|
46
|
+
|
47
|
+
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).
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/i2bskn/schema_serializer.
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require "active_support"
|
2
|
+
require "active_support/core_ext"
|
3
|
+
|
4
|
+
require "schema_serializer/definition"
|
5
|
+
require "schema_serializer/schema"
|
6
|
+
require "schema_serializer/serializable"
|
7
|
+
require "schema_serializer/errors"
|
8
|
+
require "schema_serializer/version"
|
9
|
+
require "schema_serializer/railtie" if defined?(Rails)
|
10
|
+
|
11
|
+
class SchemaSerializer
|
12
|
+
attr_reader :object
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def definition
|
16
|
+
@definition
|
17
|
+
end
|
18
|
+
|
19
|
+
def definition=(define)
|
20
|
+
define = SchemaSerializer::Definition.new(define) unless define.is_a?(SchemaSerializer::Definition)
|
21
|
+
@definition = define
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(object)
|
26
|
+
@object = object
|
27
|
+
end
|
28
|
+
|
29
|
+
def as_json
|
30
|
+
schema.serialize(self)
|
31
|
+
end
|
32
|
+
|
33
|
+
def schema
|
34
|
+
self.class.definition.find(schema_name)
|
35
|
+
end
|
36
|
+
|
37
|
+
def schema_name
|
38
|
+
object.class.to_s
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def method_missing(name, *args, &block)
|
44
|
+
super unless object.respond_to?(name)
|
45
|
+
|
46
|
+
define_singleton_method(name) do |*a, &b|
|
47
|
+
object.public_send(name)
|
48
|
+
end
|
49
|
+
|
50
|
+
send(name, *args, &block)
|
51
|
+
end
|
52
|
+
|
53
|
+
def respond_to_missing?(name, include_private = false)
|
54
|
+
object.respond_to?(name)
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class SchemaSerializer
|
2
|
+
class Definition
|
3
|
+
attr_reader :schemas
|
4
|
+
|
5
|
+
def initialize(hash)
|
6
|
+
@schemas = hash.each_with_object({}) { |(name, hash), obj|
|
7
|
+
obj[name.to_s] = Schema.new(hash)
|
8
|
+
}
|
9
|
+
end
|
10
|
+
|
11
|
+
def find(name)
|
12
|
+
schemas[name] || (raise SchemaNotFound, name)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "openapi3_parser"
|
2
|
+
|
3
|
+
class SchemaSerializer::Definition
|
4
|
+
module FromOpenapi3Parser
|
5
|
+
class << self
|
6
|
+
def build(document)
|
7
|
+
SchemaSerializer::Definition.new(schemas_to_hash(document.components.schemas))
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def schemas_to_hash(schemas)
|
13
|
+
schemas.each_with_object({}) { |(name, node), obj|
|
14
|
+
obj[name.to_s] = node_to_hash(node)
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def node_to_hash(node)
|
19
|
+
obj = { "type" => node.type, "nullable" => node.nullable? }
|
20
|
+
case obj["type"]
|
21
|
+
when "array"
|
22
|
+
obj["items"] = node_to_hash(node.items)
|
23
|
+
when nil
|
24
|
+
obj["required"] = node.required.node_data
|
25
|
+
obj["properties"] = node.properties.each_with_object({}) { |(name, node), obj|
|
26
|
+
obj[name.to_s] = node_to_hash(node)
|
27
|
+
}
|
28
|
+
end
|
29
|
+
obj
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class SchemaSerializer
|
2
|
+
class Schema
|
3
|
+
attr_reader :type, :nullable, :items, :required, :properties
|
4
|
+
|
5
|
+
def initialize(hash = {})
|
6
|
+
@type = hash["type"]
|
7
|
+
@nullable = !!hash["nullable"]
|
8
|
+
|
9
|
+
case type
|
10
|
+
when "array"
|
11
|
+
@items = self.class.new(hash.fetch("items"))
|
12
|
+
when "object", nil
|
13
|
+
@required = hash["required"] || []
|
14
|
+
@properties = hash.fetch("properties").each_with_object({}) { |(column, _hash), obj|
|
15
|
+
obj[column] = self.class.new(_hash)
|
16
|
+
}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def serialize(object)
|
21
|
+
return nil if nullable && object.nil?
|
22
|
+
|
23
|
+
case type
|
24
|
+
when "integer"
|
25
|
+
object.to_i
|
26
|
+
when "number"
|
27
|
+
object.to_f
|
28
|
+
when "string"
|
29
|
+
object.to_s
|
30
|
+
when "array"
|
31
|
+
object.map { |item| items.serialize(item) }
|
32
|
+
else
|
33
|
+
properties.each_with_object({}) { |(column, schema), obj|
|
34
|
+
obj[column] = schema.serialize(object.public_send(column))
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "schema_serializer/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "schema_serializer"
|
8
|
+
spec.version = SchemaSerializer::VERSION
|
9
|
+
spec.authors = ["i2bskn"]
|
10
|
+
spec.email = ["i2bskn@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{SchemaSerializer is provide serialization from schema definition to JSON.}
|
13
|
+
spec.description = %q{SchemaSerializer is provide serialization from schema definition to JSON.}
|
14
|
+
spec.homepage = "https://github.com/i2bskn/schema_serializer"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "activesupport"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
29
|
+
spec.add_development_dependency "pry"
|
30
|
+
spec.add_development_dependency "openapi3_parser"
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: schema_serializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- i2bskn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
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.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
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: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: openapi3_parser
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: SchemaSerializer is provide serialization from schema definition to JSON.
|
98
|
+
email:
|
99
|
+
- i2bskn@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".travis.yml"
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- bin/console
|
111
|
+
- bin/setup
|
112
|
+
- lib/schema_serializer.rb
|
113
|
+
- lib/schema_serializer/definition.rb
|
114
|
+
- lib/schema_serializer/definition/from_openapi3_parser.rb
|
115
|
+
- lib/schema_serializer/errors.rb
|
116
|
+
- lib/schema_serializer/railtie.rb
|
117
|
+
- lib/schema_serializer/schema.rb
|
118
|
+
- lib/schema_serializer/serializable.rb
|
119
|
+
- lib/schema_serializer/version.rb
|
120
|
+
- schema_serializer.gemspec
|
121
|
+
homepage: https://github.com/i2bskn/schema_serializer
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata: {}
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 2.7.6
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: SchemaSerializer is provide serialization from schema definition to JSON.
|
145
|
+
test_files: []
|