jsapi 0.1.1 → 0.1.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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jsapi/controller/error_result.rb +1 -2
  3. data/lib/jsapi/controller/methods.rb +16 -19
  4. data/lib/jsapi/controller/parameters.rb +7 -8
  5. data/lib/jsapi/controller/parameters_invalid.rb +1 -2
  6. data/lib/jsapi/controller/response.rb +2 -2
  7. data/lib/jsapi/dsl/callbacks.rb +1 -1
  8. data/lib/jsapi/dsl/definitions.rb +3 -3
  9. data/lib/jsapi/dsl/error.rb +3 -5
  10. data/lib/jsapi/dsl/examples.rb +1 -1
  11. data/lib/jsapi/dsl/openapi/callback.rb +1 -1
  12. data/lib/jsapi/dsl/openapi/root.rb +115 -1
  13. data/lib/jsapi/dsl/operation.rb +50 -11
  14. data/lib/jsapi/dsl/parameter.rb +29 -0
  15. data/lib/jsapi/dsl/request_body.rb +22 -0
  16. data/lib/jsapi/dsl/response.rb +29 -0
  17. data/lib/jsapi/dsl/schema.rb +139 -13
  18. data/lib/jsapi/json/value.rb +2 -2
  19. data/lib/jsapi/meta/attributes/type_caster.rb +3 -2
  20. data/lib/jsapi/meta/base.rb +3 -2
  21. data/lib/jsapi/meta/base_reference.rb +3 -2
  22. data/lib/jsapi/meta/definitions.rb +2 -1
  23. data/lib/jsapi/meta/example/model.rb +1 -2
  24. data/lib/jsapi/meta/example/reference.rb +0 -1
  25. data/lib/jsapi/meta/invalid_argument_error.rb +1 -0
  26. data/lib/jsapi/meta/openapi/callback/model.rb +1 -1
  27. data/lib/jsapi/meta/openapi/root.rb +4 -5
  28. data/lib/jsapi/meta/openapi/security_scheme/oauth2.rb +1 -2
  29. data/lib/jsapi/meta/openapi/security_scheme/open_id_connect.rb +2 -3
  30. data/lib/jsapi/meta/openapi/security_scheme.rb +2 -2
  31. data/lib/jsapi/meta/openapi/version.rb +3 -0
  32. data/lib/jsapi/meta/operation.rb +15 -24
  33. data/lib/jsapi/meta/parameter/model.rb +2 -2
  34. data/lib/jsapi/meta/parameter/reference.rb +3 -4
  35. data/lib/jsapi/meta/property.rb +3 -4
  36. data/lib/jsapi/meta/request_body/model.rb +1 -1
  37. data/lib/jsapi/meta/rescue_handler.rb +7 -0
  38. data/lib/jsapi/meta/response/model.rb +1 -1
  39. data/lib/jsapi/meta/schema/base.rb +7 -9
  40. data/lib/jsapi/meta/schema/reference.rb +1 -2
  41. data/lib/jsapi/meta/schema/string.rb +1 -1
  42. data/lib/jsapi/meta/schema/validation/maximum.rb +6 -2
  43. data/lib/jsapi/meta/schema/validation/minimum.rb +6 -2
  44. data/lib/jsapi/meta/schema/validation/multiple_of.rb +3 -1
  45. data/lib/jsapi/model/base.rb +2 -2
  46. data/lib/jsapi/model/errors.rb +3 -4
  47. data/lib/jsapi/model/naming.rb +3 -4
  48. data/lib/jsapi/model/validations.rb +2 -2
  49. data/lib/jsapi/version.rb +5 -3
  50. metadata +5 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4326454795353f4fc3d2513d4b58b5d42df113665fb3e7b197e2aac0917de794
4
- data.tar.gz: 3f25ce60031f26bb76b3a4dbebd7cf9cd333ae928bc59d12aee46d215206dcc2
3
+ metadata.gz: fbf64d8068afe39168667e9f907654d308ab933d7688c1a10a7c6341f66a13d5
4
+ data.tar.gz: 006af1f3b5fec80a643c7dab1bdc82917428c355930c434302df465177235359
5
5
  SHA512:
