jsapi 0.4.0 → 0.5.0

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: 5740302f08b361b1f70f50844a1c3858e8bf2d29b781ea245ec201c2290fb3f4
4
- data.tar.gz: 27f401b21f5fece8a1dd675ab8597a7822d101a3d738a6f5ef76e5a8969dca8d
3
+ metadata.gz: 162f63ee5ef06ba76a2acb100e92928144533e8d2486175b95cf4f8a38d6f93d
4
+ data.tar.gz: bf6579a80a1bdc8eb2ab3ca7921f42f1a68466508425568e3c5b984e7e1b34b8
5
5
  SHA512:
6
- metadata.gz: fe84b39402a26fa951058ba59fab7cc20e0e0321c74826b672bd65b87a4975d68f37517bfa54f62549f2287c3221702d4d338e312ae01551481f5a529a865f40
7
- data.tar.gz: 476347dfb1a27d43dc42835d87a845a75806a7a6c6f721ad9e372d53d435650c685b9cf7c8cb70f34718dffc675e1536475851b841e549b50789f9f08965aa33
6
+ metadata.gz: 71c6b38cb010530b4b6f4cfebee131981c2874114bf663287739452f709863e64e073b358dded464ee86d2d14ce68439170e15c709667d1098a804f874728cd3
7
+ data.tar.gz: 8e60c31c91af85df15c2bb9c2c0a17fa67599df3621c90603bbe624615a6749e3a909b1a8e91cb5ea5a7aab8d75ee03adaf32ea28e4abd95d30bc7fd1cadd754
@@ -2,19 +2,26 @@
2
2
 
3
3
  module Jsapi
4
4
  module Controller
5
- # Used by +api_operation!+ to produce an error response.
5
+ # Used by Methods#api_operation! to produce an error response.
6
6
  class ErrorResult
7
+ delegate_missing_to :@exception
7
8
 
8
9
  # The HTTP status code of the error response to be produced.
9
10
  attr_reader :status
10
11
 
11
- delegate :message, :to_s, to: :@exception
12
-
13
12
  # Creates a new instance to produce an error response with the given HTTP status code.
14
- def initialize(exception, status:)
13
+ def initialize(exception, status: nil)
15
14
  @exception = exception
16
15
  @status = status
17
16
  end
17
+
18
+ # Returns the string representation of the exception encountered to render this string
19
+ # when the response type is +string+, for example:
20
+ #
21
+ # response 500, type: 'string'
22
+ def to_s
23
+ @exception.to_s
24
+ end
18
25
  end
19
26
  end
20
27
  end
@@ -45,7 +45,7 @@ module Jsapi
45
45
  [
46
46
  validate_attributes(errors),
47
47
  !@strong || validate_parameters(
48
- @params.except(:controller, :action),
48
+ @params.except(:controller, :action, :format),
49
49
  attributes,
50
50
  errors
51
51
  )
@@ -2,8 +2,10 @@
2
2
 
3
3
  module Jsapi
4
4
  module Controller
5
- # Raised by Methods#api_operation! if the request parameters are invalid.
5
+ # Raised by Methods#api_operation! when the request parameters are invalid.
6
6
  class ParametersInvalid < StandardError
7
+
8
+ # The parameters.
7
9
  attr_reader :params
8
10
 
9
11
  def initialize(params)
@@ -11,13 +13,14 @@ module Jsapi
11
13
  super('')
12
14
  end
13
15
 
14
- # Overrides <code>StandardError#message</code> to lazily generate the error message.
16
+ # Returns the errors encountered.
17
+ def errors
18
+ @params.errors.errors
19
+ end
20
+
21
+ # Overrides <code>Exception#message</code> to lazily generate the error message.
15
22
  def message
16
- "#{
17
- @params.errors.full_messages.map do |message|
18
- message.delete_suffix('.')
19
- end.join('. ')
20
- }."
23
+ "#{@params.errors.full_messages.map { |m| m.delete_suffix('.') }.join('. ')}."
21
24
  end
22
25
  end
23
26
  end
@@ -81,7 +81,11 @@ module Jsapi
81
81
  # Serialize properties
82
82
  properties = schema.resolve_properties(:read, @definitions).transform_values do |property|
83
83
  serialize(
84
- object.public_send(property.source || property.name.underscore),
84
+ if (method_chain = property.source).present?
85
+ method_chain.call(object)
86
+ else
87
+ object.public_send(property.name.underscore)
88
+ end,
85
89
  property.schema.resolve(@definitions),
86
90
  path.nil? ? property.name : "#{path}.#{property.name}"
87
91
  )
@@ -89,7 +93,7 @@ module Jsapi
89
93
  if (additional_properties = schema.additional_properties&.resolve(@definitions))
