api_schema 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f57c171f374e7457aa20f56cfa8f62a0b91110ce
4
- data.tar.gz: d0533b1c70ea503dc795c9cfe8f2754edf8be545
3
+ metadata.gz: 35b851123a21e5f4688f88f145ddf1e2881323a1
4
+ data.tar.gz: e54cb55d27b82897bb098dc915a7b1b70fb81fff
5
5
  SHA512:
6
- metadata.gz: 4dfbe06022ab3f80f1b6ea177bb805b3b1d561c0854a38e0b14d47f299730066a66ab28b05eb892b6062a363e141d40006b0600e7c68e31b6c896677752c0120
7
- data.tar.gz: 90ca9cee0ffe27b68c572f1574eca928d79f2003297b5ed83101debef056b40a61ae18f5838923a470f5263df811842cfb4afb5065cabf41e65161ae21267b7a
6
+ metadata.gz: 6938dfbd1307a7af2da7b0e9aa61b552c250ae07927c516b8e101544fdc83087f5b2285f19a891f6f63cee9b541aa40e4fb785591e3fa91d87ccc720a2949a3d
7
+ data.tar.gz: f2fab1d59adaefbf68ab0099c39729037a0bd6c584c34e3916eb7ca38770fbe8abc5f902a0682855a00bcb9494b905fae9e1b5f1e4e40f1caf19ca25ae075542
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Api Schema [![Build Status](https://travis-ci.org/qonto/api_schema.svg?branch=master)](https://travis-ci.org/qonto/api_schema)
1
+ # Api Schema [![Build Status](https://travis-ci.org/qonto/api_schema.svg?branch=master)](https://travis-ci.org/qonto/api_schema) [![Gem Version](https://badge.fury.io/rb/api_schema.svg)](https://badge.fury.io/rb/api_schema)
2
2
  Provides a framework and DSL for describing APIs and generate swagger-json using minimalist, schema.db-like syntax.
3
3
 
4
4
  <p align="center">
@@ -2,8 +2,6 @@ module ApiSchema
2
2
  class ResourceDefinition
3
3
  include ::Swagger::Blocks::ClassMethods
4
4
 
5
- @neighbors = {}
6
-
7
5
  def initialize(method, base_path, extra_path = nil)
8
6
  @base_path = base_path
9
7
  @extra_path = extra_path
@@ -21,10 +19,6 @@ module ApiSchema
21
19
  :path_params, :query_params, :resp,
22
20
  :errors, :base_path, :extra_path, :full_path
23
21
 
24
- def self.neighbors
25
- @neighbors
26
- end
27
-
28
22
  def name(name)
29
23
  @summary = name
30
24
  end
@@ -77,13 +71,13 @@ module ApiSchema
77
71
  @full_path << "/#{extra_path}" if extra_path
78
72
  end
79
73
 
80
- def build_neighbors
74
+ def build_neighbors(neighbors)
81
75
  generate_full_path
82
- self.class.neighbors[full_path] ||= []
83
- self.class.neighbors[full_path] << self
76
+ neighbors[full_path] ||= []
77
+ neighbors[full_path] << self
84
78
  end
85
79
 
86
- def build
80
+ def build(neighbors)
87
81
  error_model = :error_model
88
82
  error_desc = {
89
83
  '401' => "Unauthorized",
@@ -92,9 +86,8 @@ module ApiSchema
92
86
  '422' => "Unprocessable Entity"
93
87
  }
94
88
  resource = self
95
- resource_class = self.class
96
89
  swagger_path resource.full_path do
97
- resource_class.neighbors[resource.full_path].each do |r|
90
+ neighbors[resource.full_path].each do |r|
98
91
  operation(r.method) do
99
92
  key :summary, r.summary
100
93
  key :description, r.description
@@ -7,35 +7,35 @@ module ApiSchema
7
7
  resource = ResourceDefinition.new(:get, base_path, extra_path)
8
8
  resource.instance_eval(&block)
9
9
  api_version.resources << resource
10
- resource.build_neighbors
10
+ resource.build_neighbors(version_resources)
11
11
  end
12
12
 
13
13
  def post(base_path = default_path, extra_path: nil, &block)
14
14
  resource = ResourceDefinition.new(:post, base_path, extra_path)
15
15
  resource.instance_eval(&block)
16
16
  api_version.resources << resource
17
- resource.build_neighbors
17
+ resource.build_neighbors(version_resources)
18
18
  end
19
19
 
20
20
  def put(base_path = default_path, extra_path: nil, &block)
21
21
  resource = ResourceDefinition.new(:put, base_path, extra_path)
22
22
  resource.instance_eval(&block)
23
23
  api_version.resources << resource
24
- resource.build_neighbors
24
+ resource.build_neighbors(version_resources)
25
25
  end
26
26
 
27
27
  def patch(base_path = default_path, extra_path: nil, &block)
28
28
  resource = ResourceDefinition.new(:patch, base_path, extra_path)
29
29
  resource.instance_eval(&block)
30
30
  api_version.resources << resource
31
- resource.build_neighbors
31
+ resource.build_neighbors(version_resources)
32
32
  end
33
33
 
34
34
  def delete(base_path = default_path, extra_path: nil, &block)
35
35
  resource = ResourceDefinition.new(:delete, base_path, extra_path)
36
36
  resource.instance_eval(&block)
37
37
  api_version.resources << resource
38
- resource.build_neighbors
38
+ resource.build_neighbors(version_resources)
39
39
  end
40
40
 
41
41
  def default_path
@@ -4,22 +4,36 @@ module ApiSchema
4
4
  def inherited(subclass)
5
5
  instance_var = "@api_version"
6
6
  subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
7
+ instance_var_neighbors = "@version_resources"
8
+ subclass.instance_variable_set(instance_var_neighbors, instance_variable_get(instance_var_neighbors))
9
+ instance_var_neighbors = "@version_serializers"
10
+ subclass.instance_variable_set(instance_var_neighbors, instance_variable_get(instance_var_neighbors))
7
11
  end
8
12
 
9
13
  def configure
10
14
  configuration = Configuration.new
11
15
  yield(configuration)
12
16
  @api_version = ApiVersion.new(configuration)
17
+ @version_resources = {}
18
+ @version_serializers = {}
13
19
  end
14
20
 
15
21
  def api_version
16
22
  @api_version
17
23
  end
18
24
 
25
+ def version_resources
26
+ @version_resources
27
+ end
28
+
29
+ def version_serializers
30
+ @version_serializers
31
+ end
32
+
19
33
  def generate_json
20
34
  @api_version.configuration.build
21
- @api_version.serializers.each { |s| s.build }
22
- @api_version.resources.each { |r| r.build }
35
+ @api_version.serializers.each { |s| s.build(version_serializers) }
36
+ @api_version.resources.each { |r| r.build(version_resources) }
23
37
 
24
38
  nodes = [@api_version.configuration] + @api_version.serializers + @api_version.resources
25
39
  ::Swagger::Blocks.build_root_json(nodes)
@@ -2,26 +2,21 @@ module ApiSchema
2
2
  class SerializerDefinition
3
3
  include ::Swagger::Blocks::ClassMethods
4
4
 
5
- @serializers = {}
6
-
7
5
  PriorReference = ::Struct.new(:id, :type, :desc)
8
6
 
9
7
  attr_reader :id, :fields, :references, :parent
10
8
  attr_accessor :type, :name, :description
11
9
 
12
- def initialize(id, type, name=nil, parent_id = nil)
10
+ def initialize(id, type, serializers, name=nil, parent_id = nil)
13
11
  @id = id
14
12
  @type = type
15
13
  @name = name || id
16
- @parent = self.class.serializers[parent_id]
14
+ @parent = serializers[parent_id]
17
15
  @fields = parent&.fields || []
18
16
  @prior_references = parent&.prior_references || []
19
17
  @references = []
20
- self.class.serializers[id] = self
21
- end
22
18
 
23
- def self.serializers
24
- @serializers
19
+ serializers[id] = self
25
20
  end
26
21
 
27
22
  def required_fields
@@ -32,16 +27,16 @@ module ApiSchema
32
27
  @prior_references << PriorReference.new(refernce_id, type, desc)
33
28
  end
34
29
 
35
- def build
36
- build_references
30
+ def build(serializers)
31
+ build_references(serializers)
37
32
  sd = self
38
33
  swagger_schema(id) { schema_for(sd) }
39
34
  end
40
35
 
41
- def build_references
36
+ def build_references(serializers)
42
37
  @prior_references.each do |pr|
43
- raise "Model #{pr.id} is not defined" unless self.class.serializers[pr.id]
44
- reference = self.class.serializers[pr.id].clone
38
+ raise "Model #{pr.id} is not defined" unless serializers[pr.id]
39
+ reference = serializers[pr.id].clone
45
40
  reference.type = pr.type
46
41
  reference.description = pr.desc
47
42
  reference.name = reference.name.to_s.pluralize if reference.type == :array
@@ -4,13 +4,13 @@ module ApiSchema
4
4
  private
5
5
 
6
6
  def serializer(id, structure: :object, name: nil, parent: nil)
7
- serializer = SerializerDefinition.new(id, structure, name, parent)
7
+ serializer = SerializerDefinition.new(id, structure, version_serializers, name, parent)
8
8
  yield serializer if block_given?
9
9
  api_version.serializers << serializer
10
10
  end
11
11
 
12
12
  def array_serializer(id, name: nil, parent: nil)
13
- serializer = SerializerDefinition.new(id, :array, name, parent)
13
+ serializer = SerializerDefinition.new(id, :array, version_serializers, name, parent)
14
14
  yield serializer if block_given?
15
15
  api_version.serializers << serializer
16
16
  end
@@ -1,3 +1,3 @@
1
1
  module ApiSchema
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
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.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Chopey
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-24 00:00:00.000000000 Z
11
+ date: 2017-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  version: '0'
135
135
  requirements: []
136
136
  rubyforge_project:
137
- rubygems_version: 2.6.8
137
+ rubygems_version: 2.6.11
138
138
  signing_key:
139
139
  specification_version: 4
140
140
  summary: api_schema provides a framework and DSL for describing APIs and generate