6
- metadata.gz: 1f0304f56a303c1e2658ddaf31a40c0e3d6ad4584094896cc6eeb9fb40c919e91f45fffa9bc47a0f8096426eadcd301987de313f5145b779974d874f89988a4e
7
- data.tar.gz: b2255d64dc7c9e8b05c639a2e62fbc7e33dc71074d3284aedd5788de4da8951c9aebc00c8589a4240bcf450f17ba8090696e96ea5a1259e3c81004fc3b2e9776
6
+ metadata.gz: 04a02c4e688e8c02cf7cbb4ee66356bdcb570fa9ff89a8ba3303d52f4713d7bc5c8bbbddafd22b3871d3de960e7606f25dd9496d39731f0fa0e095ffa53688a0
7
+ data.tar.gz: 5099312a76a600aed362733b23bf4e358ba18b3f5a9f060258cb5aabed3caecab8d28afd9034844ce8619adf5a811f418c7fc19b28f67738392d0ec8becc659e
@@ -10,8 +10,7 @@ module Jsapi
10
10
 
11
11
  delegate :message, :to_s, to: :@exception
12
12
 
13
- # Creates a new instance to produce an error response with the given
14
- # HTTP status code.
13
+ # Creates a new instance to produce an error response with the given HTTP status code.
15
14
  def initialize(exception, status:)
16
15
  @exception = exception
17
16
  @status = status
@@ -3,8 +3,8 @@
3
3
  module Jsapi
4
4
  module Controller
5
5
  module Methods
6
- # Returns the Meta::Definitions instance associated with the controller class.
7
- # This method can be used to create an OpenAPI document, for example:
6
+ # Returns the Meta::Definitions instance associated with the controller class. In
7
+ # particular, this method can be used to create an OpenAPI document, for example:
8
8
  #
9
9
  # render(json: api_definitions.openapi_document)
10
10
  #
@@ -12,10 +12,10 @@ module Jsapi
12
12
  self.class.api_definitions
13
13
  end
14
14
 
15
- # Performs an API operation by calling the given block. The request parameters
16
- # are passed as an instance of the operation's model class to the block. The
17
- # object returned by the block is implicitly rendered according to the
18
- # appropriate +response+ specification.
15
+ # Performs an API operation by calling the given block. The request parameters are
16
+ # passed as an instance of the operation's model class to the block. The object
17
+ # returned by the block is implicitly rendered according to the appropriate
18
+ # +response+ specification.
19
19
  #
20
20
  # api_operation('foo') do |api_params|
21
21
  # # ...
@@ -23,10 +23,9 @@ module Jsapi
23
23
  #
24
24
  # +operation_name+ can be +nil+ if the controller handles one operation only.
25
25
  #
26
- # If +strong+ is +true+, parameters that can be mapped are accepted only.
27
- # That means that the model passed to the block is invalid if there are any
28
- # request parameters that cannot be mapped to a parameter or a request body
29
- # property of the operation.
26
+ # If +strong+ is +true+, parameters that can be mapped are accepted only. That means
27
+ # that the model passed to the block is invalid if there are any request parameters
28
+ # that can't be mapped to a parameter or a request body property of the operation.
30
29
  #
31
30
  def api_operation(operation_name = nil, status: nil, strong: false, &block)
32
31
  _perform_api_operation(
@@ -37,8 +36,8 @@ module Jsapi
37
36
  )
38
37
  end
39
38
 
40
- # Like +api_operation+, except that a ParametersInvalid exception is raised
41
- # if the request parameters are invalid.
39
+ # Like +api_operation+, except that a ParametersInvalid exception is raised on
40
+ # invalid request parameters.
42
41
  #
43
42
  # api_operation!('foo') do |api_params|
44
43
  # # ...
@@ -59,13 +58,11 @@ module Jsapi
59
58
  #
