Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds generating of desc block. #41

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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