genio-parser 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/genio/parser/format/json_schema.rb +89 -14
- data/lib/genio/parser/version.rb +1 -1
- metadata +28 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10f0d02b905c7e01cd0a4e0fb09a30d61478fa61
|
4
|
+
data.tar.gz: 9e1f7659da5584fc553caf34a419112a7b28a362
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0df4ebe74f1159f7ee00eeeb2db23458b8bc035c50d8f7d6324efdc3497ed043f1ae5460d9ddbe2fce94ae2977db0e77333b39a05899ca17a542c88df3023d75
|
7
|
+
data.tar.gz: 173a8fe70e2b25313e3fec7dd4bd7d62586e52f79b3954c83eb21d3a3f750d6bc0c3df69c53689f1ad44622a9e1b05b81758680aba9fed03354672384020eef1
|
@@ -1,11 +1,14 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
|
+
|
3
4
|
module Genio
|
4
5
|
module Parser
|
5
6
|
module Format
|
6
7
|
class JsonSchema < Base
|
7
8
|
include Logging
|
8
9
|
|
10
|
+
attr_accessor :current_schema
|
11
|
+
|
9
12
|
# Load schema
|
10
13
|
# == Example
|
11
14
|
# schema.load("path/to/json_schema.json")
|
@@ -13,12 +16,61 @@ module Genio
|
|
13
16
|
def load(filename, force = false)
|
14
17
|
if data_types[filename] || (!force and data_types[class_name(filename)])
|
15
18
|
class_name(filename)
|
19
|
+
elsif filename =~ /#./ and self.current_schema
|
20
|
+
inline_schema(filename)
|
16
21
|
elsif files[filename]
|
17
22
|
files[filename]
|
18
23
|
else
|
19
24
|
files[filename] = class_name(filename)
|
20
25
|
parse_file(filename)
|
21
26
|
end
|
27
|
+
rescue Errno::ENOENT => error
|
28
|
+
if force and data_types[class_name(filename)]
|
29
|
+
logger.error error.message
|
30
|
+
class_name(filename)
|
31
|
+
else
|
32
|
+
raise error
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def inline_schema(filename)
|
37
|
+
file, names = filename.split("#", 2)
|
38
|
+
names = names.split("/").delete_if(&:empty?)
|
39
|
+
|
40
|
+
schema = self.current_schema
|
41
|
+
if file.present?
|
42
|
+
read_file(file) do |data|
|
43
|
+
schema = JSON.parse(data, :object_class => Types::Base, :max_nesting => 100)
|
44
|
+
store_schema(schema) do
|
45
|
+
get_inline_schema_klass(names, schema, filename)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
else
|
49
|
+
get_inline_schema_klass(names, schema, filename)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_inline_schema_klass(names, schema, filename)
|
54
|
+
names.each do |name|
|
55
|
+
schema = schema[name] if schema
|
56
|
+
end
|
57
|
+
|
58
|
+
if schema
|
59
|
+
klass = class_name(class_name(names.join("_")))
|
60
|
+
data_types[klass] = {}
|
61
|
+
data_types[klass] = parse_object(schema)
|
62
|
+
klass
|
63
|
+
else
|
64
|
+
raise "Unable to find schema #{filename}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def store_schema(schema)
|
69
|
+
backup = self.current_schema
|
70
|
+
self.current_schema = schema
|
71
|
+
yield
|
72
|
+
ensure
|
73
|
+
self.current_schema = backup
|
22
74
|
end
|
23
75
|
|
24
76
|
# Parse Given schema file and return class name
|
@@ -26,11 +78,13 @@ module Genio
|
|
26
78
|
klass = class_name(filename)
|
27
79
|
read_file(filename) do |data|
|
28
80
|
data = JSON.parse(data, :object_class => Types::Base, :max_nesting => 100)
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
81
|
+
store_schema(data) do
|
82
|
+
if data.resources # discovery format
|
83
|
+
parse_resource(data)
|
84
|
+
else
|
85
|
+
data_types[klass] = {}
|
86
|
+
data_types[klass] = parse_object(data)
|
87
|
+
end
|
34
88
|
end
|
35
89
|
end
|
36
90
|
klass
|
@@ -43,23 +97,39 @@ module Genio
|
|
43
97
|
end
|
44
98
|
|
45
99
|
properties = Types::Base.new
|
100
|
+
required_properties = data.required.is_a?(Array) ? data.required : []
|
46
101
|
|
47
102
|
# Parse each properties
|
48
103
|
if data.properties
|
49
104
|
data.properties.each do |name, options|
|
50
105
|
properties[name] = parse_property(name, options)
|
106
|
+
properties[name].required = true if required_properties.include? name
|
51
107
|
end
|
52
108
|
elsif data.type.is_a?(Array)
|
53
109
|
data.type.each do |object|
|
54
|
-
properties.merge!(parse_object(object).properties)
|
110
|
+
properties.merge!(parse_object(object).properties) if object.is_a? Hash
|
55
111
|
end
|
56
112
|
end
|
57
113
|
|
58
114
|
# Load extends class.
|
59
|
-
|
60
|
-
data.extends
|
61
|
-
|
62
|
-
data.extends
|
115
|
+
data.extends =
|
116
|
+
if data.extends.is_a? String
|
117
|
+
self.load(data.extends)
|
118
|
+
elsif data.extends.is_a? Hash
|
119
|
+
if data.extends["$ref"]
|
120
|
+
self.load(data.extends["$ref"])
|
121
|
+
else
|
122
|
+
properties.merge!(parse_object(data.extends).properties)
|
123
|
+
nil
|
124
|
+
end
|
125
|
+
else
|
126
|
+
data.extends = nil
|
127
|
+
end
|
128
|
+
|
129
|
+
["oneOf", "anyOf", "allOf"].each do |name|
|
130
|
+
data[name] = data[name].map do |schema|
|
131
|
+
parse_property(name, schema)
|
132
|
+
end if data[name].is_a? Array
|
63
133
|
end
|
64
134
|
|
65
135
|
# Parse array type
|
@@ -87,15 +157,17 @@ module Genio
|
|
87
157
|
data_types[klass_name] = parse_object(data)
|
88
158
|
klass_name
|
89
159
|
elsif data.type.is_a? Array
|
90
|
-
data.
|
91
|
-
|
160
|
+
data.oneOf = data.type.map do |type|
|
161
|
+
type = Types::Base.new( "type" => type ) unless type.is_a? Hash
|
162
|
+
parse_object(type) || Types::Base.new( "type" => "self" )
|
92
163
|
end
|
93
164
|
"object"
|
94
165
|
elsif data.items # Parse array value type
|
95
166
|
array_property = parse_property(name, data.items)
|
96
167
|
array_property.type
|
97
168
|
else
|
98
|
-
data.
|
169
|
+
data.merge!(parse_object(data))
|
170
|
+
data.type || "object"
|
99
171
|
end
|
100
172
|
Types::Property.new(data)
|
101
173
|
rescue => error
|
@@ -175,7 +247,10 @@ module Genio
|
|
175
247
|
# class_name("credit-card") # return "CreditCard"
|
176
248
|
# class_name("/path/to/payment.json") # return "Payment"
|
177
249
|
def class_name(name)
|
178
|
-
|
250
|
+
name, anchor = name.to_s.split("#", 2)
|
251
|
+
name = File.basename(name, ".json")
|
252
|
+
name = name + "_" + anchor if anchor.present?
|
253
|
+
name.gsub(/[^\w]/, "_").camelcase
|
179
254
|
end
|
180
255
|
|
181
256
|
# Fix file name format
|
data/lib/genio/parser/version.rb
CHANGED
metadata
CHANGED
@@ -1,125 +1,125 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: genio-parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- PayPal
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: nokogiri
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.6'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.6'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '1.3'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '1.3'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: vcr
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: webmock
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- -
|
101
|
+
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: '0'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- -
|
108
|
+
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: simplecov
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- -
|
115
|
+
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '0'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- -
|
122
|
+
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
125
|
description: Parse schema and generates common object used by genio
|
@@ -129,21 +129,21 @@ executables: []
|
|
129
129
|
extensions: []
|
130
130
|
extra_rdoc_files: []
|
131
131
|
files:
|
132
|
+
- LICENSE.txt
|
133
|
+
- README.md
|
134
|
+
- data/wadl2meta.xsl
|
135
|
+
- data/wsdl2meta.xsl
|
136
|
+
- lib/genio-parser.rb
|
137
|
+
- lib/genio/parser.rb
|
132
138
|
- lib/genio/parser/format/base.rb
|
133
139
|
- lib/genio/parser/format/iodocs.rb
|
134
140
|
- lib/genio/parser/format/json_schema.rb
|
135
141
|
- lib/genio/parser/format/wadl.rb
|
136
142
|
- lib/genio/parser/format/wsdl.rb
|
137
143
|
- lib/genio/parser/logging.rb
|
138
|
-
- lib/genio/parser/types/base.rb
|
139
144
|
- lib/genio/parser/types.rb
|
145
|
+
- lib/genio/parser/types/base.rb
|
140
146
|
- lib/genio/parser/version.rb
|
141
|
-
- lib/genio/parser.rb
|
142
|
-
- lib/genio-parser.rb
|
143
|
-
- data/wadl2meta.xsl
|
144
|
-
- data/wsdl2meta.xsl
|
145
|
-
- README.md
|
146
|
-
- LICENSE.txt
|
147
147
|
homepage: https://github.com/paypal/genio-parser
|
148
148
|
licenses:
|
149
149
|
- Apache License, Version 2.0
|
@@ -154,17 +154,17 @@ require_paths:
|
|
154
154
|
- lib
|
155
155
|
required_ruby_version: !ruby/object:Gem::Requirement
|
156
156
|
requirements:
|
157
|
-
- -
|
157
|
+
- - ">="
|
158
158
|
- !ruby/object:Gem::Version
|
159
159
|
version: '0'
|
160
160
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
161
161
|
requirements:
|
162
|
-
- -
|
162
|
+
- - ">="
|
163
163
|
- !ruby/object:Gem::Version
|
164
164
|
version: '0'
|
165
165
|
requirements: []
|
166
166
|
rubyforge_project:
|
167
|
-
rubygems_version: 2.
|
167
|
+
rubygems_version: 2.4.1
|
168
168
|
signing_key:
|
169
169
|
specification_version: 4
|
170
170
|
summary: Parse schema and generates common object used by genio
|