90
94
  additional_properties_schema = additional_properties.schema.resolve(@definitions)
91
95
 
92
- object.public_send(additional_properties.source)&.each do |key, value|
96
+ additional_properties.source.call(object)&.each do |key, value|
93
97
  # Don't replace the property with the same key
94
98
  next if properties.key?(key = key.to_s)
95
99
 
@@ -54,7 +54,8 @@ module Jsapi
54
54
 
55
55
  # hash value reader
56
56
  define_method(singular_name) do |key = nil|
57
- send(name)&.[](key_type_caster.cast(key) || default_key)
57
+ key = default_key if key.to_s.empty?
58
+ send(name)&.[](key_type_caster.cast(key))
58
59
  end
59
60
 
60
61
  if writer
@@ -66,9 +67,10 @@ module Jsapi
66
67
  key = default_key
67
68
  value = key_or_value
68
69
  else
69
- key = key_or_value || default_key
70
+ key = key_or_value
71
+ key = default_key if key.to_s.empty?
70
72
  end
71
- raise ArgumentError, "key can't be blank" if key.blank?
73
+ raise ArgumentError, "key can't be blank" if key.to_s.empty?
72
74
 
73
75
  casted_key = key_type_caster.cast(key)
74
76
  casted_value = value_type_caster.cast(value)
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jsapi
4
+ module Meta
5
+ class MethodChain
6
+ # The methods to be called in chain.
7
+ attr_reader :methods
8
+
9
+ def initialize(methods)
10
+ @methods = Array.wrap(methods).flat_map do |method|
11
+ next method if method.is_a?(Symbol)
12
+
13
+ method.to_s.split('.')
14
+ end.map(&:to_sym)
15
+ end
16
+
17
+ def inspect # :nodoc:
18
+ "#<#{self.class.name} #{methods.inspect}>"
19
+ end
20
+
21
+ # Calls the chained methods on +object+.
22
+ def call(object)
23
+ return if methods.blank?
24
+
25
+ methods.each do |method|
26
+ object = object.public_send(method)
27
+ end
28
+ object
29
+ end
30
+ end
31
+ end
32
+ end
@@ -7,6 +7,8 @@ module Jsapi
7
7
  class Model < Base
8
8
  include Extensions
9
9
 
10
+ delegate_missing_to :schema
11
+
10
12
  ##
11
13
  # :attr: deprecated
12
14
  # Specifies whether or not the header is deprecated.
@@ -27,8 +29,6 @@ module Jsapi
27
29
  # The Schema of the header.
28
30
  attribute :schema, writer: false
29
31
 
30
- delegate_missing_to :schema
31
-
32
32
  def initialize(keywords = {})
33
33
  keywords = keywords.dup
34
34
  super(keywords.extract!(:deprecated, :description, :examples))
@@ -6,6 +6,8 @@ module Jsapi
6
6
  class Model < Base
7
7
  include OpenAPI::Extensions
8
8
 
9
+ delegate_missing_to :schema
10
+
9
11
  ##
10
12
  # :attr: deprecated
11
13
  # Specifies whether or not the parameter is deprecated.
@@ -41,8 +43,6 @@ module Jsapi
41
43
  # The Schema of the parameter.
42
44
  attribute :schema, writer: false
43
45
 
44
- delegate_missing_to :schema
45
-
46
46
  # Creates a new parameter.
47
47
  #
48
48
  # Raises an +ArgumentError+ if +name+ is blank.
@@ -3,6 +3,8 @@
3
3
  module Jsapi
4
4
  module Meta
5
5
  class Property < Base
6
+ delegate_missing_to :schema
7
+
6
8
  ##
7
9
  # :attr_reader: name
8
10
  # The name of the property.
@@ -19,15 +21,13 @@ module Jsapi
19
21
 
20
22
  ##
21
23
  # :attr: source
22
- # The alternative method to read a property value when serializing an object.
23
- attribute :source, Symbol
24
+ # The MethodChain to be called when reading a property value.
25
+ attribute :source, MethodChain
24
26
 
25
27
  ##
26
28
  # :attr: write_only
27
29
  attribute :write_only, values: [true, false]
28
30
 
29
- delegate_missing_to :schema
30
-
31
31
  # Creates a new property.
32
32
  #
33
33
  # Raises an +ArgumentError+ if +name+ is blank.
@@ -6,6 +6,8 @@ module Jsapi
6
6
  class Model < Base
7
7
  include OpenAPI::Extensions
8
8
 
9
+ delegate_missing_to :schema
10
+
9
11
  ##
10
12
  # :attr: description
11
13
  # The optional description of the request body.
@@ -21,8 +23,6 @@ module Jsapi
21
23
  # The Schema of the request body.
