@@ -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 "
0 commit comments