Module: Jsapi::DSL::ClassMethods

Defined in:
lib/jsapi/dsl/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#api_base_path(arg) ⇒ Object

Specifies the base path of the API.

api_base_path '/foo'


10
11
12
# File 'lib/jsapi/dsl/class_methods.rb', line 10

def api_base_path(arg)
  api_definitions { base_path(arg) }
end

#api_callback(name, **keywords, &block) ⇒ Object

Specifies a reusable callback.

api_callback 'onFoo' do
  operation '{$request.query.foo}', path: '/bar'
end


20
21
22
# File 'lib/jsapi/dsl/class_methods.rb', line 20

def api_callback(name, **keywords, &block)
  api_definitions { callback(name, **keywords, &block) }
end

#api_default(type, **keywords, &block) ⇒ Object

Specifies the general default values for type.

api_default 'array', within_requests: [], within_responses: []


28
29
30
# File 'lib/jsapi/dsl/class_methods.rb', line 28

def api_default(type, **keywords, &block)
  api_definitions { default(type, **keywords, &block) }
end

#api_definitions(**keywords, &block) ⇒ Object

Returns the API definitions associated with the current class. Adds additional definitions when any keywords or a block is specified.

api_definitions base_path: '/foo' do
  operation 'bar'
end


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jsapi/dsl/class_methods.rb', line 38

def api_definitions(**keywords, &block)
  unless defined? @api_definitions
    @api_definitions = Meta::Definitions.new(
      owner: self,
      parent: superclass.try(:api_definitions)
    )
    if (name = try(:name))
      pathname = Jsapi.configuration.pathname(
        name.deconstantize.split('::').map(&:underscore),
        "#{name.demodulize.delete_suffix('Controller').underscore}.rb"
      )
      Definitions.new(@api_definitions, pathname) if pathname&.file?
    end
  end
  @api_definitions.merge!(keywords) if keywords.any?
  Definitions.new(@api_definitions, &block) if block

  @api_definitions
end

#api_example(name, **keywords, &block) ⇒ Object

Specifies a reusable example.

example 'foo', value: 'bar'


62
63
64
# File 'lib/jsapi/dsl/class_methods.rb', line 62

def api_example(name, **keywords, &block)
  api_definitions { example(name, **keywords, &block) }
end

#api_external_docs(**keywords, &block) ⇒ Object

Specifies the external documentation.

api_external_docs url: 'https://foo.bar'


70
71
72
# File 'lib/jsapi/dsl/class_methods.rb', line 70

def api_external_docs(**keywords, &block)
  api_definitions { external_docs(**keywords, &block) }
end

#api_header(name, **keywords, &block) ⇒ Object

Specifies a reusable header.

api_header 'foo', type: 'string'


78
79
80
# File 'lib/jsapi/dsl/class_methods.rb', line 78

def api_header(name, **keywords, &block)
  api_definitions { header(name, **keywords, &block) }
end

#api_host(arg) ⇒ Object

Specifies the host serving the API.

api_host 'foo.bar'


86
87
88
# File 'lib/jsapi/dsl/class_methods.rb', line 86

def api_host(arg)
  api_definitions { host(arg) }
end

#api_import(filename) ⇒ Object

Imports the file named filename relative to Jsapi.configation.path.



91
92
93
# File 'lib/jsapi/dsl/class_methods.rb', line 91

def api_import(filename)
  api_definitions { import(filename) }
end

#api_include(*klasses) ⇒ Object

Includes API definitions from klasses.



96
97
98
# File 'lib/jsapi/dsl/class_methods.rb', line 96

def api_include(*klasses)
  api_definitions { include(*klasses) }
end

#api_info(**keywords, &block) ⇒ Object

Specifies general information about the API.

api_info title: 'Foo', version: '1' do
  contact name: 'bar'
end


105
106
107
# File 'lib/jsapi/dsl/class_methods.rb', line 105

def api_info(**keywords, &block)
  api_definitions { info(**keywords, &block) }
end

Specifies a reusable link.

api_link 'foo', operation_id: 'bar'


113
114
115
# File 'lib/jsapi/dsl/class_methods.rb', line 113

def api_link(name, **keywords, &block)
  api_definitions { link(name, **keywords, &block) }
end

