jsapi 0.9.0 → 0.9.2

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: e68467f73e8f1e909a03cd8c50f16674231dc0d070f05a5368b84e0f75dedfe6
4
- data.tar.gz: 6c0b1304cb7192d223f517d5233fd96fa13aad7e0affb6832f313088bc3b3993
3
+ metadata.gz: 59c9ddcd1965cd62ee1b1bbb0e7f066061eaef6a5f8217c69b6d86ee92230b4a
4
+ data.tar.gz: f02a5ff2b0d13ca4c238356b8d11473432ec71b12b40218639335f9f2f45f1da
5
5
  SHA512:
6
- metadata.gz: 45b860d17ffc8f22cfbe4fca26d38e727ff87ddc93a959c6285fd1aaa8eb0de9c5e2a9c516f717f0f6d463b0bfe5c8708b9efa5bab5aa6b08eb642d90ee84d10
7
- data.tar.gz: fe573b4b85e50373bd832f1ec3d4fdd9c11fe57bf42527fe80021d6c84d2271e01161b08cdedcee587a69bd43a28219562d36ed0c127197e4cbfe34e05902aca
6
+ metadata.gz: 4f8f331323ab4d88ba89133f37b0eee8fc6bdb1ab5159f3ce5885b7139d4376e74f6df42ed8232c6ea5c4cb7b373d8ea5a8c866c6833d23f5f1412f6a004ea14
7
+ data.tar.gz: f7061a754b6bbde17046b4cc4cb29eca42daede67f3978f474574c9389d555815e5e11f8f2af021f93985458511790699a64d8cd5fc95b03f421792dcd0dd4a5
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jsapi
4
+ # Holds the \Jsapi configuration.
5
+ class Configuration
6
+ # The path where the API definitions are located relative to +Rails.root+.
7
+ # The default is <code>"app/api_defs"</code>.
8
+ attr_accessor :api_defs_path
9
+
10
+ def initialize # :nodoc:
11
+ @api_defs_path = 'app/api_defs'
12
+ end
13
+
14
+ # Returns the absolute +Pathname+ for +args+ within +api_defs_path+.
15
+ def pathname(*args)
16
+ return unless (root = Rails.root)
17
+
18
+ root.join(*[api_defs_path, args].flatten)
19
+ end
20
+ end
21
+
22
+ class << self
23
+ # The singleton \Jsapi configuration.
24
+ def configuration
25
+ @configuration ||= Configuration.new
26
+ end
27
+ end
28
+ end
@@ -162,7 +162,7 @@ module Jsapi
162
162
  (operation.model || Model::Base).new(
163
163
  Parameters.new(
164
164
  params.except(:action, :controller, :format).permit!,
165
- headers,
165
+ request.headers,
166
166
  operation,
167
167
  definitions,
168
168
  strong: strong
@@ -3,9 +3,26 @@
3
3
  module Jsapi
4
4
  module DSL
5
5
  class Base
6
- def initialize(meta_model, &block)
6
+ def initialize(meta_model, pathname = nil, parent: nil, &block)
7
7
  @meta_model = meta_model
8
+ @pathname = pathname
9
+ @parent = parent
8
10
 
11
+ # Raise an error when pathname is attempted to be imported again
12
+ if pathname && (ancestor = parent)
13
+ while ancestor
14
+ if ancestor.pathname == pathname
15
+ raise Error, "Attempted #{pathname.to_path.inspect} to be imported again"
16
+ end
17
+
18
+ ancestor = ancestor.parent
19
+ end
20
+ end
21
+
22
+ # Evaluate the file to be imported
23
+ instance_eval(pathname.read, pathname.to_path) if pathname
24
+
25
+ # Evaluate block
9
26
  if block
10
27
  if meta_model.reference?
11
28
  raise Error, "reference can't be specified together with a block"
@@ -15,10 +32,30 @@ module Jsapi
15
32
  end
16
33
  end
17
34
 
35
+ # Imports the file named +filename+ relative to +Jsapi.configation.path+.
36
+ def import(filename)
37
+ raise ArgumentError, "file name can't be blank" if filename.blank?
38
+
39
+ pathname = Jsapi.configuration.pathname("#{filename}.rb")
40
+ self.class.new(@meta_model, pathname, parent: self)
41
+ end
42
+
43
+ # Imports the file named +filename+ relative to the current file's path.
44
+ def import_relative(filename)
45
+ raise ArgumentError, "file name can't be blank" if filename.blank?
46
+
47
+ pathname = (@pathname&.parent || Jsapi.configuration.pathname) + "#{filename}.rb"
48
+ self.class.new(@meta_model, pathname, parent: self)
49
+ end
50
+
18
51
  def respond_to_missing?(*args) # :nodoc:
19
52
  keyword?(args.first)
20
53
  end
21
54
 
55
+ protected
56
+
57
+ attr_reader :parent, :pathname
58
+
22
59
  private
23
60
 
24
61
  def define(*args, &block)
@@ -29,13 +29,29 @@ module Jsapi
29
29
  api_definitions { default(type, **keywords, &block) }
30
30
  end
31
31
 
32
- # The API definitions of the current class.
33
- def api_definitions(&block)
34
- @api_definitions ||= Meta::Definitions.new(
35
- owner: self,
36
- parent: superclass.try(:api_definitions)
37
- )
32
+ # Returns the API definitions associated with the current class. Adds additional
33
+ # definitions when any keywords or a block is specified.
34
+ #
35
+ # api_definitions base_path: '/foo' do
36
+ # operation 'bar'
37
+ # end
38
+ def api_definitions(**keywords, &block)
39
+ unless defined? @api_definitions
40
+ @api_definitions = Meta::Definitions.new(
41
+ owner: self,
42
+ parent: superclass.try(:api_definitions)
43
+ )
44
+ if (name = try(:name))
45
+ pathname = Jsapi.configuration.pathname(
46
+ name.deconstantize.split('::').map(&:underscore),
47
+ "#{name.demodulize.delete_suffix('Controller').underscore}.rb"
48
+ )
49
+ Definitions.new(@api_definitions, pathname) if pathname&.file?
50
+ end
51
+ end
52
+ @api_definitions.merge!(keywords) if keywords.any?
38
53
  Definitions.new(@api_definitions, &block) if block
54
+
39
55
  @api_definitions
40
56
  end
41
57
 
@@ -71,6 +87,11 @@ module Jsapi
71
87
  api_definitions { host(arg) }
72
88
  end
73
89
 
90
+ # Imports the file named +filename+ relative to +Jsapi.configation.path+.
91
+ def api_import(filename)
92
+ api_definitions { import(filename) }
93
+ end
94
+
74
95
  # Includes API definitions from +klasses+.
75
96
  def api_include(*klasses)
76
97
  api_definitions { include(*klasses) }
@@ -218,7 +218,7 @@ module Jsapi
218
218
  ##
219
219
  # :method: security_requirement
220
220
  # :args: **keywords, &block
221
- # Adds a security requirement.
221
+ # Specifies a security requirement.
222
222
  #
223
223
  # security_requirement do
224
224
  # scheme 'basic_auth'
@@ -38,6 +38,15 @@ module Jsapi
38
38
  #
39
39
  # deprecated true
40
40
 
41
+ ##
42
+ # :method: external_docs
43
+ # :args: **keywords, &block
44
+ # Specifies the external documentation.
45
+ #
46
+ # external_docs url: 'https://foo.bar/docs'
47
+ #
48
+ # See Meta::Operation#external_docs for further information.
49
+
41
50
  ##
42
51
  # :method: description
43
52
  # :args: arg
@@ -162,6 +171,17 @@ module Jsapi
162
171
  end
163
172
  end
164
173
 
174
+ ##
175
+ # :method: security_requirement
176
+ # :args: **keywords, &block
177
+ # Specifies a security requirement.
178
+ #
179
+ # security_requirement do
180
+ # scheme 'basic_auth'
181
+ # end
182
+ #
183
+ # See Meta::Operation#security_requirements for further information.
184
+
165
185
  ##
166
186
  # :method: summary
167
187
  # :args: arg
@@ -5,6 +5,15 @@ module Jsapi
5
5
  # Used to define a schema.
6
6
  class Schema < Base
7
7
 
8
+ ##
9
+ # :method: additional_properties
10
+ # :args: **keywords, &block
11
+ # Specifies the schema of properties that are not explicity specified.
12
+ #
13
+ # additional_properties type: 'string'
14
+ #
15
+ # See Meta::Schema::Object#additional_properties for further information.
16
+
8
17
  # Includes all of the properties from +schemas+. Each argument must be the name of
9
18
  # a schema defined by ClassMethods#api_schema or Definitions#schema.
10
19
  def all_of(*schemas)
@@ -47,8 +56,9 @@ module Jsapi
47
56
  #
48
57
  # enum %w[foo bar]
49
58
 
50
- ## :method: external_docs
51
- ## :args: **keywords, &blocks
59
+ ##
60
+ # :method: external_docs
61
+ # :args: **keywords, &block
52
62
  # Specifies the external documentation.
53
63
  #
54
64
  # See Meta::Schema::Base#external_docs for further information.
@@ -3,6 +3,7 @@
3
3
  module Jsapi
4
4
  module Meta
5
5
  module Callback
6
+ # Specifies a callback reference.
6
7
  class Reference < Base::Reference
7
8
  # Returns a hash representing the \OpenAPI reference object.
8
9
  def to_openapi(*)
@@ -382,10 +382,14 @@ module Jsapi
382
382
  @objects ||= ancestors.each_with_object({}) do |ancestor, objects|
383
383
  self.class.attribute_names.each do |key|
384
384
  case value = ancestor.send(key)
385
- when Hash
386
- (objects[key] ||= {}).reverse_merge!(value)
387
385
  when Array
388
386
  (objects[key] ||= []).push(*value)
387
+ when Hash
388
+ if (hash = objects[key])
389
+ value.each { |k, v| hash[k] = v unless hash.key?(k) }
390
+ else
391
+ objects[key] = value.dup
392
+ end
389
393
  else
390
394
  objects[key] ||= value
391
395
  end
@@ -3,6 +3,7 @@
3
3
  module Jsapi
4
4
  module Meta
5
5
  module Example
6
+ # Specifies an example reference.
6
7
  class Reference < Base::Reference
7
8
  # Returns a hash representing the \OpenAPI reference object.
8
9
  def to_openapi(*)
@@ -3,6 +3,7 @@
3
3
  module Jsapi
4
4
  module Meta
5
5
  module Header
6
+ # Specifies a header reference.
6
7
  class Reference < Base::Reference
7
8
  # Returns a hash representing the \OpenAPI reference object.
8
9
  def to_openapi(*)
@@ -3,6 +3,7 @@
3
3
  module Jsapi
4
4
  module Meta
5
5
  module Link
6
+ # Specifies a link reference.
6
7
  class Reference < Base::Reference
7
8
  # Returns a hash representing the \OpenAPI reference object.
8
9
  def to_openapi(*)
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Jsapi
4
4
  module Meta
5
- # Defines an API operation.
5
+ # Specifies an API operation.
6
6
  class Operation < Base::Model
7
7
  include OpenAPI::Extensions
8
8
 
@@ -3,10 +3,10 @@
3
3
  module Jsapi
4
4
  module Meta
5
5
  module Parameter
6
- # Defines a parameter.
6
+ # Specifies a parameter.
7
7
  class Model < Base::Model
8
- include ToOpenAPI
9
8
  include OpenAPI::Extensions
9
+ include ToOpenAPI
10
10
 
11
11
  delegate_missing_to :schema
12
12
 
@@ -3,12 +3,14 @@
3
3
  module Jsapi
4
4
  module Meta
5
5
  module Parameter
6
+ # Specifies a parameter reference.
6
7
  class Reference < Base::Reference
7
8
  include ToOpenAPI
8
9
 
9
- # Returns an array of hashes. Each hash represents an \OpenAPI parameter object
10
- # if the type of the referred parameter is <code>"object"</code>. Otherwise the
11
- # array contains a single hash representing the \OpenAPI reference object.
10
+ # Returns an array of hashes. If the type of the referred parameter is
11
+ # <code>"object"</code>, each hash represents an \OpenAPI parameter object.
12
+ # Otherwise the array contains a single hash representing the \OpenAPI
13
+ # reference object.
12
14
  #
13
15
  # Raises a ReferenceError when the reference could not be resolved.
14
16
  def to_openapi_parameters(version, definitions)
@@ -4,6 +4,7 @@ module Jsapi
4
4
  module Meta
5
5
  module Parameter
6
6
  module ToOpenAPI
7
+ # Returns a hash representing the \OpenAPI parameter or reference object.
7
8
  def to_openapi(version, definitions)
8
9
  to_openapi_parameters(version, definitions).first
9
10
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  module Jsapi
4
4
  module Meta
5
+ # Specifies a property
5
6
  class Property < Base::Model
6
7
  delegate_missing_to :schema
7
8
 
@@ -3,7 +3,7 @@
3
3
  module Jsapi
4
4
  module Meta
5
5
  module RequestBody
6
- # Defines a request body.
6
+ # Specifies a request body.
7
7
  class Model < Base::Model
8
8
  include OpenAPI::Extensions
9
9
 
@@ -3,6 +3,7 @@
3
3
  module Jsapi
4
4
  module Meta
5
5
  module RequestBody
6
+ # Specifies a request body reference.
6
7
  class Reference < Base::Reference
7
8
  # Returns a hash representing the \OpenAPI reference object.
8
9
  def to_openapi(*)
@@ -3,7 +3,7 @@
3
3
  module Jsapi
4
4
  module Meta
5
5
  module Response
6
- # Defines a response.
6
+ # Specifies a response.
7
7
  class Model < Base::Model
8
8
  include OpenAPI::Extensions
9
9
 
@@ -3,6 +3,7 @@
3
3
  module Jsapi
4
4
  module Meta
5
5
  module Response
6
+ # Specifies a response reference.
6
7
  class Reference < Base::Reference
7
8
  # Returns a hash representing the \OpenAPI reference object.
8
9
  def to_openapi(version, *)
data/lib/jsapi/version.rb CHANGED
@@ -5,6 +5,6 @@ module Jsapi
5
5
  # NOTE: See https://bundler.io/guides/creating_gem.html
6
6
 
7
7
  # The current GEM version.
8
- VERSION = '0.9.0'
8
+ VERSION = '0.9.2'
9
9
  end
10
10
  end
data/lib/jsapi.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require 'jsapi/invalid_value_helper'
4
4
  require 'jsapi/invalid_value_error'
5
5
  require 'jsapi/invalid_argument_error'
6
-
6
+ require 'jsapi/configuration'
7
7
  require 'jsapi/model'
8
8
  require 'jsapi/meta'
9
9
  require 'jsapi/dsl'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Göller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-12 00:00:00.000000000 Z
11
+ date: 2024-10-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: denis@dmgoeller.de
@@ -17,6 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/jsapi.rb
20
+ - lib/jsapi/configuration.rb
20
21
  - lib/jsapi/controller.rb
21
22
  - lib/jsapi/controller/base.rb
22
23
  - lib/jsapi/controller/error.rb