# frozen_string_literal: true require 'rspec/rails/api/metadata' module RSpec module Rails module Api module DSL # All these methods will be available in example groups # (anything but 'it', 'example', 'for_code') module ExampleGroup # First method to be called in a spec file # as it will initialize the metadatas. def resource(name, description = '') metadata[:rrad] ||= Metadata.new metadata[:rrad].add_resource name, description end # Used to describe an entity def entity(type, fields) metadata[:rrad].add_entity type, fields end # Used to describe query parameters def path_params(fields) metadata[:rrad].add_path_params fields end def request_params(fields) metadata[:rrad].add_request_params fields end def on_get(url, description = nil, &block) on_action(:get, url, description, &block) end def on_post(url, description = nil, &block) on_action(:post, url, description, &block) end def on_put(url, description = nil, &block) on_action(:put, url, description, &block) end def on_patch(url, description = nil, &block) on_action(:patch, url, description, &block) end def on_delete(url, description = nil, &block) on_action(:delete, url, description, &block) end # Currently fill metadatas with the action def on_action(action, url, description, &block) metadata[:rrad].add_action(action, url, description) describe("#{action.upcase} #{url}", &block) end def for_code(status_code, description, doc_only: false, &block) metadata[:rrad].add_status_code(status_code, description) describe "-> #{status_code} - #{description}" do if (!ENV['DOC_ONLY'] || ENV['DOC_ONLY'] == 'false' || !doc_only) && block example 'Test and create documentation', caller: block.send(:caller) do instance_eval(&block) if block_given? end else document_only status_code end end end private def document_only(status_code) example 'Create documentation' do |example| parent_example = example.example_group request_params = prepare_request_params parent_example.parent.description set_request_example parent_example.metadata[:rrad], request_params, status_code end end end end end end end RSpec::Core::ExampleGroup.extend(RSpec::Rails::Api::DSL::ExampleGroup)