Skip to content

Commit 2a4fc7b

Browse files
committed
[API] Generator: Refactor, check for repeated paths
1 parent fd82ac4 commit 2a4fc7b

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

elasticsearch-api/utils/thor/generate_source.rb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ class SourceGenerator < Thor
3030
__root = Pathname( File.expand_path('../../..', __FILE__) )
3131

3232
desc "generate", "Generate source code and tests from the REST API JSON specification"
33-
method_option :force, type: :boolean, default: false, desc: 'Overwrite the output'
34-
method_option :verbose, type: :boolean, default: false, desc: 'Output more information'
35-
method_option :input, default: File.expand_path(SRC_PATH, __FILE__), desc: 'Path to directory with JSON API specs'
36-
method_option :output, default: File.expand_path('../../../tmp/out', __FILE__), desc: 'Path to output directory'
33+
method_option :force, type: :boolean, default: false, desc: 'Overwrite the output'
34+
method_option :verbose, type: :boolean, default: false, desc: 'Output more information'
35+
method_option :input, default: File.expand_path(SRC_PATH, __FILE__), desc: 'Path to directory with JSON API specs'
36+
method_option :output, default: File.expand_path('../../../tmp/out', __FILE__), desc: 'Path to output directory'
3737

3838
def generate(*files)
3939
self.class.source_root File.expand_path('../', __FILE__)
@@ -100,7 +100,7 @@ def generate(*files)
100100

101101
if options[:verbose]
102102
colorized_output = colorized_output = CodeRay.scan_file(@test_file, :ruby).terminal
103-
lines = colorized_output.split("\n")
103+
lines = colorized_output.split("\n")
104104
say_status 'ruby',
105105
lines.first + "\n" + lines[1, lines.size].map { |l| ' '*14 + l }.join("\n"),
106106
:yellow
@@ -122,32 +122,36 @@ def generate(*files)
122122
# Create the hierarchy of directories based on API namespaces
123123
#
124124
def __create_directories(key, value)
125-
unless value['documentation']
126-
empty_directory @output.join(key)
127-
create_directory_hierarchy *value.to_a.first
128-
end
125+
return if value['documentation']
126+
127+
empty_directory @output.join(key)
128+
create_directory_hierarchy * value.to_a.first
129129
end
130130

131131
# Extract parts from each path
132132
#
133133
def __endpoint_parts
134134
parts = @spec['url']['paths'].select do |a|
135135
a.keys.include?('parts')
136-
end.map do |part|
137-
part&.[]('parts')
136+
end.map do |path|
137+
path&.[]('parts')
138138
end
139-
(parts.first || [])
139+
(parts.inject(&:merge) || [])
140140
end
141141

142142
def __http_path
143143
return "\"#{__parse_path(@paths.first)}\"" if @paths.size == 1
144-
result = ''
145144

145+
result = ''
146+
anchor_string = ''
146147
@paths.sort { |a, b| b.length <=> a.length }.each_with_index do |path, i|
147148
var_string = __extract_path_variables(path).map { |var| "_#{var}" }.join(' && ')
148-
result += if i == 0
149+
next if anchor_string == var_string
150+
151+
anchor_string = var_string
152+
result += if i.zero?
149153
"if #{var_string}\n"
150-
elsif i == @paths.size - 1
154+
elsif (i == @paths.size - 1) || var_string.empty?
151155
"else\n"
152156
else
153157
"elsif #{var_string}\n"

elasticsearch-api/utils/thor/lister.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
require 'pathname'
1010

1111
module Elasticsearch
12-
1312
module API
14-
1513
class Lister < Thor
1614
namespace 'api'
1715

16+
DEFAULT_PATH = '../../tmp/elasticsearch/rest-api-spec/src/main/resources/rest-api-spec/api/'.freeze
17+
1818
desc "list <PATH DIRECTORY WITH JSON SPEC FILES>", "List all the REST API endpoints from the JSON specification"
1919
method_option :verbose, type: :boolean, default: false, desc: 'Output more information'
2020
method_option :format, default: 'text', desc: 'Output format (text, json)'
21-
def list(directory)
21+
def list(directory = DEFAULT_PATH)
2222
input = Pathname(directory).join('*.json')
2323
apis = Dir[input.to_s].map do |f|
2424
File.basename(f, '.json')
@@ -40,6 +40,5 @@ def list(directory)
4040
end
4141
end
4242
end
43-
4443
end
4544
end

0 commit comments

Comments
 (0)