From 72e71dbf8a32118133dee349b53048805f8dc359 Mon Sep 17 00:00:00 2001 From: LeFnord Date: Sun, 22 Oct 2023 18:22:38 +0200 Subject: [PATCH] Adds generating of desc block. - adds CHANGELOG entry --- CHANGELOG.md | 3 ++- lib/starter/importer/description.rb | 26 ++++++++++++++++++++++++++ lib/starter/importer/namespace.rb | 6 ++++++ lib/starter/importer/parameter.rb | 2 ++ lib/starter/importer/specification.rb | 4 +++- spec/lib/importer/parameter_spec.rb | 12 ++++++------ 6 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 lib/starter/importer/description.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index cde88cb..570691b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,11 @@ ### NEXT +- [(#41)](https://github.com/LeFnord/grape-starter/pull/41) Adds generating of desc block [LeFnord](https://github.com/LeFnord) - contributions ### v2.0.0 / 2023-10-21 Imports OApi specs -- [(#38)](https://github.com/LeFnord/grape-starter/pull/38) Handles nested body [LeFnord](https://github.com/LeFnord) +- [(#39)](https://github.com/LeFnord/grape-starter/pull/39) Handles nested body [LeFnord](https://github.com/LeFnord) - [(#38)](https://github.com/LeFnord/grape-starter/pull/38) Handle parameters from request body [LeFnord](https://github.com/LeFnord) - [(#37)](https://github.com/LeFnord/grape-starter/pull/37) Ignores possible version segments in path [LeFnord](https://github.com/LeFnord) - Sets min ruby to 3.1 [LeFnord](https://github.com/LeFnord) diff --git a/lib/starter/importer/description.rb b/lib/starter/importer/description.rb new file mode 100644 index 0000000..011108d --- /dev/null +++ b/lib/starter/importer/description.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: false + +module Starter + module Importer + class Description + attr_accessor :content + + def initialize(content:) + @content = content + end + + def to_s + return if content.blank? + + desc = content['summary'] || name + + entry = "desc '#{desc}' do\n" + entry << " detail '#{content['description']}'\n" if content.key?('description') + entry << " tags #{content['tags']}\n" if content.key?('tags') + entry << 'end' + + entry + end + end + end +end diff --git a/lib/starter/importer/namespace.rb b/lib/starter/importer/namespace.rb index 4733f85..3cca7e1 100644 --- a/lib/starter/importer/namespace.rb +++ b/lib/starter/importer/namespace.rb @@ -1,3 +1,4 @@ +# rubocop:disable Metrics/AbcSize # frozen_string_literal: false module Starter @@ -39,10 +40,13 @@ def endpoints verbs.keys.each_with_object([]) do |verb, memo| next unless allowed_verbs.include?(verb) + memo << verbs[verb]['describe'].to_s if verbs[verb]['describe'].present? + if (parameters = verbs[verb]['parameters'].presence) params_block = params_block(parameters) memo << params_block end + memo << "#{verb} '#{segment}' do\n # your code comes here\nend\n" end end @@ -71,3 +75,5 @@ def allowed_verbs end end end + +# rubocop:enable Metrics/AbcSize diff --git a/lib/starter/importer/parameter.rb b/lib/starter/importer/parameter.rb index e709f96..c45ef1f 100644 --- a/lib/starter/importer/parameter.rb +++ b/lib/starter/importer/parameter.rb @@ -145,6 +145,8 @@ def serialized entry = definition['required'] ? 'requires' : 'optional' entry << " :#{name}" entry << ", type: #{type}" + entry << ", default: '#{definition['default']}'" if definition.key?('default') && definition['default'].present? + entry << ", values: #{definition['enum'].map(&:to_s)}" if definition.key?('enum') doc = documentation entry << ", #{doc}" if doc diff --git a/lib/starter/importer/specification.rb b/lib/starter/importer/specification.rb index ad56211..c548b10 100644 --- a/lib/starter/importer/specification.rb +++ b/lib/starter/importer/specification.rb @@ -58,7 +58,9 @@ def prepare_verbs(spec) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticCom memo[verb] = content next unless content.key?('parameters') || content.key?('requestBody') || path_params - parameters = ((content['parameters'] || path_params || []) + [content['requestBody']]).compact + parameters = ((content.delete('parameters') || path_params || []) + [content.delete('requestBody')]).compact + + memo[verb]['describe'] = Description.new(content: content.except('responses')) memo[verb]['parameters'] = parameters.each_with_object({}) do |definition, para| parameter = Parameter.new(definition:, components:) diff --git a/spec/lib/importer/parameter_spec.rb b/spec/lib/importer/parameter_spec.rb index df7eb82..0528327 100644 --- a/spec/lib/importer/parameter_spec.rb +++ b/spec/lib/importer/parameter_spec.rb @@ -232,8 +232,8 @@ expect(subject.definition.keys).to match_array %w[ required content in type enum description example ] - expect(subject.to_s).to eql( - "requires :mark, type: String, documentation: { desc: 'Possible values for a board square. `.` means empty square.', in: 'body' }" + expect(subject.to_s).to include( + "requires :mark, type: String, values: [\".\", \"X\", \"O\"], documentation: { desc: 'Possible values for a board square. `.` means empty square.', in: 'body' }" ) end end @@ -413,16 +413,16 @@ "optional :order, type: JSON, documentation: { desc: 'Specify result order', in: 'body' } do" ) expect(subject.to_s).to include( - "optional :facet, type: String, documentation: { in: 'body' }" + "optional :facet, type: String, default: 'score', values: [\"created_at\", \"updated_at\", \"random\"], documentation: { in: 'body' }" ) expect(subject.to_s).to include( - "optional :dir, type: String, documentation: { in: 'body' }" + "optional :dir, type: String, default: 'asc', values: [\"asc\", \"desc\"], documentation: { in: 'body' }" ) expect(subject.to_s).to include( - "optional :per_page, type: Integer, documentation: { in: 'body', format: 'int32' }" + "optional :per_page, type: Integer, default: '24', documentation: { in: 'body', format: 'int32' }" ) expect(subject.to_s).to include( - "optional :page, type: Integer, documentation: { in: 'body', format: 'int32' }" + "optional :page, type: Integer, default: '1', documentation: { in: 'body', format: 'int32' }" ) expect(subject.to_s).to include( "optional :choose, type: Array[String], documentation: { in: 'body' }"