apigen 0.0.8 → 0.0.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bfbf6481fb12ed2ad6769d683f3fca04602dd9047f5f08f985754c2f54459db2
4
- data.tar.gz: bf0079b8ff759121cb989d0e356a38807b37e18d3c614cbbd94b312a085044b1
3
+ metadata.gz: 006430db0df68e00b9c45b68417ba25b164037f1d66489ceec9d1cfdf7c79af8
4
+ data.tar.gz: ef50f03eeda9c321c5dd5edc50d73667270d4093dbe5bcfcd20ddd25f97c38e5
5
5
  SHA512:
6
- metadata.gz: 180193f20b5136144f76cef36e8e920cf7cbce38c75bca0f88f728a7639a5a7148cab198ba4add60bf244d7cfa8ccbc6ecd5ddd97aca97c25459c0bd1a6e3d0d
7
- data.tar.gz: ace74867bbcea6a41403a36f32705934f9182160917064e02334d5776ac9e6020e3d967b170b71709ea7ce7ff9bc3b866e59e64b04bc10645e47a61b8efbddb6
6
+ metadata.gz: e6feb9c9da7a4836d7346ef3b4c0d3656be2fc8889b8683c3ad0fe3aa039c141c1a9e751b8bb9117be2fc9d080b0612f6f247173a0440e76daa620c4fc6f3793
7
+ data.tar.gz: 8f9e6ccef15d032e15d38780ceca12b86cf49a947afdf4a431add3fa67c414bcdb6d54aa0f1dcf3a6a9246599e82d9e26a8fbf7df48a6ad4cf2fa314c94170f4
@@ -18,8 +18,8 @@ module Apigen
18
18
 
19
19
  def schema(api, type, description = nil, example = nil)
20
20
  schema = schema_without_description(api, type)
21
- schema['description'] = description unless description.nil?
22
- schema['example'] = example unless example.nil?
21
+ add_description(schema, description)
22
+ add_example(schema, example)
23
23
  schema
24
24
  end
25
25
 
@@ -101,6 +101,14 @@ module Apigen
101
101
  end
102
102
  end
103
103
 
104
+ def add_description(hash, description)
105
+ hash['description'] = description unless description.nil?
106
+ end
107
+
108
+ def add_example(hash, example)
109
+ hash['example'] = example unless example.nil?
110
+ end
111
+
104
112
  def reference_schema(reference_type)
105
113
  { '$ref' => model_ref(reference_type.model_name) }
106
114
  end
@@ -54,7 +54,7 @@ module Apigen
54
54
  'parameters' => parameters,
55
55
  'responses' => responses
56
56
  }
57
- operation['description'] = endpoint.description unless endpoint.description.nil?
57
+ add_description(operation, endpoint.description)
58
58
  operation['requestBody'] = input(api, endpoint.input) if endpoint.input
59
59
  hash[endpoint.path] ||= {}
60
60
  hash[endpoint.path][endpoint.method.to_s] = operation
@@ -69,8 +69,8 @@ module Apigen
69
69
  'required' => true,
70
70
  'schema' => schema(api, property.type)
71
71
  }
72
- parameter['description'] = property.description unless property.description.nil?
73
- parameter['example'] = property.example unless property.example.nil?
72
+ add_description(parameter, property.description)
73
+ add_example(parameter, property.example)
74
74
  parameter
75
75
  end
76
76
 
@@ -81,8 +81,8 @@ module Apigen
81
81
  'required' => property.required?,
82
82
  'schema' => schema(api, property.type)
83
83
  }
84
- parameter['description'] = property.description unless property.description.nil?
85
- parameter['example'] = property.example unless property.example.nil?
84
+ add_description(parameter, property.description)
85
+ add_example(parameter, property.example)
86
86
  parameter
87
87
  end
88
88
 
@@ -95,15 +95,15 @@ module Apigen
95
95
  }
96
96
  }
97
97
  }
