8
8
import java .io .File ;
9
9
10
10
public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
11
- protected String invokerPackage = "com.wordnik.client" ;
12
- protected String groupId = "com.wordnik" ;
13
- protected String artifactId = "swagger-client" ;
14
- protected String artifactVersion = "1.0.0" ;
11
+ protected String gemName = "swagger_client" ;
12
+ protected String moduleName = null ;
13
+ protected String libFolder = "lib" ;
15
14
16
15
public CodegenType getTag () {
17
16
return CodegenType .CLIENT ;
@@ -25,10 +24,18 @@ public String getHelp() {
25
24
return "Generates a Ruby client library." ;
26
25
}
27
26
27
+ /**
28
+ * Generate Ruby module name from the gem name, e.g. use "SwaggerClient" for "swagger_client".
29
+ */
30
+ public String generateModuleName () {
31
+ return camelize (gemName .replaceAll ("[^\\ w]+" , "_" ));
32
+ }
33
+
28
34
public RubyClientCodegen () {
29
35
super ();
30
- modelPackage = "models" ;
31
- apiPackage = "lib" ;
36
+ moduleName = generateModuleName ();
37
+ modelPackage = gemName + "/models" ;
38
+ apiPackage = gemName + "/api" ;
32
39
outputFolder = "generated-code/ruby" ;
33
40
modelTemplateFiles .put ("model.mustache" , ".rb" );
34
41
apiTemplateFiles .put ("api.mustache" , ".rb" );
@@ -39,17 +46,15 @@ public RubyClientCodegen() {
39
46
40
47
reservedWords = new HashSet <String > (
41
48
Arrays .asList (
42
- "__FILE__" , "and" , "def" , "end" , "in" , "or" , "self" , "unless" , "__LINE__" ,
49
+ "__FILE__" , "and" , "def" , "end" , "in" , "or" , "self" , "unless" , "__LINE__" ,
43
50
"begin" , "defined?" , "ensure" , "module" , "redo" , "super" , "until" , "BEGIN" ,
44
- "break" , "do" , "false" , "next" , "rescue" , "then" , "when" , "END" , "case" ,
51
+ "break" , "do" , "false" , "next" , "rescue" , "then" , "when" , "END" , "case" ,
45
52
"else" , "for" , "nil" , "retry" , "true" , "while" , "alias" , "class" , "elsif" ,
46
53
"if" , "not" , "return" , "undef" , "yield" )
47
54
);
48
55
49
- additionalProperties .put ("invokerPackage" , invokerPackage );
50
- additionalProperties .put ("groupId" , groupId );
51
- additionalProperties .put ("artifactId" , artifactId );
52
- additionalProperties .put ("artifactVersion" , artifactVersion );
56
+ additionalProperties .put ("gemName" , gemName );
57
+ additionalProperties .put ("moduleName" , moduleName );
53
58
54
59
languageSpecificPrimitives .add ("int" );
55
60
languageSpecificPrimitives .add ("array" );
@@ -64,15 +69,18 @@ public RubyClientCodegen() {
64
69
typeMapping .put ("List" , "array" );
65
70
typeMapping .put ("map" , "map" );
66
71
67
- supportingFiles .add (new SupportingFile ("swagger-client.gemspec.mustache" , "" , "swagger-client.gemspec" ));
68
- supportingFiles .add (new SupportingFile ("swagger-client.mustache" , "" , "lib/swagger-client.rb" ));
69
- supportingFiles .add (new SupportingFile ("swagger.mustache" , "" , "lib/swagger.rb" ));
70
- supportingFiles .add (new SupportingFile ("monkey.mustache" , "" , "lib/monkey.rb" ));
71
- supportingFiles .add (new SupportingFile ("swagger/request.mustache" , "" , "lib/swagger/request.rb" ));
72
- supportingFiles .add (new SupportingFile ("swagger/response.mustache" , "" , "lib/swagger/response.rb" ));
73
- supportingFiles .add (new SupportingFile ("swagger/version.mustache" , "" , "lib/swagger/version.rb" ));
74
- supportingFiles .add (new SupportingFile ("swagger/configuration.mustache" , "" , "lib/swagger/configuration.rb" ));
75
- supportingFiles .add (new SupportingFile ("base_object.mustache" , "" , "models/base_object.rb" ));
72
+ String baseFolder = "lib/" + gemName ;
73
+ String swaggerFolder = baseFolder + "/swagger" ;
74
+ String modelFolder = baseFolder + "/models" ;
75
+ supportingFiles .add (new SupportingFile ("swagger_client.gemspec.mustache" , "" , gemName + ".gemspec" ));
76
+ supportingFiles .add (new SupportingFile ("swagger_client.mustache" , "lib" , gemName + ".rb" ));
77
+ supportingFiles .add (new SupportingFile ("monkey.mustache" , baseFolder , "monkey.rb" ));
78
+ supportingFiles .add (new SupportingFile ("swagger.mustache" , baseFolder , "swagger.rb" ));
79
+ supportingFiles .add (new SupportingFile ("swagger/request.mustache" , swaggerFolder , "request.rb" ));
80
+ supportingFiles .add (new SupportingFile ("swagger/response.mustache" , swaggerFolder , "response.rb" ));
81
+ supportingFiles .add (new SupportingFile ("swagger/version.mustache" , swaggerFolder , "version.rb" ));
82
+ supportingFiles .add (new SupportingFile ("swagger/configuration.mustache" , swaggerFolder , "configuration.rb" ));
83
+ supportingFiles .add (new SupportingFile ("base_object.mustache" , modelFolder , "base_object.rb" ));
76
84
}
77
85
78
86
@ Override
@@ -82,11 +90,11 @@ public String escapeReservedWord(String name) {
82
90
83
91
@ Override
84
92
public String apiFileFolder () {
85
- return outputFolder + "/ " + apiPackage (). replace ( '.' , File .separatorChar ) ;
93
+ return outputFolder + File . separatorChar + "lib " + File . separatorChar + gemName + File .separatorChar + "api" ;
86
94
}
87
95
88
96
public String modelFileFolder () {
89
- return outputFolder + "/ " + modelPackage (). replace ( '.' , File .separatorChar ) ;
97
+ return outputFolder + File . separatorChar + "lib " + File . separatorChar + gemName + File .separatorChar + "models" ;
90
98
}
91
99
92
100
@ Override
@@ -150,13 +158,13 @@ public String toParamName(String name) {
150
158
// should be the same as variable name
151
159
return toVarName (name );
152
160
}
153
-
161
+
154
162
@ Override
155
163
public String toModelName (String name ) {
156
164
// model name cannot use reserved keyword, e.g. return
157
165
if (reservedWords .contains (name ))
158
166
throw new RuntimeException (name + " (reserved word) cannot be used as a model name" );
159
-
167
+
160
168
// camelize the model name
161
169
// phone_number => PhoneNumber
162
170
return camelize (name );
@@ -167,11 +175,11 @@ public String toModelFilename(String name) {
167
175
// model name cannot use reserved keyword, e.g. return
168
176
if (reservedWords .contains (name ))
169
177
throw new RuntimeException (name + " (reserved word) cannot be used as a model name" );
170
-
178
+
171
179
// underscore the model file name
172
180
// PhoneNumber.rb => phone_number.rb
173
181
return underscore (name );
174
- }
182
+ }
175
183
176
184
@ Override
177
185
public String toApiFilename (String name ) {
@@ -186,7 +194,7 @@ public String toApiFilename(String name) {
186
194
public String toApiName (String name ) {
187
195
if (name .length () == 0 )
188
196
return "DefaultApi" ;
189
- // e.g. phone_number_api => PhoneNumberApi
197
+ // e.g. phone_number_api => PhoneNumberApi
190
198
return camelize (name ) + "Api" ;
191
199
}
192
200
@@ -196,8 +204,17 @@ public String toOperationId(String operationId) {
196
204
if (reservedWords .contains (operationId ))
197
205
throw new RuntimeException (operationId + " (reserved word) cannot be used as method name" );
198
206
199
- return underscore (operationId );
207
+ return underscore (operationId );
200
208
}
201
209
210
+ @ Override
211
+ public String toModelImport (String name ) {
212
+ return modelPackage () + "/" + toModelFilename (name );
213
+ }
214
+
215
+ @ Override
216
+ public String toApiImport (String name ) {
217
+ return apiPackage () + "/" + toApiFilename (name );
218
+ }
202
219
203
220
}
0 commit comments