Skip to content

Commit

Permalink
Adds generating of desc block.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeFnord committed Oct 22, 2023
1 parent 676ffa1 commit 10490d6
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 deletions.
26 changes: 26 additions & 0 deletions lib/starter/importer/description.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions lib/starter/importer/namespace.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rubocop:disable Metrics/AbcSize
# frozen_string_literal: false

module Starter
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -71,3 +75,5 @@ def allowed_verbs
end
end
end

# rubocop:enable Metrics/AbcSize
2 changes: 2 additions & 0 deletions lib/starter/importer/parameter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/starter/importer/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:)
Expand Down
14 changes: 7 additions & 7 deletions spec/lib/import_spec.rb
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down
12 changes: 6 additions & 6 deletions spec/lib/importer/parameter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' }"
Expand Down

0 comments on commit 10490d6

Please sign in to comment.