Skip to content

Commit

Permalink
Adds generating of desc block.
Browse files Browse the repository at this point in the history
- adds CHANGELOG entry
  • Loading branch information
LeFnord committed Oct 22, 2023
1 parent 676ffa1 commit 72e71db
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
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
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 72e71db

Please sign in to comment.