jsapi 0.1.2 → 0.2.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: fbf64d8068afe39168667e9f907654d308ab933d7688c1a10a7c6341f66a13d5
4
- data.tar.gz: 006af1f3b5fec80a643c7dab1bdc82917428c355930c434302df465177235359
3
+ metadata.gz: 72aa6ad77a99ade6acb3d5c70b6521b640e012cccc7e9ab00b9d55db3062d4d6
4
+ data.tar.gz: 96ec077740dde1963ca6b448662e1f4370f3bc0b54778db2d03d9447ba69aa3b
5
5
  SHA512:
6
- metadata.gz: 04a02c4e688e8c02cf7cbb4ee66356bdcb570fa9ff89a8ba3303d52f4713d7bc5c8bbbddafd22b3871d3de960e7606f25dd9496d39731f0fa0e095ffa53688a0
7
- data.tar.gz: 5099312a76a600aed362733b23bf4e358ba18b3f5a9f060258cb5aabed3caecab8d28afd9034844ce8619adf5a811f418c7fc19b28f67738392d0ec8becc659e
6
+ metadata.gz: ec7997fbedc7c9035f9e36d5426b23502b87f4c4c654b1e02f93db711f514a8a434d371dc4761156165c439256ad3f339c7a151b1b35c8dcbf3d1e14f1fd4628
7
+ data.tar.gz: 1f2952e80bf883302fd835f9425bae0bd2e7629b2746d3abec4b19a259c5abbd9869534b18eb6c69afd94a9397e13c7d86d78720f3573d92ef355b1e66b99fb0
@@ -71,6 +71,8 @@ module Jsapi
71
71
  object.to_date
72
72
  when 'date-time'
73
73
  object.to_datetime
74
+ when 'duration'
75
+ object.iso8601
74
76
  else
75
77
  object.to_s
76
78
  end
@@ -24,16 +24,6 @@ module Jsapi
24
24
  # Specifies the location of the parameter.
25
25
  #
26
26
  # See Meta::Parameter::Model#in for further information.
27
-
28
- ##
29
- # :method: ref
30
- # :args: name
31
- # Specifies the name of the reusable parameter to be referred.
32
-
33
- ##
34
- # :method: schema
35
- # :args: name
36
- # Specifies the name of the reusable schema to be referred.
37
27
  end
38
28
  end
39
29
  end
@@ -17,16 +17,6 @@ module Jsapi
17
17
  # :method: description
18
18
  # :args: arg
19
19
  # Specifies the description of the request body.
20
-
21
- ##
22
- # :method: ref
23
- # :args: name
24
- # Specifies the name of the reusable request body to be referred.
25
-
26
- ##
27
- # :method: schema
28
- # :args: name
29
- # Specifies the name of the reusable schema to be referred.
30
20
  end
31
21
  end
32
22
  end
@@ -47,16 +47,6 @@ module Jsapi
47
47
  # Specifies the locale to be used when rendering a response.
48
48
  #
49
49
  # locale :en
50
-
51
- ##
52
- # :method: ref
53
- # :args: name
54
- # Specifies the name of the reusable response to be referred.
55
-
56
- ##
57
- # :method: schema
58
- # :args: name
59
- # Specifies the name of the reusable schema to be referred.
60
50
  end
61
51
  end
62
52
  end
@@ -8,7 +8,7 @@ module Jsapi
8
8
  # Includes all of the properties from +schemas+. Each argument must be the name of
9
9
  # a schema defined by ClassMethods#api_schema or Definitions#schema.
10
10
  def all_of(*schemas)
11
- schemas.each { |schema| _meta_model.add_all_of({ schema: schema }) }
11
+ schemas.each { |schema| _meta_model.add_all_of({ ref: schema }) }
12
12
  end
13
13
 
14
14
  ##
@@ -11,7 +11,7 @@ module Jsapi
11
11
  end
12
12
  end
13
13
 
14
- # Returns +true+ if it contains no elements, +false+ otherwise.
14
+ # Returns true if it contains no elements, false otherwise.
15
15
  def empty?
16
16
  @elements.empty?
17
17
  end
@@ -20,8 +20,7 @@ module Jsapi
20
20
  "#<#{self.class.name} [#{@elements.map(&:inspect).join(', ')}]>"
21
21
  end
22
22
 
23
- # See Value#validate.
24
- def validate(errors)
23
+ def validate(errors) # :nodoc:
25
24
  return false unless super
26
25
 
27
26
  @elements.map { |element| element.validate(errors) }.all?
