Module: Flappi::Definition
- Includes:
- Common
- Defined in:
- lib/flappi/definition.rb
Overview
DSL for API construction.
This DSL defines methods which can be used to construct API responses, docs, etc. Include it in each API endpoint definition as shown below:
Constant Summary collapse
- BOOLEAN =
:boolean_type
- BOOLEAN_STRICT =
:boolean_strict
- SOURCE_PRESENT =
:source_present
Instance Method Summary collapse
-
#api_error(status_code, field_name, field_description) ⇒ Object
Define an API error.
-
#build(options = {}) {|base_object| ... } ⇒ Object
Define how to build the API response.
-
#check_params(mode = false) ⇒ Object
Enable parameter check for strict parameters in Rails.
-
#description(v) ⇒ Object
Define the description.
-
#field(*args_or_name, &block) ⇒ Object
Define a single (scalar) field in the result.
-
#group(v) ⇒ Object
Define the API group this endpoint is in - used to break documentation into sections.
-
#hash_key(*args, &block) ⇒ Object
Define the hash key to be used in generating a hash from #objects.
-
#http_method(v) ⇒ Object
Define the HTTP method - note however that Rails and the controller is responsible for routing.
-
#link(*link_params) ⇒ Object
Specify a link to be provided in the response Links defined at the top level will be output at the end of the response Links defined at object level will be output at the end of the object.
-
#major_version ⇒ Object
The major version for documentation and example purposes.
-
#object(*args_or_name, &block) ⇒ Object
Define an object (which will be rendered within json as name:hash).
-
#objects(*args_or_name, &block) ⇒ Object
Define multiple objects which will be rendered into json as an array of object hashes.
-
#param(*args_or_name, &block) ⇒ Object
Define an input parameter (inline or query string) This is used to document and validate the parameters.
-
#path(v) ⇒ Object
Define the path to this endpoint under the API root.
-
#query {|controller_params| ... } ⇒ Object
Define a query to be used to retrieve the source object for the response.
-
#reference(*args_or_name, &block) ⇒ Object
Creates a sideloaded reference to an object created by the block.
-
#request_example(*v) ⇒ Object
Define an example request path (no scheme or host) that will be included in documentation.
-
#response_example(*v) ⇒ Object
Define an example response that will be included in documentation.
-
#return_error(status_code, error_info) ⇒ Object
From inside a query, return an error.
-
#return_no_content ⇒ Object
From inside a query, return 204 NO CONTENT.
-
#strict(mode = false) ⇒ Object
Enable/disable strict mode, unknown parameters will cause an error Default is to disable this.
-
#title(v) ⇒ Object
Define the title.
-
#version(version_rule) ⇒ Object
Define the version this endpoint works with.
Instance Method Details
#api_error(status_code, field_name, field_description) ⇒ Object
Define an API error
360 361 362 363 364 365 366 |
# File 'lib/flappi/definition.rb', line 360 def api_error(status_code, field_name, field_description) endpoint_info[:api_errors] << { status_code: status_code, response_field_name: field_name, response_field_description: field_description } end |
#build(options = {}) {|base_object| ... } ⇒ Object
Define how to build the API response. Use this typically with a block that defines how each field of the response is to be generated.
130 131 132 |
# File 'lib/flappi/definition.rb', line 130 def build( = {}, &block) @delegate.build , &block end |
#check_params(mode = false) ⇒ Object
Enable parameter check for strict parameters in Rails
444 445 446 |
# File 'lib/flappi/definition.rb', line 444 def check_params(mode = false) endpoint_info[:check_params] = mode end |
#description(v) ⇒ Object
Define the description
317 318 319 |
# File 'lib/flappi/definition.rb', line 317 def description(v) endpoint_info[:description] = v end |
#field(name) {|current_source| ... } ⇒ Object #field(name, value) ⇒ Object #field(options = {}) {|current_source| ... } ⇒ Object
Define a single (scalar) field in the result. This will produce a name:value pair in the json response.
236 237 238 |
# File 'lib/flappi/definition.rb', line 236 def field(*args_or_name, &block) @delegate.field(*args_or_name, block) end |
#group(v) ⇒ Object
Define the API group this endpoint is in - used to break documentation into sections
335 336 337 |
# File 'lib/flappi/definition.rb', line 335 def group(v) endpoint_info[:group] = v end |
#hash_key(value, options = {}) ⇒ Object #hash_key(options = {}) ⇒ Object
Define the hash key to be used in generating a hash from #objects
252 253 254 |
# File 'lib/flappi/definition.rb', line 252 def hash_key(*args, &block) @delegate.hash_key(*args, block) end |
#http_method(v) ⇒ Object
Define the HTTP method - note however that Rails and the controller is responsible for routing
305 306 307 |
# File 'lib/flappi/definition.rb', line 305 def http_method(v) endpoint_info[:http_method] = v end |
#link(: self) ⇒ Object #link(key, path) ⇒ Object #link(options = {}) ⇒ Object
Specify a link to be provided in the response Links defined at the top level will be output at the end of the response Links defined at object level will be output at the end of the object
297 298 299 |
# File 'lib/flappi/definition.rb', line 297 def link(*link_params) @delegate.link(*link_params) if @delegate.respond_to? :link end |
#major_version ⇒ Object
The major version for documentation and example purposes
476 477 478 |
# File 'lib/flappi/definition.rb', line 476 def major_version @delegate.requested_version&.major end |
#object(name) {|current_source| ... } ⇒ Object #object(name, value) {|current_source| ... } ⇒ Object #object(options) {|current_source| ... } ⇒ Object
Define an object (which will be rendered within json as name:hash). Use this with a block that defines the fields of the object hash. For a named object, if no source data exists (is nil) then no object will be rendered. (Set value: true if the block can render with no input)
163 164 165 |
# File 'lib/flappi/definition.rb', line 163 def object(*args_or_name, &block) @delegate.object(*args_or_name, block) end |
#objects(name, options = {}) {|current_source| ... } ⇒ Object #objects(name, value, options = {}) {|current_source| ... } ⇒ Object #objects(options) {|current_source| ... } ⇒ Object
Define multiple objects which will be rendered into json as an array of object hashes. Use this with a block that defines the fields of each object hash. The array is generated by iterating over the source object (if an Array) or with one field (if scalar).
202 203 204 |
# File 'lib/flappi/definition.rb', line 202 def objects(*args_or_name, &block) @delegate.objects(*args_or_name, block) end |
#param(name, options = {}) {|param| ... } ⇒ ParamProcessor #param(options = {}) {|param| ... } ⇒ ParamProcessor
Define an input parameter (inline or query string) This is used to document and validate the parameters.
Chain processor &block to this to define a parameter processor
and/or validator &block for define a parameter validator
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/flappi/definition.rb', line 420 def param(*args_or_name, &block) def_args = extract_definition_args(args_or_name) require_arg def_args, :name return unless version_wanted(def_args) param_def = { name: def_args[:name], type: name_for_type(def_args[:type]), default_doc: def_args[:default_doc], description: def_args[:doc], hidden: def_args[:hidden], optional: def_args.key?(:optional) ? def_args[:optional] : true, validation_block: block, fail_code: def_args[:fail_code] } # options that take nil param_def[:default] = def_args[:default] if def_args.key?(:default) endpoint_info[:params] << param_def ParamProcessor.new(param_def) end |
#path(v) ⇒ Object
Define the path to this endpoint under the API root. This is for documentation only.
342 343 344 |
# File 'lib/flappi/definition.rb', line 342 def path(v) endpoint_info[:path] = v end |
#query {|controller_params| ... } ⇒ Object
Define a query to be used to retrieve the source object for the response.
459 460 461 |
# File 'lib/flappi/definition.rb', line 459 def query(&block) @delegate.query(block) end |
#reference(name, options = {}) {|current_source| ... } ⇒ Object #reference(name, value, options = {}) ⇒ Object
Creates a sideloaded reference to an object created by the block. The object must include an ID field
280 281 282 |
# File 'lib/flappi/definition.rb', line 280 def reference(*args_or_name, &block) @delegate.reference(*args_or_name, block) end |
#request_example(*v) ⇒ Object
Define an example request path (no scheme or host) that will be included in documentation. If not specified, the #path is used.
355 356 357 |
# File 'lib/flappi/definition.rb', line 355 def request_example(*v) set_example('request', v) end |
#response_example(*v) ⇒ Object
Define an example response that will be included in documentation.
348 349 350 |
# File 'lib/flappi/definition.rb', line 348 def response_example(*v) set_example('response', v) end |
#return_error(status_code, error_info) ⇒ Object
From inside a query, return an error
471 472 473 |
# File 'lib/flappi/definition.rb', line 471 def return_error(status_code, error_info) @delegate.return_error(status_code, error_info) if @delegate.respond_to?(:return_error) end |
#return_no_content ⇒ Object
From inside a query, return 204 NO CONTENT
464 465 466 |
# File 'lib/flappi/definition.rb', line 464 def return_no_content @delegate.return_no_content if @delegate.respond_to?(:return_no_content) end |
#strict(mode = false) ⇒ Object
Enable/disable strict mode, unknown parameters will cause an error Default is to disable this
450 451 452 |
# File 'lib/flappi/definition.rb', line 450 def strict(mode = false) endpoint_info[:strict_mode] = mode end |
#title(v) ⇒ Object
Define the title
311 312 313 |
# File 'lib/flappi/definition.rb', line 311 def title(v) endpoint_info[:title] = v end |
#version(version_rule) ⇒ Object
Define the version this endpoint works with.
This assumes that a version plan (which defines semantic versioning and version flavours) is configured into Flappi.
327 328 329 330 331 |
# File 'lib/flappi/definition.rb', line 327 def version(version_rule) raise "No version plan is defined - cannot use 'version'" unless version_plan @version_rule = version_rule end |