60
59
  # +operation_name+ can be +nil+ if the controller handles one operation only.
61
60
  #
62
- # If +strong+ is +true+, parameters that can be mapped are accepted only.
63
- # That means that the model returned is invalid if there are any request
64
- # parameters that cannot be mapped to a parameter or a request body property
65
- # of the operation.
61
+ # If +strong+ is +true+, parameters that can be mapped are accepted only. That means
62
+ # that the model returned is invalid if there are any request parameters that can't be
63
+ # mapped to a parameter or a request body property of the operation.
66
64
  #
67
65
  # Note that each call of +api_params+ returns a newly created instance.
68
- #
69
66
  def api_params(operation_name = nil, strong: false)
70
67
  definitions = api_definitions
71
68
  _api_params(
@@ -75,8 +72,8 @@ module Jsapi
75
72
  )
76
73
  end
77
74
 
78
- # Returns a Response to serialize the JSON representation of +result+ according
79
- # to the appropriate +response+ specification.
75
+ # Returns a Response to serialize the JSON representation of +result+ according to the
76
+ # appropriate +response+ specification.
80
77
  #
81
78
  # render(json: api_response(bar, 'foo', status: 200))
82
79
  #
@@ -8,13 +8,12 @@ module Jsapi
8
8
 
9
9
  attr_reader :raw_attributes
10
10
 
11
- # Creates a new instance that wraps +params+ according to +operation+.
12
- # References are resolved to API components in +definitions+.
11
+ # Creates a new instance that wraps +params+ according to +operation+. References are
12
+ # resolved to API components in +definitions+.
13
13
  #
14
- # If +strong+ is true+ parameters that can be mapped are accepted only.
15
- # That means that the instance created is invalid if +params+ contains
16
- # any parameters that cannot be mapped to a parameter or a request body
17
- # property of +operation+.
14
+ # If +strong+ is true+ parameters that can be mapped are accepted only. That means that
15
+ # the instance created is invalid if +params+ contains any parameters that can't be
16
+ # mapped to a parameter or a request body property of +operation+.
18
17
  def initialize(params, operation, definitions, strong: false)
19
18
  @params = params
20
19
  @strong = strong == true
@@ -43,8 +42,8 @@ module Jsapi
43
42
  "#{attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')}>"
44
43
  end
45
44
 
46
- # Validates the request parameters. Returns true if the parameters are
47
- # valid, false otherwise. Detected errors are added to +errors+.
45
+ # Validates the request parameters. Returns true if the parameters are valid, false
46
+ # otherwise. Detected errors are added to +errors+.
48
47
  def validate(errors)