#api_on_rescue(method = nil, &block) ⇒ Object

Registers a callback to be called when rescuing an exception.

api_on_rescue :foo

api_on_rescue do |error|
  # ...
end


124
125
126
# File 'lib/jsapi/dsl/class_methods.rb', line 124

def api_on_rescue(method = nil, &block)
  api_definitions { on_rescue(method, &block) }
end

#api_operation(name = nil, **keywords, &block) ⇒ Object

Specifies an operation.

api_operation 'foo', path: '/foo' do
  parameter 'bar', type: 'string'
  response do
    property 'foo', type: 'string'
  end
end

name can be nil if the controller handles one operation only.



138
139
140
# File 'lib/jsapi/dsl/class_methods.rb', line 138

def api_operation(name = nil, **keywords, &block)
  api_definitions { operation(name, **keywords, &block) }
end

#api_parameter(name, **keywords, &block) ⇒ Object

Specifies a reusable parameter.

api_parameter 'foo', type: 'string'


146
147
148
# File 'lib/jsapi/dsl/class_methods.rb', line 146

def api_parameter(name, **keywords, &block)
  api_definitions { parameter(name, **keywords, &block) }
end

#api_request_body(name, **keywords, &block) ⇒ Object

Defines a reusable request body.

api_request_body 'foo', type: 'string'


154
155
156
# File 'lib/jsapi/dsl/class_methods.rb', line 154

def api_request_body(name, **keywords, &block)
  api_definitions { request_body(name, **keywords, &block) }
end

#api_rescue_from(*klasses, with: nil) ⇒ Object

Specifies the HTTP status code of an error response rendered when an exception of any of klasses has been raised.

api_rescue_from Jsapi::Controller::ParametersInvalid, with: 400


163
164
165
# File 'lib/jsapi/dsl/class_methods.rb', line 163

def api_rescue_from(*klasses, with: nil)
  api_definitions { rescue_from(*klasses, with: with) }
end

#api_response(name, **keywords, &block) ⇒ Object

Specifies a reusable response.

api_response 'Foo', type: 'object' do
  property 'bar', type: 'string'
end


172
173
174
# File 'lib/jsapi/dsl/class_methods.rb', line 172

def api_response(name, **keywords, &block)
  api_definitions { response(name, **keywords, &block) }
end

#api_schema(name, **keywords, &block) ⇒ Object

Specifies a reusable schema.

api_schema 'Foo' do
  property 'bar', type: 'string'
end


181
182
183
# File 'lib/jsapi/dsl/class_methods.rb', line 181

def api_schema(name, **keywords, &block)
  api_definitions { schema(name, **keywords, &block) }
end

#api_scheme(arg) ⇒ Object

Specifies a URI scheme supported by the API.

api_scheme 'https'


189
190
191
# File 'lib/jsapi/dsl/class_methods.rb', line 189

def api_scheme(arg)
  api_definitions { scheme(arg) }
end

#api_security_requirement(**keywords, &block) ⇒ Object

Specifies a security requirement.

api_security_requirement do
  scheme 'basic_auth'
end


199
200
201
# File 'lib/jsapi/dsl/class_methods.rb', line 199

def api_security_requirement(**keywords, &block)
  api_definitions { security_requirement(**keywords, &block) }
end

#api_security_scheme(name, **keywords, &block) ⇒ Object

Specifies a security scheme.

api_security_scheme 'basic_auth', type: 'http', scheme: 'basic'


207
208
209
# File 'lib/jsapi/dsl/class_methods.rb', line 207

def api_security_scheme(name, **keywords, &block)
  api_definitions { security_scheme(name, **keywords, &block) }
end

#api_server(**keywords, &block) ⇒ Object

Specifies a server providing the API.

api_server url: 'https://foo.bar/foo'


215
216
217
# File 'lib/jsapi/dsl/class_methods.rb', line 215

def api_server(**keywords, &block)
  api_definitions { server(**keywords, &block) }
end

#api_tag(**keywords, &block) ⇒ Object

Specifies a tag.

api_tag name: 'foo', description: 'description of foo'


223
224
225
# File 'lib/jsapi/dsl/class_methods.rb', line 223

def api_tag(**keywords, &block)
  api_definitions { tag(**keywords, &block) }
end