@@ -5,7 +5,7 @@ module Jsapi
5
5
  # Represents +null+.
6
6
  class Null < Value
7
7
 
8
- # Returns allways +true+.
8
+ # Returns true.
9
9
  def empty?
10
10
  true
11
11
  end
@@ -14,7 +14,7 @@ module Jsapi
14
14
  "#<#{self.class}>"
15
15
  end
16
16
 
17
- # Returns allways +true+.
17
+ # Returns true.
18
18
  def null?
19
19
  true
20
20
  end
@@ -25,7 +25,7 @@ module Jsapi
25
25
  super(schema)
26
26
  end
27
27
 
28
- # Returns +true+ if all attributes are empty, +false+ otherwise.
28
+ # Returns true if all attributes are empty, false otherwise.
29
29
  def empty?
30
30
  @raw_attributes.values.all?(&:empty?)
31
31
  end
@@ -8,22 +8,36 @@ module Jsapi
8
8
 
9
9
  def initialize(value, schema)
10
10
  super(schema)
11
- @value = schema.convert(
12
- case schema.format
13
- when 'date'
14
- value.to_date
15
- when 'date-time'
16
- value.to_datetime
17
- else
18
- value.to_s
11
+
12
+ @value =
13
+ begin
14
+ case schema.format
15
+ when 'date'
16
+ value.to_date
17
+ when 'date-time'
18
+ value.to_datetime
19
+ when 'duration'
20
+ ActiveSupport::Duration.parse(value)
21
+ else
22
+ value.to_s
23
+ end
24
+ rescue StandardError => e
25
+ @error = e
26
+ value
19
27
  end
20
- )
28
+ @value = schema.convert(@value) unless defined? @error
21
29
  end
22
30
 
23
- # See Value#empty?.
24
- def empty?
31
+ def empty? # :nodoc:
25
32
  value.blank?
26
33
  end
34
+
35
+ def validate(errors) # :nodoc:
36
+ return super unless defined? @error
37
+
38
+ errors.add(:base, :invalid)
39
+ false
40
+ end
27
41
  end
28
42
  end
29
43
  end
@@ -14,7 +14,7 @@ module Jsapi
14
14
  end
15
15
 
16
16
  # Used by #validate to test whether or not it is empty.
17
- # Returns +false+ by default.
17
+ # Returns false by default.
18
18
  def empty?
19
19
  false
20
20
  end
@@ -23,13 +23,13 @@ module Jsapi
23
23
  "#<#{self.class} #{value.inspect}>"
24
24
  end
25
25
 
26
- # Used by #validate to test whether or not it is +null+.
27
- # Returns +false+ by default.
26
+ # Used by #validate to test whether or not it is null.
27
+ # Returns false by default.
28
28
  def null?
29
29
  false
30
30
  end
31
31
 
32
- # Validates it against #schema. Returns +true+ if it is valid, +false+ otherwise.
32
+ # Validates it against #schema. Returns true if it is valid, false otherwise.
33
33
  # Detected errors are added to +errors+.
34
34
  def validate(errors)
35
35
  unless schema.existence.reach?(self)
@@ -96,12 +96,13 @@ module Jsapi
96
96
  end
97
97
 
98
98
  if version.major == 2