98
- parameter['description'] = property.description unless property.description.nil?
99
- parameter['example'] = property.example unless property.example.nil?
98
+ add_description(parameter, property.description)
99
+ add_example(parameter, property.example)
100
100
  parameter
101
101
  end
102
102
 
103
103
  def response(api, output)
104
104
  response = {}
105
- response['description'] = output.description unless output.description.nil?
106
- response['example'] = output.example unless output.example.nil?
105
+ add_description(response, output.description)
106
+ add_example(response, output.example)
107
107
  if output.type != Apigen::PrimaryType.new(:void)
108
108
  response['content'] = {
109
109
  'application/json' => {
@@ -16,18 +16,7 @@ module Apigen
16
16
  # TODO: Allow overriding any of the hardcoded elements.
17
17
  {
18
18
  'swagger' => '2.0',
19
- 'info' => {
20
- 'version' => '1.0.0',
21
- 'title' => 'API',
22
- 'description' => api.description,
23
- 'termsOfService' => '',
24
- 'contact' => {
25
- 'name' => ''
26
- },
27
- 'license' => {
28
- 'name' => ''
29
- }
30
- },
19
+ 'info' => info(api),
31
20
  'host' => 'localhost',
32
21
  'basePath' => '/',
33
22
  'schemes' => %w[
@@ -47,6 +36,21 @@ module Apigen
47
36
 
48
37
  private
49
38
 
39
+ def info(api)
40
+ {
41
+ 'version' => '1.0.0',
42
+ 'title' => 'API',
43
+ 'description' => api.description,
44
+ 'termsOfService' => '',
45
+ 'contact' => {
46
+ 'name' => ''
47
+ },
48
+ 'license' => {
49
+ 'name' => ''
50
+ }
51
+ }
52
+ end
53
+
50
54
  def paths(api)
51
55
  hash = {}
52
56
  api.endpoints.each do |endpoint|
@@ -60,7 +64,7 @@ module Apigen
60
64
  'parameters' => parameters,
61
65
  'responses' => responses
62
66
  }
63
- hash[endpoint.path][endpoint.method.to_s]['description'] = endpoint.description unless endpoint.description.nil?
67
+ add_description(hash[endpoint.path][endpoint.method.to_s], endpoint.description)
64
68
  end
65
69
  hash
66
70
  end
@@ -88,15 +92,15 @@ module Apigen
88
92
  'required' => true,
89
93
  'schema' => schema(api, property.type)
90
94
  }
91
- parameter['description'] = property.description unless property.description.nil?
92
- parameter['example'] = property.example unless property.example.nil?
95
+ add_description(parameter, property.description)
96
+ add_example(parameter, property.example)
93
97
  parameter
94
98
  end
95
99
 
96
100
  def response(api, output)
97
101
  response = {}
98
- response['description'] = output.description unless output.description.nil?
99
- response['example'] = output.example unless output.example.nil?
102
+ add_description(response, output.description)
103
+ add_example(response, output.example)
100
104
  response['schema'] = schema(api, output.type) if output.type != Apigen::PrimaryType.new(:void)
101
105
  [output.status.to_s, response]
102
106
  end
@@ -19,8 +19,12 @@ module Apigen
19
19
 
20
20
  def update_endpoint(name, &block)
21
21
  endpoint = @api.endpoints.find { |e| e.name == name }
22
- raise "No such endpoint #{name}." unless endpoint
23
- raise 'You must pass a block when calling `update_endpoint`.' unless block_given?
22
+ error = if !endpoint
23
+ "No such endpoint #{name}."
24
+ elsif !block_given?
25
+ 'You must pass a block when calling `update_endpoint`.'
26
+ end
27
+ raise error unless error.nil?
24
28
  endpoint.instance_eval(&block)
25
29
  end
26
30
 
@@ -39,8 +43,12 @@ module Apigen
39
43
 
40
44
  def update_model(name, &block)
41
45
  model = @api.models[name]
42
- raise "No such model :#{name}." unless model
43
- raise 'You must pass a block when calling `update_model`.' unless block_given?
46
+ error = if !model
47
+ "No such model :#{name}."
48
+ elsif !block_given?
49
+ 'You must pass a block when calling `update_model`.'
50
+ end
51
+ raise error unless error.nil?
44
52
  model.instance_eval(&block)
45
53
  end
46
54
 
@@ -28,7 +28,8 @@ module Apigen
28
28
  @type = Model.type shape, &block
29
29
  end
30
30
 
31
- def self.type(shape, &block)
31
+ def self.type(shape = nil, &block)
32
+ return type if shape.nil?
32
33
  case shape
33
34
  when :object
34
35
  object = ObjectType.new
@@ -61,8 +62,12 @@ module Apigen
61
62
  end
62
63
 
63
64
  def validate(model_registry)
64
- raise 'One of the models is missing a name.' unless @name
65
- raise "Use `type :model_type [block]` to assign a type to :#{@name}." unless @type
65
+ error = if !@name
66
+ 'One of the models is missing a name.'
67
+ elsif !@type
68
+ "Use `type :model_type [block]` to assign a type to :#{@name}."
69
+ end
70
+ raise error unless error.nil?
66
71
  model_registry.check_type @type
67
72
  end
68
73
 
@@ -25,8 +25,7 @@ module Apigen
25
25
 
26
26
  # rubocop:disable Style/MethodMissingSuper
27
27
  def method_missing(property_name, property_shape, &block)
28
- raise "Property :#{property_name} is defined multiple times." if @properties.key? property_name
29
- raise "Property type must be a symbol, found #{property_shape}." unless property_shape.is_a? Symbol
28
+ ensure_correctness(property_name, property_shape)
30
29
  if property_shape.to_s.end_with? '?'
31
30
  property_shape = property_shape[0..-2].to_sym
32
31
  required = false
@@ -64,6 +63,15 @@ module Apigen
64
63
 
65
64
  private
66
65
 
66
+ def ensure_correctness(property_name, property_shape)
67
+ error = if @properties.key? property_name
68
+ "Property :#{property_name} is defined multiple times."
69
+ elsif !property_shape.is_a? Symbol
70
+ "Property type must be a symbol, found #{property_shape}."
71
+ end
72
+ raise error unless error.nil?
73
+ end
74
+
67
75
  def property_repr(indent, key, property)
68
76
  type_repr = if property.type.respond_to? :repr
69
77
  property.type.repr(indent + ' ')
@@ -24,10 +24,21 @@ module Apigen
24
24
 
25
25
  def validate(model_registry)
26
26
  @mapping.each do |key, value|
27
- raise 'Mapping keys must be model names (use symbols).' unless key.is_a? Symbol
28
- raise 'Mapping values must be strings.' unless value.is_a? String
29
- raise "No such model :#{key} for oneof mapping." unless model_registry.models.key? key
27
+ validate_mapping_item(model_registry, key, value)
30
28
  end
31
29
  end
30
+
31
+ private
32
+
33
+ def validate_mapping_item(model_registry, key, value)
34
+ error = if !(key.is_a? Symbol)
35
+ 'Mapping keys must be model names (use symbols).'
36
+ elsif !(value.is_a? String)
37
+ 'Mapping values must be strings.'
38
+ elsif !(model_registry.models.key? key)
39
+ "No such model :#{key} for oneof mapping."
40
+ end
41
+ raise error unless error.nil?
42
+ end
32
43
  end
33
44
  end
@@ -13,9 +13,13 @@ module Apigen
13
13
  end
14
14
 
15
15
  def model(name, &block)
16
- raise "Model :#{name} is declared twice." if @models.key? name
16
+ error = if @models.key? name
17
+ "Model :#{name} is declared twice."
18
+ elsif !block_given?
19
+ 'You must pass a block when calling `model`.'
20
+ end
21
+ raise error unless error.nil?
17
22
  model = Apigen::Model.new name
18
- raise 'You must pass a block when calling `model`.' unless block_given?
19
23
  model.instance_eval(&block)
20
24
  @models[model.name] = model
21
25
  end
@@ -33,10 +33,14 @@ module Apigen
33
33
  ##
34
34
  # Declares a specific endpoint.
35
35
  def endpoint(name, &block)
36
- raise "Endpoint :#{name} is declared twice." if @endpoints.find { |e| e.name == name }
36
+ error = if @endpoints.find { |e| e.name == name }
37
+ "Endpoint :#{name} is declared twice."
38
+ elsif !block_given?
39
+ 'You must pass a block when calling `endpoint`.'
40
+ end
41
+ raise error unless error.nil?
37
42
  endpoint = Endpoint.new name
38
43
  @endpoints << endpoint
39
- raise 'You must pass a block when calling `endpoint`.' unless block_given?
40
44
  endpoint.instance_eval(&block)
41
45
  end
42
46
 
@@ -128,23 +128,34 @@ module Apigen
128
128
  end
129
129
 
130
130
  def validate_properties
131
- raise 'One of the endpoints is missing a name.' unless @name
132
- raise "Use `method :get/post/put/delete` to set an HTTP method for :#{@name}." unless @method
133
- raise "Use `path \"/some/path\"` to assign a path to :#{@name}." unless @path
131
+ error = if !@name
132
+ 'One of the endpoints is missing a name.'
133
+ elsif !@method
134
+ "Use `method :get/post/put/delete` to set an HTTP method for :#{@name}."
135
+ elsif !@path
136
+ "Use `path \"/some/path\"` to assign a path to :#{@name}."
137
+ end
138
+ raise error unless error.nil?
134
139
  end
135
140
 
136
141
  def validate_input(model_registry)
137
142
  case @method
138
143
  when :put, :post
139
- raise "Use `input { type :typename }` to assign an input type to :#{@name}." unless @input
140
- @input.validate(model_registry)
141
- when :get
142
- raise "Endpoint :#{@name} with method GET cannot accept an input payload." if @input
143
- when :delete
144
- raise "Endpoint :#{@name} with method DELETE cannot accept an input payload." if @input
144
+ validate_required_input(model_registry)
145
+ when :get, :delete
146
+ validate_forbidden_input
145
147
  end
146
148
  end
147
149
 
150
+ def validate_required_input(model_registry)
151
+ raise "Use `input { type :typename }` to assign an input type to :#{@name}." unless @input
152
+ @input.validate(model_registry)
153
+ end
154
+
155
+ def validate_forbidden_input
156
+ raise "Endpoint :#{@name} with method #{@method.to_s.upcase} cannot accept an input payload." if @input
157
+ end
158
+
148
159
  def validate_path_parameters(model_registry)
149
160
  @path_parameters.validate model_registry
150
161
  end
@@ -36,9 +36,14 @@ module Apigen
36
36
  private
37
37
 
38
38
  def validate_properties
39
- raise 'One of the outputs is missing a name.' unless @name
40
- raise "Use `status [code]` to assign a status code to :#{@name}." unless @status
41
- raise "Use `type :typename` to assign a type to :#{@name}." unless @type
39
+ error = if !@name
40
+ 'One of the outputs is missing a name.'
41
+ elsif !@status
42
+ "Use `status [code]` to assign a status code to :#{@name}."
43
+ elsif !@status
44
+ "Use `type :typename` to assign a type to :#{@name}."
45
+ end
46
+ raise error unless error.nil?
42
47
  end
43
48
  end
44
49
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apigen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francois Wouts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-25 00:00:00.000000000 Z
11
+ date: 2018-06-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple DSL to generate OpenAPI and/or JSON Schema definitions in Ruby.
14
14
  email: f@zenc.io