api_schema 0.1.9.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/api_schema.gemspec +1 -0
- data/lib/api_schema/api_version.rb +85 -1
- data/lib/api_schema/exceptions/undefined_serializers_error.rb +5 -0
- data/lib/api_schema/resource_definition.rb +2 -12
- data/lib/api_schema/root_methods.rb +9 -0
- data/lib/api_schema/serializer_definition.rb +1 -1
- data/lib/api_schema/version.rb +1 -1
- data/lib/api_schema.rb +1 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce61bc89fdc9a8d2447e7f959c2403984663af40
|
4
|
+
data.tar.gz: 04c4b334df2e0ee12b56701119da990e054c1848
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd6866e8eda8b81328f14e72934efc3a5cf7d685b576fd90bb54bd43088a429f8c1b17e0950cbdbd0f874c1051bf1539dc76ffd0f0e4e078fca6778a37d997d8
|
7
|
+
data.tar.gz: 0f5d08c060d02c8803ad3885f7e3a94d8472029e8b69803fa3a229d89e1a12144ea2132fce6400002c038a821cc7761f42fb246ac3590aa1705b7253756eaeb0
|
data/api_schema.gemspec
CHANGED
@@ -1,11 +1,95 @@
|
|
1
|
+
require 'pry'
|
1
2
|
module ApiSchema
|
2
3
|
class ApiVersion
|
3
|
-
attr_accessor :configuration, :resources, :serializers
|
4
|
+
attr_accessor :configuration, :resources, :serializers, :imported_versions, :errors
|
4
5
|
|
5
6
|
def initialize(configuration)
|
6
7
|
@configuration = configuration
|
7
8
|
@resources = []
|
8
9
|
@serializers = []
|
10
|
+
@imported_versions = []
|
11
|
+
@errors = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def check_consistency
|
16
|
+
check_serializers_consistency
|
17
|
+
check_resources_consistency
|
18
|
+
|
19
|
+
raise UndefinedSerializersError, error_message unless errors.empty?
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def check_serializers_consistency
|
25
|
+
serializers.each do |serializer|
|
26
|
+
serializer.prior_references.each do |reference|
|
27
|
+
add_errors(:undefined_reference, reference) unless find_or_import(reference.id)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def check_resources_consistency
|
33
|
+
resources.each do |resource|
|
34
|
+
add_errors(:undefined_body, resource) unless find_or_import(resource.body_param)
|
35
|
+
add_errors(:undefined_response, resource) unless find_or_import(resource.resp.model)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def find_or_import(serializer_id)
|
40
|
+
return true unless serializer_id
|
41
|
+
return true if find_in_version(self, serializer_id)
|
42
|
+
return true if find_in_another_version(serializer_id)
|
43
|
+
end
|
44
|
+
|
45
|
+
def find_in_another_version(serializer_id)
|
46
|
+
imported_versions.each do |import|
|
47
|
+
version = imported_version(import)
|
48
|
+
serializer = find_in_version(version, serializer_id)
|
49
|
+
return serializers << serializer if serializer
|
50
|
+
end
|
51
|
+
|
52
|
+
false
|
53
|
+
end
|
54
|
+
|
55
|
+
def find_in_version(version, serializer_id)
|
56
|
+
version.serializers.detect { |s| s.id == serializer_id }
|
57
|
+
end
|
58
|
+
|
59
|
+
def imported_version(import)
|
60
|
+
"#{import.name.capitalize}::#{import.version.capitalize}::BaseDocs".constantize.api_version
|
61
|
+
end
|
62
|
+
|
63
|
+
def add_errors(key, object)
|
64
|
+
errors[key] ||= []
|
65
|
+
errors[key] << object
|
66
|
+
end
|
67
|
+
|
68
|
+
def error_message
|
69
|
+
message = errors.reduce([]) do |memo, (key, errors)|
|
70
|
+
case key
|
71
|
+
when :undefined_body
|
72
|
+
memo += errors.map(&self.method(:body_error))
|
73
|
+
when :undefined_response
|
74
|
+
memo += errors.map(&self.method(:response_error))
|
75
|
+
when :undefined_reference
|
76
|
+
memo += errors.map(&self.method(:reference_error))
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
message.join(" \n")
|
81
|
+
end
|
82
|
+
|
83
|
+
def body_error(object)
|
84
|
+
"There is no '#{object.body_param}' body parameter defined for #{object.base_path} resource. Please define it with 'request_body' method"
|
85
|
+
end
|
86
|
+
|
87
|
+
def response_error(object)
|
88
|
+
"There is no '#{object.resp.model}' response defined for '#{object.summary}' resource. Please define it with 'serializer' method"
|
89
|
+
end
|
90
|
+
|
91
|
+
def reference_error(object)
|
92
|
+
"Reference #{object.id} is not defined"
|
9
93
|
end
|
10
94
|
end
|
11
95
|
end
|
@@ -68,16 +68,6 @@ module ApiSchema
|
|
68
68
|
!!body_param
|
69
69
|
end
|
70
70
|
|
71
|
-
def body_defined?
|
72
|
-
return true if api_version.serializers.detect {|s| s.id == body_param }
|
73
|
-
raise "There is no '#{body_param}' body parameter defined for #{full_path} resource. Please define it with 'request_body' method"
|
74
|
-
end
|
75
|
-
|
76
|
-
def response_defined?
|
77
|
-
return true if resp.model.nil? || api_version.serializers.detect {|s| s.id == resp.model }
|
78
|
-
raise "There is no '#{resp.model}' response defined for ''#{summary} resource'. Please define it with 'serializer' method"
|
79
|
-
end
|
80
|
-
|
81
71
|
def with_errors?
|
82
72
|
!errors.empty?
|
83
73
|
end
|
@@ -112,7 +102,7 @@ module ApiSchema
|
|
112
102
|
security do
|
113
103
|
key :authorization, []
|
114
104
|
end
|
115
|
-
body_param(r.body_param) if r.with_body?
|
105
|
+
body_param(r.body_param) if r.with_body?
|
116
106
|
|
117
107
|
r.header_params.each do |p|
|
118
108
|
header_param(p.name, p.type, p.required)
|
@@ -124,7 +114,7 @@ module ApiSchema
|
|
124
114
|
query_param(p.name, p.type, p.required)
|
125
115
|
end
|
126
116
|
|
127
|
-
success_response(r.resp.code, r.resp.model, r.resp.fields)
|
117
|
+
success_response(r.resp.code, r.resp.model, r.resp.fields)
|
128
118
|
error_responses(error_model, error_desc, *r.errors) if r.with_errors?
|
129
119
|
end
|
130
120
|
end
|
@@ -18,6 +18,11 @@ module ApiSchema
|
|
18
18
|
@version_serializers = {}
|
19
19
|
end
|
20
20
|
|
21
|
+
def import_serializers_from(name, version)
|
22
|
+
@api_version.imported_versions << OpenStruct.new(name: name,
|
23
|
+
version: version)
|
24
|
+
end
|
25
|
+
|
21
26
|
def api_version
|
22
27
|
@api_version
|
23
28
|
end
|
@@ -32,6 +37,10 @@ module ApiSchema
|
|
32
37
|
|
33
38
|
def generate_json
|
34
39
|
@api_version.configuration.build
|
40
|
+
@api_version.serializers.each { |s| s.build_references(version_serializers) }
|
41
|
+
|
42
|
+
@api_version.check_consistency
|
43
|
+
|
35
44
|
@api_version.serializers.each { |s| s.build(version_serializers) }
|
36
45
|
@api_version.resources.each { |r| r.build(version_resources) }
|
37
46
|
|
@@ -5,7 +5,7 @@ module ApiSchema
|
|
5
5
|
PriorReference = ::Struct.new(:id, :type, :desc)
|
6
6
|
|
7
7
|
attr_reader :id, :fields, :references, :parent
|
8
|
-
attr_accessor :type, :name, :description
|
8
|
+
attr_accessor :type, :name, :description, :prior_references
|
9
9
|
|
10
10
|
def initialize(id, type, serializers, name=nil, parent_id = nil)
|
11
11
|
@id = id
|
data/lib/api_schema/version.rb
CHANGED
data/lib/api_schema.rb
CHANGED
@@ -14,6 +14,7 @@ require "api_schema/serializer_definition"
|
|
14
14
|
require "api_schema/serializer_methods"
|
15
15
|
require "api_schema/resource_definition"
|
16
16
|
require "api_schema/resource_methods"
|
17
|
+
require "api_schema/exceptions/undefined_serializers_error.rb"
|
17
18
|
require "api_schema/version"
|
18
19
|
|
19
20
|
module ApiSchema
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitry Chopey
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.11.3
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.11.3
|
83
97
|
description: |-
|
84
98
|
api_schema provides a framework and DSL for describing APIs
|
85
99
|
and generate swagger json using minimalistic, schema.db-like syntax.
|
@@ -103,6 +117,7 @@ files:
|
|
103
117
|
- lib/api_schema.rb
|
104
118
|
- lib/api_schema/api_version.rb
|
105
119
|
- lib/api_schema/configuration.rb
|
120
|
+
- lib/api_schema/exceptions/undefined_serializers_error.rb
|
106
121
|
- lib/api_schema/field.rb
|
107
122
|
- lib/api_schema/patches/operation_node.rb
|
108
123
|
- lib/api_schema/patches/property_node.rb
|