99
+ uri = servers&.first&.then { |server| URI(server.url) }
99
100
  {
100
101
  swagger: '2.0',
101
102
  info: info&.to_openapi,
102
- host: host,
103
- basePath: base_path,
104
- schemes: schemes,
103
+ host: host || uri&.hostname,
104
+ basePath: base_path || uri&.path,
105
+ schemes: schemes || uri&.scheme&.then { |scheme| [scheme] },
105
106
  consumes: consumed_mime_types,
106
107
  produces: produced_mime_types,
107
108
  securityDefinitions: security_schemes,
@@ -47,12 +47,14 @@ module Jsapi
47
47
  def initialize(name, keywords = {})
48
48
  raise ArgumentError, "parameter name can't be blank" if name.blank?
49
49
 
50
+ @name = name.to_s
51
+
50
52
  keywords = keywords.dup
51
53
  super(keywords.extract!(:deprecated, :description, :examples, :in))
52
54
 
53
55
  add_example(value: keywords.delete(:example)) if keywords.key?(:example)
56
+ keywords[:ref] = keywords.delete(:schema) if keywords.key?(:schema)
54
57
 
55
- @name = name.to_s
56
58
  @schema = Schema.new(keywords)
57
59
  end
58
60
 
@@ -34,10 +34,12 @@ module Jsapi
34
34
  def initialize(name, keywords = {})
35
35
  raise ArgumentError, "property name can't be blank" if name.blank?
36
36
 
37
+ @name = name.to_s
38
+
37
39
  keywords = keywords.dup
38
40
  super(keywords.extract!(:read_only, :source, :write_only))
41
+ keywords[:ref] = keywords.delete(:schema) if keywords.key?(:schema)
39
42
 
40
- @name = name.to_s
41
43
  @schema = Schema.new(keywords)
42
44
  end
43
45
 
@@ -26,6 +26,7 @@ module Jsapi
26
26
  super(keywords.extract!(:description, :examples))
27
27
 
28
28
  add_example(value: keywords.delete(:example)) if keywords.key?(:example)
29
+ keywords[:ref] = keywords.delete(:schema) if keywords.key?(:schema)
29
30
 
30
31
  @schema = Schema.new(keywords)
31
32
  end
@@ -36,6 +36,7 @@ module Jsapi
36
36
  super(keywords.extract!(:description, :examples, :locale))
37
37
 
38
38
  add_example(value: keywords.delete(:example)) if keywords.key?(:example)
39
+ keywords[:ref] = keywords.delete(:schema) if keywords.key?(:schema)
39
40
 
40
41
  @schema = Schema.new(**keywords)
41
42
  end
@@ -7,7 +7,7 @@ module Jsapi
7
7
  ##
8
8
  # :attr: items
9
9
  # The Schema defining the kind of items.
10
- attribute :items, Schema
10
+ attribute :items, Schema, writer: false
11
11
 
12
12
  ##
13
13
  # :attr: max_items
@@ -19,6 +19,14 @@ module Jsapi
19
19
  # The minimum length of an array.
20
20
  attribute :min_items, writer: false
21
21
 
22
+ def items=(keywords = {}) # :nodoc:
23
+ if keywords.key?(:schema)
24
+ keywords = keywords.dup
25
+ keywords[:ref] = keywords.delete(:schema)
26
+ end
27
+ @items = Schema.new(keywords)
28
+ end
29
+
22
30
  def max_items=(value) # :nodoc:
23
31
  add_validation('max_items', Validation::MaxItems.new(value))
24
32
  @max_items = value
@@ -72,7 +72,7 @@ module Jsapi
72
72
  {}.tap do |properties|
73
73
  all_of_references.each do |reference|
74
74
  schema = reference.resolve(definitions)
75
- raise "circular reference: #{reference.schema}" if schema.in?(path)
75
+ raise "circular reference: #{reference.ref}" if schema.in?(path)
76
76
 
77
77
  properties.merge!(schema.merge_properties(definitions, path + [self]))
78
78
  end
@@ -4,9 +4,6 @@ module Jsapi
4
4
  module Meta
5
5
  module Schema
6
6
  class Reference < BaseReference
7
- alias :schema :ref
8
- alias :schema= :ref=
9
-
10
7
  ##
11
8
  # :attr: existence
12
9
  # The level of Existence. The default level of existence is +ALLOW_OMITTED+.
@@ -8,12 +8,8 @@ module Jsapi
8
8
 
9
9
  ##
10
10
  # :attr: format
11
- # The format of a string. Possible values are:
12
- #
13
- # - <code>"date"</code>
14
- # - <code>"date-time"</code>
15
- #
16
- attribute :format, ::String, values: %w[date date-time]
11
+ # The format of a string.
12
+ attribute :format, ::String
17
13
 
18
14
  ##
19
15
  # :attr: max_length
@@ -33,9 +33,7 @@ module Jsapi
33
33
  #
34
34
  # Raises an InvalidArgumentError if the given type is invalid.
35
35
  def new(keywords = {})
36
- if keywords.key?(:ref) || keywords.key?(:schema)
37
- return Reference.new(keywords)
38
- end
36
+ return Reference.new(keywords) if keywords.key?(:ref)
39
37
 
40
38
  type = keywords[:type]
41
39
  case type&.to_s
data/lib/jsapi/version.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Jsapi
4
- # Holds the current GEM version
5
4
  module Version
6
- VERSION = '0.1.2'
7
-
8
5
  # NOTE: See https://bundler.io/guides/creating_gem.html
6
+
7
+ # The current GEM version.
8
+ VERSION = '0.2.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.1.2
4
+ version: 0.2.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-06-28 00:00:00.000000000 Z
11
+ date: 2024-07-05 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