49
48
  [
50
49
  validate_attributes(errors),
@@ -11,8 +11,7 @@ module Jsapi
11
11
  super('')
12
12
  end
13
13
 
14
- # Overrides <code>StandardError#message</code> to lazily generate the
15
- # error message.
14
+ # Overrides <code>StandardError#message</code> to lazily generate the error message.
16
15
  def message
17
16
  "#{
18
17
  @params.errors.full_messages.map do |message|
@@ -5,8 +5,8 @@ module Jsapi
5
5
  # Used to serialize a response.
6
6
  class Response
7
7
 
8
- # Creates a new instance to serialize +object+ according to +response+.
9
- # References are resolved to API components in +definitions+.
8
+ # Creates a new instance to serialize +object+ according to +response+. References are
9
+ # resolved to API components in +definitions+.
10
10
  def initialize(object, response, definitions)
11
11
  @object = object
12
12
  @response = response
@@ -3,7 +3,7 @@
3
3
  module Jsapi
4
4
  module DSL
5
5
  module Callbacks
6
- # Defines an OpenAPI callback or refers a reusable callback.
6
+ # Defines an \OpenAPI callback or refers a reusable callback.
7
7
  #
8
8
  # # define a callback
9
9
  # callback 'foo' do
@@ -12,7 +12,7 @@ module Jsapi
12
12
  end
13
13
  end
14
14
 
15
- # Defines the root of an OpenAPI document.
15
+ # Defines the root of an \OpenAPI document.
16
16
  #
17
17
  # openapi do
18
18
  # info title: 'Foo', version: '1'
@@ -63,8 +63,8 @@ module Jsapi
63
63
  end
64
64
  end
65
65
 
66
- # Specifies the HTTP status code of an error response rendered when an
67
- # exception of any of +klasses+ has been raised.
66
+ # Specifies the HTTP status code of an error response rendered when an exception of
67
+ # any of +klasses+ has been raised.
68
68
  #
69
69
  # rescue_from Jsapi::Controller::ParametersInvalid, with: 400
70
70
  #
@@ -5,8 +5,7 @@ module Jsapi
5
5
  # Raised when an error occurred while defining an API component.
6
6
  class Error < StandardError
7
7
 
8
- # Creates a new error. +origin+ is the innermost position at where
9
- # the error occurred.
8
+ # Creates a new error. +origin+ is the innermost position at where the error occurred.
10
9
  def initialize(error_or_message, origin = nil)
11
10
  @path = Array(origin)
12
11
  super(
@@ -18,9 +17,8 @@ module Jsapi
18
17
  )
19
18
  end
20
19
 
21
- # Overrides <code>StandardError#message</code> to append the whole path of
22
- # the position at where the error occurred, for example:
23
- # <code>{message} (at foo / bar)</code>.
20
+ # Overrides <code>StandardError#message</code> to append the whole path of the position
21
+ # at where the error occurred, for example: <code>{message} (at foo / bar)</code>.
24
22
  def message
25
23
  message = super
26
24
  return message if @path.empty?
@@ -3,7 +3,7 @@
3
3
  module Jsapi
4
4
  module DSL
5
5
  module Examples
6
- # Defines an example.
6
+ # Adds an example.
7
7
  #
8
8
  # example 'foo', value: 'bar'
9
9
  #
@@ -6,7 +6,7 @@ module Jsapi
6
6
  # Used to specify details of a callback.
7
7
  class Callback < Node
8
8
 
9
- # Defines a callback operation.
9
+ # Adds a callback operation.
10
10
  #
11
11
  # operation '{$request.query.foo}' do
12
12
  # parameter 'bar', type: 'string'
@@ -3,9 +3,123 @@
3
3
  module Jsapi
4
4
  module DSL
5
5
  module OpenAPI
6
- # Used to specify details of an OpenAPI object.
6
+ # Used to specify details of an \OpenAPI object.
7
7
  class Root < Node
8
8
  include Callbacks
9
+
10
+ ##
11
+ # :method: base_path
12
+ # :args: arg
13
+ # Specifies the base path of the API.
14
+ #
15
+ # base_path '/foo'
16
+ #
17
+ # See Meta::OpenAPI::Root#base_path for further information.
18
+
19
+ ##
20
+ # :method: consumes
21
+ # :args: mime_types
22
+ # Specifies one or more MIME types the API can consume.
23
+ #
24
+ # consumes 'application/json'
25
+ #
26
+ # consumes %w[application/json application/pdf]
27
+ #
28
+ # See Meta::OpenAPI::Root#consumes for further information.
29
+
30
+ ##
31
+ # :method: external_docs
32
+ # :args: **keywords, &block
33
+ # Specifies the external documentation.
34
+ #
35
+ # external_docs url: 'https://foo.bar'
36
+ #
37
+ # See Meta::OpenAPI::Root#external_docs for further information.
38
+
39
+ ##
40
+ # :method: host
41
+ # :args: arg
42
+ # Specifies the host serving the API.
43
+ #
44
+ # host 'foo.bar'
45
+ #
46
+ # See Meta::OpenAPI::Root#host for further information.
47
+
48
+ ##
49
+ # :method: info
50
+ # :args: **keywords, &block
51
+ # Specifies general information about the API.
52
+ #
53
+ # info title: 'foo', version: 1
54
+ #
55
+ # See Meta::OpenAPI::Root#info for further information.
56
+
57
+ ##
58
+ # :method: link
59
+ # :args: name, **keywords, &block
60
+ # Adds a link.
61
+ #
62
+ # link 'foo', operation_id: 'bar'
63
+ #
64
+ # See Meta::OpenAPI::Root#links for further information.
65
+
66
+ ##
67
+ # :method: produces
68
+ # :args: mime_types
69
+ # Specifies one or more MIME types the API can produce.
70
+ #
71
+ # produces 'application/json'
72
+ #
73
+ # produces %w[application/json application/pdf]'
74
+ #
75
+ # See Meta::OpenAPI::Root#produces for further information.
76
+
77
+ ##
78
+ # :method: scheme
79
+ # :args: arg
80
+ # Adds a URI scheme supported by the API.
81
+ #
82
+ # scheme 'https'
83
+ #
84
+ # See Meta::OpenAPI::Root#scheme for further information.
85
+
86
+ ##
87
+ # :method: security_requirement
88
+ # :args: **keywords, &block
89
+ # Adds a security requirement.
90
+ #
91
+ # security_requirement do
92
+ # scheme 'basic_auth'
93
+ # end
94
+ #
95
+ # See Meta::OpenAPI::Root#security_requirements for further information.
96
+
97
+ ##
98
+ # :method: security_scheme
99
+ # :args: name, **keywords, &block
100
+ # Adds a security scheme.
101
+ #
102
+ # security_scheme 'basic_auth', type: 'http', scheme: 'basic'
103
+ #
104
+ # See Meta::OpenAPI::Root#security_schemes for further information.
105
+
106
+ ##
107
+ # :method: server
108
+ # :args: arg
109
+ # Adds a server providing the API.
110
+ #
111
+ # server url: 'https://foo.bar'
112
+ #
113
+ # See Meta::OpenAPI::Root#servers for further information.
114
+
115
+ ##
116
+ # :method: tag
117
+ # :args: **keywords, &block
118
+ # Adds a tag.
119
+ #
120
+ # tag name: 'foo', description: 'description of foo'
121
+ #
122
+ # See Meta::OpenAPI::Root#tags for further information.
9
123
  end
10
124
  end
11
125
  end
@@ -6,12 +6,28 @@ module Jsapi
6
6
  class Operation < Node
7
7
  include Callbacks
8
8
 
9
- # Overrides Object#method to handle +method+ as a keyword.
10
- def method(method) # :nodoc:
11
- _keyword(:method, method)
9
+ ##
10
+ # :method: deprecated
11
+ # :args: arg
12
+ # Specifies whether or not the operation is deprecated.
13
+ #
14
+ # deprecated true
15
+
16
+ ##
17
+ # :method: description
18
+ # :args: arg
19
+ # Specifies the description of the operation.
20
+
21
+ # Specifies the HTTP verb of the operation.
22
+ #
23
+ # method 'post'
24
+ #
25
+ # See Meta::Operation#method for further information.
26
+ def method(arg)
27
+ _keyword(:method, arg)
12
28
  end
13
29
 
14
- # Specifies the model class to access top-level parameters by.
30
+ # Defines the model class to access top-level parameters by.
15
31
  #
16
32
  # model Foo do
17
33
  # def bar
@@ -19,9 +35,8 @@ module Jsapi
19
35
  # end
20
36
  # end
21
37
  #
22
- # +klass+ can be any subclass of Model::Base. If block is given, an
23
- # anonymous class is created that inherits either from +klass+ or
24
- # Model::Base.
38
+ # +klass+ can be any subclass of Model::Base. If block is given, an anonymous class
39
+ # is created that inherits either from +klass+ or Model::Base.
25
40
  def model(klass = nil, &block)
26
41
  if block
27
42
  klass = Class.new(klass || Model::Base)
@@ -30,7 +45,7 @@ module Jsapi
30
45
  _meta_model.model = klass
31
46
  end
32
47
 
33
- # Defines a parameter or refers a reusable parameter.
48
+ # Adds a parameter or a reference to a reusable parameter.
34
49
  #
35
50
  # # define a parameter
36
51
  # parameter 'foo', type: 'string'
@@ -43,8 +58,8 @@ module Jsapi
43
58
  # # refer a reusable parameter
44
59
  # parameter ref: 'foo'
45
60
  #
46
- # Refers the reusable parameter with the same name if neither any
47
- # keywords nor a block is specified.
61
+ # Refers the reusable parameter with the same name if neither any keywords nor a
62
+ # block is specified.
48
63
  #
49
64
  # parameter 'foo'
50
65
  #
@@ -58,6 +73,11 @@ module Jsapi
58
73
  end
59
74
  end
60
75
 
76
+ ##
77
+ # :method: path
78
+ # :args: arg
79
+ # Specifies the relative path of the operation.
80
+
61
81
  # Defines the request body or refers a reusable request body.
62
82
  #
63
83
  # # define a request body
@@ -80,7 +100,7 @@ module Jsapi
80
100
  end
81
101
  end
82
102
 
83
- # Defines a response or refers a reusable response.
103
+ # Adds a response or a reference to a reusable response.
84
104
  #
85
105
  # # define a response
86
106
  # response 200, type: 'object' do
@@ -113,6 +133,25 @@ module Jsapi
113
133
  _eval(response_model, Response, &block)
114
134
  end
115
135
  end
136
+
137
+ ##
138
+ # :method: summary
139
+ # :args: arg
140
+ # Specifies the short summary of the operation.
141
+
142
+ ##
143
+ # :method: tag
144
+ # :args: name
145
+ # Adds a tag.
146
+ #
147
+ # tag 'foo'
148
+
149
+ ##
150
+ # :method: tags
151
+ # :args: names
152
+ # Specifies all of the tags at once.
153
+ #
154
+ # tags %w[foo bar]
116
155
  end
117
156
  end
118
157
  end
@@ -5,6 +5,35 @@ module Jsapi
5
5
  # Used to specify details of a parameter.
6
6
  class Parameter < Schema
7
7
  include Examples
8
+
9
+ ##
10
+ # :method: deprecated
11
+ # :args: arg
12
+ # Specifies whether or not the parameter is deprecated.
13
+ #
14
+ # deprecated true
15
+
16
+ ##
17
+ # :method: description
18
+ # :args: arg
19
+ # Specifies the description of the parameter.
20
+
21
+ ##
22
+ # :method: in
23
+ # :args: location
24
+ # Specifies the location of the parameter.
25
+ #
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.
8
37
  end
9
38
  end
10
39
  end
@@ -5,6 +5,28 @@ module Jsapi
5
5
  # Used to specify details of a request body.
6
6
  class RequestBody < Schema
7
7
  include Examples
8
+
9
+ ##
10
+ # :method: deprecated
11
+ # :args: arg
12
+ # Specifies whether or not the request body is deprecated.
13
+ #
14
+ # deprecated true
15
+
16
+ ##
17
+ # :method: description
18
+ # :args: arg
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.
8
30
  end
9
31
  end
10
32
  end
@@ -6,6 +6,18 @@ module Jsapi
6
6
  class Response < Schema
7
7
  include Examples
8
8
 
9
+ ##
10
+ # :method: deprecated
11
+ # :args: arg
12
+ # Specifies whether or not the response is deprecated.
13
+ #
14
+ # deprecated true
15
+
16
+ ##
17
+ # :method: description
18
+ # :args: arg
19
+ # Specifies the description of the response.
20
+
9
21
  # Defines a link or refers a reusable link object.
10
22
  #
11
23
  # # define a link
@@ -28,6 +40,23 @@ module Jsapi
28
40
  Node.new(link_model, &block) if block
29
41
  end
30
42
  end
43
+
44
+ ##
45
+ # :method: locale
46
+ # :args: arg
47
+ # Specifies the locale to be used when rendering a response.
48
+ #
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.
31
60
  end
32
61
  end
33
62
  end