22
24
  attribute :schema, writer: false
23
25
 
24
- delegate_missing_to :schema
25
-
26
26
  def initialize(keywords = {})
27
27
  keywords = keywords.dup
28
28
  super(keywords.extract!(:description, :examples))
@@ -6,6 +6,8 @@ module Jsapi
6
6
  class Model < Base
7
7
  include OpenAPI::Extensions
8
8
 
9
+ delegate_missing_to :schema
10
+
9
11
  ##
10
12
  # :attr: description
11
13
  # The optional description of the response.
@@ -36,8 +38,6 @@ module Jsapi
36
38
  # The Schema of the response.
37
39
  attribute :schema, writer: false
38
40
 
39
- delegate_missing_to :schema
40
-
41
41
  def initialize(keywords = {})
42
42
  keywords = keywords.dup
43
43
  super(keywords.extract!(:description, :examples, :locale))
@@ -4,18 +4,20 @@ module Jsapi
4
4
  module Meta
5
5
  module Schema
6
6
  class AdditionalProperties < Meta::Base
7
- #
7
+ DEFAULT_METHOD_CHAIN = MethodChain.new(:additional_properties) # :nodoc:
8
+
9
+ delegate_missing_to :schema
10
+
11
+ ##
8
12
  # :attr: schema
9
13
  # The Schema of additional properties.
10
14
  attribute :schema, Schema, writer: false
11
15
 
12
16
  ##
13
17
  # :attr: source
14
- # The method to read additional properties when serializing an object.
18
+ # The MethodChain to be called when reading additional properties.
15
19
  # The default method is +additional_properties+.
16
- attribute :source, Symbol, default: :additional_properties
17
-
18
- delegate_missing_to :schema
20
+ attribute :source, MethodChain, default: DEFAULT_METHOD_CHAIN
19
21
 
20
22
  def initialize(keywords = {})
21
23
  keywords = keywords.dup
@@ -5,11 +5,11 @@ module Jsapi
5
5
  module Schema
6
6
  # Used by Reference to delegate method calls to the referred schema.
7
7
  class Delegator
8
+ delegate_missing_to :@schema
9
+
8
10
  # The level of Existence.
9
11
  attr_reader :existence
10
12
 
11
- delegate_missing_to :@schema
12
-
13
13
  def initialize(schema, existence)
14
14
  @schema = schema
15
15
  @existence = existence
@@ -6,7 +6,7 @@ module Jsapi
6
6
  class Discriminator < Meta::Base
7
7
  ##
8
8
  # :attr: mappings
9
- attribute :mappings, { String => String }
9
+ attribute :mappings, { Object => String }
10
10
 
11
11
  ##
12
12
  # :attr: property_name
@@ -27,7 +27,7 @@ module Jsapi
27
27
 
28
28
  {
29
29
  propertyName: property_name,
30
- mapping: mappings
30
+ mapping: mappings&.transform_keys(&:to_s)
31
31
  }.compact
32
32
  end
33
33
  end
data/lib/jsapi/meta.rb CHANGED
@@ -7,6 +7,7 @@ require_relative 'meta/base'
7
7
  require_relative 'meta/base_reference'
8
8
  require_relative 'meta/openapi'
9
9
  require_relative 'meta/existence'
10
+ require_relative 'meta/method_chain'
10
11
  require_relative 'meta/property'
11
12
  require_relative 'meta/schema'
12
13
  require_relative 'meta/request_body'
@@ -4,10 +4,10 @@ module Jsapi
4
4
  module Model
5
5
  # Wraps an error related to a nested model.
6
6
  class NestedError
7
- attr_reader :attribute, :error
8
-
9
7
  delegate_missing_to :error
10
8
 
9
+ attr_reader :attribute, :error
10
+
11
11
  def initialize(attribute, error)
12
12
  @attribute = attribute
13
13
  @error = error
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.4.0'
8
+ VERSION = '0.5.0'
9
9
  end
10
10
  end
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.4.0
4
+ version: 0.5.0
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-08-21 00:00:00.000000000 Z
11
+ date: 2024-08-31 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Jsapi can be used to read requests, produce responses and create OpenAPI
14
14
  documents
@@ -58,6 +58,7 @@ files:
58
58
  - lib/jsapi/meta/definitions.rb
59
59
  - lib/jsapi/meta/existence.rb
60
60
  - lib/jsapi/meta/invalid_argument_error.rb
61
+ - lib/jsapi/meta/method_chain.rb
61
62
  - lib/jsapi/meta/openapi.rb
62
63
  - lib/jsapi/meta/openapi/callback.rb
63
64
  - lib/jsapi/meta/openapi/callback/model.rb