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/import_spec.rb b/spec/lib/import_spec.rb index 7676d2b..c0d0e64 100644 --- a/spec/lib/import_spec.rb +++ b/spec/lib/import_spec.rb @@ -1,15 +1,15 @@ # frozen_string_literal: false RSpec.describe Starter::Import do - # describe '.do_it!' do - # let(:path) { './spec/fixtures/links.json' } + describe '.do_it!' do + let(:path) { './spec/fixtures/nested.json' } - # subject { described_class.do_it!(path) } + subject { described_class.do_it!(path) } - # specify do - # subject - # end - # end + specify do + subject + end + end describe '.load_spec' do subject { described_class.load_spec(path) } 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' }"