Skip to content

Commit e2e3818

Browse files
authored
Merge pull request #765 from swagger-api/timward60-timward/ruby-client-codegen
Timward60 timward/ruby client codegen
2 parents ab41c1e + 585f9cc commit e2e3818

32 files changed

+3417
-0
lines changed

src/main/java/io/swagger/codegen/v3/generators/ruby/RubyClientCodegen.java

Lines changed: 747 additions & 0 deletions
Large diffs are not rendered by default.

src/main/resources/META-INF/services/io.swagger.codegen.v3.CodegenConfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ io.swagger.codegen.v3.generators.php.PhpClientCodegen
2828
io.swagger.codegen.v3.generators.python.PythonClientCodegen
2929
io.swagger.codegen.v3.generators.python.PythonFlaskConnexionCodegen
3030
io.swagger.codegen.v3.generators.r.RClientCodegen
31+
io.swagger.codegen.v3.generators.ruby.RubyClientCodegen
3132
io.swagger.codegen.v3.generators.scala.ScalaClientCodegen
3233
io.swagger.codegen.v3.generators.scala.AkkaHttpServerCodegen
3334
io.swagger.codegen.v3.generators.swift.Swift3Codegen
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec
4+
5+
group :development, :test do
6+
gem 'rake', '~> 13.0.1'
7+
gem 'pry-byebug'
8+
gem 'rubocop', '~> 0.66.0'
9+
end
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# {{gemName}}
2+
3+
{{moduleName}} - the Ruby gem for the {{appName}}
4+
5+
{{#appDescription}}
6+
{{{appDescription}}}
7+
{{/appDescription}}
8+
9+
This SDK is automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project:
10+
11+
- API version: {{appVersion}}
12+
- Package version: {{gemVersion}}
13+
{{^hideGenerationTimestamp}}
14+
- Build date: {{generatedDate}}
15+
{{/hideGenerationTimestamp}}
16+
- Build package: {{generatorClass}}
17+
{{#infoUrl}}
18+
For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
19+
{{/infoUrl}}
20+
21+
## Installation
22+
23+
### Build a gem
24+
25+
To build the Ruby code into a gem:
26+
27+
```shell
28+
gem build {{{gemName}}}.gemspec
29+
```
30+
31+
Then either install the gem locally:
32+
33+
```shell
34+
gem install ./{{{gemName}}}-{{{gemVersion}}}.gem
35+
```
36+
(for development, run `gem install --dev ./{{{gemName}}}-{{{gemVersion}}}.gem` to install the development dependencies)
37+
38+
or publish the gem to a gem hosting service, e.g. [RubyGems](https://rubygems.org/).
39+
40+
Finally add this to the Gemfile:
41+
42+
gem '{{{gemName}}}', '~> {{{gemVersion}}}'
43+
44+
### Install from Git
45+
46+
If the Ruby gem is hosted at a git repository: https://github.com/{{#gitUserId}}{{.}}{{/gitUserId}}{{^gitUserId}}YOUR_GIT_USERNAME{{/gitUserId}}/{{#gitRepoId}}{{.}}{{/gitRepoId}}{{^gitRepoId}}YOUR_GIT_REPO{{/gitRepoId}}, then add the following in the Gemfile:
47+
48+
gem '{{{gemName}}}', :git => 'https://github.com/{{#gitUserId}}{{.}}{{/gitUserId}}{{^gitUserId}}YOUR_GIT_USERNAME{{/gitUserId}}/{{#gitRepoId}}{{.}}{{/gitRepoId}}{{^gitRepoId}}YOUR_GIT_REPO{{/gitRepoId}}.git'
49+
50+
### Include the Ruby code directly
51+
52+
Include the Ruby code directly using `-I` as follows:
53+
54+
```shell
55+
ruby -Ilib script.rb
56+
```
57+
58+
## Getting Started
59+
60+
Please follow the [installation](#installation) procedure and then run the following code:
61+
```ruby
62+
# Load the gem
63+
require '{{{gemName}}}'
64+
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}{{#hasAuthMethods}}
65+
# Setup authorization
66+
{{{moduleName}}}.configure do |config|{{#authMethods}}{{#isBasic}}
67+
# Configure HTTP basic authorization: {{{name}}}
68+
config.username = 'YOUR USERNAME'
69+
config.password = 'YOUR PASSWORD'{{/isBasic}}{{#isApiKey}}
70+
# Configure API key authorization: {{{name}}}
71+
config.api_key['{{{keyParamName}}}'] = 'YOUR API KEY'
72+
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
73+
#config.api_key_prefix['{{{keyParamName}}}'] = 'Bearer'{{/isApiKey}}{{#isOAuth}}
74+
# Configure OAuth2 access token for authorization: {{{name}}}
75+
config.access_token = 'YOUR ACCESS TOKEN'{{/isOAuth}}
76+
{{/authMethods}}end
77+
{{/hasAuthMethods}}
78+
79+
api_instance = {{{moduleName}}}::{{{classname}}}.new{{#hasParams}}
80+
{{#vendorExtensions.x-codegen-hasRequiredParams}}{{#allParams}}{{#required}}
81+
{{{paramName}}} = {{{example}}} # {{{dataType}}} | {{{description}}}
82+
{{/required}}{{/allParams}}{{/vendorExtensions.x-codegen-hasRequiredParams}}{{#hasOptionalParams}}
83+
opts = { {{#allParams}}{{^required}}
84+
{{{paramName}}}: {{{example}}}{{#vendorExtensions.x-codegen-hasMoreOptional}},{{/vendorExtensions.x-codegen-hasMoreOptional}} # {{{dataType}}} | {{{description}}}{{/required}}{{/allParams}}
85+
}{{/hasOptionalParams}}{{/hasParams}}
86+
87+
begin
88+
{{#summary}} #{{{.}}}
89+
{{/summary}} {{#returnType}}result = {{/returnType}}api_instance.{{{operationId}}}{{#hasParams}}({{#allParams}}{{#required}}{{{paramName}}}{{#vendorExtensions.x-codegen-hasMoreRequired}}, {{/vendorExtensions.x-codegen-hasMoreRequired}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{{#vendorExtensions.x-codegen-hasRequiredParams}}, {{/vendorExtensions.x-codegen-hasRequiredParams}}opts{{/hasOptionalParams}}){{/hasParams}}{{#returnType}}
90+
p result{{/returnType}}
91+
rescue {{{moduleName}}}::ApiError => e
92+
puts "Exception when calling {{classname}}->{{{operationId}}}: #{e}"
93+
end
94+
{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
95+
```
96+
97+
## Documentation for API Endpoints
98+
99+
All URIs are relative to *{{basePath}}*
100+
101+
Class | Method | HTTP request | Description
102+
------------ | ------------- | ------------- | -------------
103+
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}*{{moduleName}}::{{classname}}* | [**{{operationId}}**]({{apiDocPath}}{{classname}}.md#{{operationId}}) | **{{httpMethod}}** {{path}} | {{#summary}}{{{summary}}}{{/summary}}
104+
{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
105+
106+
## Documentation for Models
107+
108+
{{#models}}{{#model}} - [{{moduleName}}::{{classname}}]({{modelDocPath}}{{classname}}.md)
109+
{{/model}}{{/models}}
110+
111+
## Documentation for Authorization
112+
113+
{{^authMethods}} All endpoints do not require authorization.
114+
{{/authMethods}}{{#authMethods}}{{#last}} Authentication schemes defined for the API:{{/last}}{{/authMethods}}
115+
{{#authMethods}}### {{name}}
116+
117+
{{#isApiKey}}- **Type**: API key
118+
- **API key parameter name**: {{keyParamName}}
119+
- **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}}
120+
{{/isApiKey}}
121+
{{#isBasic}}- **Type**: HTTP basic authentication
122+
{{/isBasic}}
123+
{{#isOAuth}}- **Type**: OAuth
124+
- **Flow**: {{flow}}
125+
- **Authorization URL**: {{authorizationUrl}}
126+
- **Scopes**: {{^scopes}}N/A{{/scopes}}
127+
{{#scopes}} - {{scope}}: {{description}}
128+
{{/scopes}}
129+
{{/isOAuth}}
130+
131+
{{/authMethods}}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
begin
2+
require 'rspec/core/rake_task'
3+
4+
RSpec::Core::RakeTask.new(:spec)
5+
task default: :spec
6+
rescue LoadError
7+
# no rspec available
8+
end
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
=begin
2+
{{> api_info}}
3+
=end
4+
5+
module {{moduleName}}
6+
{{#operations}}
7+
class {{classname}}
8+
attr_accessor :api_client
9+
10+
def initialize(api_client = ApiClient.default)
11+
@api_client = api_client
12+
end
13+
{{#operation}}
14+
{{#contents}}
15+
{{#summary}}
16+
# {{{summary}}}
17+
{{/summary}}
18+
{{#notes}}
19+
# {{{notes}}}
20+
{{/notes}}
21+
{{#parameters}}{{#required}} # @param {{paramName}} {{description}}
22+
{{/required}}{{/parameters}} # @param [Hash] opts the optional parameters
23+
{{#parameters}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
24+
{{/required}}{{/parameters}} # @return [{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}]
25+
def {{operationId}}({{#parameters}}{{#required}}{{paramName}}, {{/required}}{{/parameters}}opts = {})
26+
{{#returnType}}data, _status_code, _headers = {{/returnType}}{{operationId}}_with_http_info({{#parameters}}{{#required}}{{paramName}}, {{/required}}{{/parameters}}opts)
27+
{{#returnType}}data{{/returnType}}{{^returnType}}nil{{/returnType}}
28+
end
29+
30+
{{#summary}}
31+
# {{summary}}
32+
{{/summary}}
33+
{{#notes}}
34+
# {{notes}}
35+
{{/notes}}
36+
{{#parameters}}{{#required}} # @param {{paramName}} {{description}}
37+
{{/required}}{{/parameters}} # @param [Hash] opts the optional parameters
38+
{{#parameters}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}
39+
{{/required}}{{/parameters}} # @return [Array<({{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}, Integer, Hash)>] {{#returnType}}{{{returnType}}} data{{/returnType}}{{^returnType}}nil{{/returnType}}, response status code and response headers
40+
def {{operationId}}_with_http_info({{#parameters}}{{#required}}{{paramName}}, {{/required}}{{/parameters}}opts = {})
41+
if @api_client.config.debugging
42+
@api_client.config.logger.debug 'Calling API: {{classname}}.{{operationId}} ...'
43+
end
44+
{{#parameters}}
45+
{{#required}}
46+
# verify the required parameter '{{paramName}}' is set
47+
if @api_client.config.client_side_validation && {{{paramName}}}.nil?
48+
fail ArgumentError, "Missing the required parameter '{{paramName}}' when calling {{classname}}.{{operationId}}"
49+
end
50+
{{#isEnum}}
51+
{{^isContainer}}
52+
# verify enum value
53+
if @api_client.config.client_side_validation && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}].include?({{{paramName}}})
54+
fail ArgumentError, "invalid value for '{{{paramName}}}', must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}"
55+
end
56+
{{/isContainer}}
57+
{{/isEnum}}
58+
{{/required}}
59+
{{^required}}
60+
{{#isEnum}}
61+
{{#collectionFormat}}
62+
if @api_client.config.client_side_validation && opts[:'{{{paramName}}}'] && !opts[:'{{{paramName}}}'].all? { |item| [{{#allowableValues}}{{#values}}'{{{this}}}'{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}].include?(item) }
63+
fail ArgumentError, 'invalid value for "{{{paramName}}}", must include one of {{#allowableValues}}{{#values}}{{{this}}}{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}'
64+
end
65+
{{/collectionFormat}}
66+
{{^collectionFormat}}
67+
if @api_client.config.client_side_validation && opts[:'{{{paramName}}}'] && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}].include?(opts[:'{{{paramName}}}'])
68+
fail ArgumentError, 'invalid value for "{{{paramName}}}", must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}'
69+
end
70+
{{/collectionFormat}}
71+
{{/isEnum}}
72+
{{/required}}
73+
{{#hasValidation}}
74+
{{#maxLength}}
75+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length > {{{maxLength}}}
76+
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, the character length must be smaller than or equal to {{{maxLength}}}.'
77+
end
78+
79+
{{/maxLength}}
80+
{{#minLength}}
81+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length < {{{minLength}}}
82+
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, the character length must be great than or equal to {{{minLength}}}.'
83+
end
84+
85+
{{/minLength}}
86+
{{#maximum}}
87+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
88+
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{{maximum}}}.'
89+
end
90+
91+
{{/maximum}}
92+
{{#minimum}}
93+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
94+
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must be greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{{minimum}}}.'
95+
end
96+
97+
{{/minimum}}
98+
{{#pattern}}
99+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} !~ Regexp.new({{{pattern}}})
100+
fail ArgumentError, "invalid value for '{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:\"{{{paramName}}}\"]{{/required}}' when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}."
101+
end
102+
103+
{{/pattern}}
104+
{{#maxItems}}
105+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.length > {{{maxItems}}}
106+
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{{maxItems}}}.'
107+
end
108+
109+
{{/maxItems}}
110+
{{#minItems}}
111+
if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.length < {{{minItems}}}
112+
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{{minItems}}}.'
113+
end
114+
115+
{{/minItems}}
116+
{{/hasValidation}}
117+
{{/parameters}}
118+
# resource path
119+
local_var_path = '{{{path}}}'{{#pathParams}}.sub('{' + '{{baseName}}' + '}', {{paramName}}.to_s){{/pathParams}}
120+
121+
# query parameters
122+
query_params = opts[:query_params] || {}
123+
{{#queryParams}}
124+
{{#required}}
125+
query_params[:'{{{baseName}}}'] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}}
126+
{{/required}}
127+
{{/queryParams}}
128+
{{#queryParams}}
129+
{{^required}}
130+
query_params[:'{{{baseName}}}'] = {{#collectionFormat}}@api_client.build_collection_param(opts[:'{{{paramName}}}'], :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}opts[:'{{{paramName}}}']{{/collectionFormat}} if !opts[:'{{{paramName}}}'].nil?
131+
{{/required}}
132+
{{/queryParams}}
133+
134+
# header parameters
135+
header_params = opts[:header_params] || {}
136+
{{#hasProduces}}
137+
# HTTP header 'Accept' (if needed)
138+
header_params['Accept'] = @api_client.select_header_accept([{{#produces}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/produces}}])
139+
{{/hasProduces}}
140+
{{#hasConsumes}}
141+
# HTTP header 'Content-Type'
142+
header_params['Content-Type'] = @api_client.select_header_content_type([{{#consumes}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}])
143+
{{/hasConsumes}}
144+
{{#headerParams}}
145+
{{#required}}
146+
header_params[:'{{{baseName}}}'] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}}
147+
{{/required}}
148+
{{/headerParams}}
149+
{{#headerParams}}
150+
{{^required}}
151+
header_params[:'{{{baseName}}}'] = {{#collectionFormat}}@api_client.build_collection_param(opts[:'{{{paramName}}}'], :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}opts[:'{{{paramName}}}']{{/collectionFormat}} if !opts[:'{{{paramName}}}'].nil?
152+
{{/required}}
153+
{{/headerParams}}
154+
155+
# form parameters
156+
form_params = opts[:form_params] || {}
157+
{{#formParams}}
158+
{{#required}}
159+
form_params['{{baseName}}'] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}}
160+
{{/required}}
161+
{{/formParams}}
162+
{{#formParams}}
163+
{{^required}}
164+
form_params['{{baseName}}'] = {{#collectionFormat}}@api_client.build_collection_param(opts[:'{{{paramName}}}'], :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}opts[:'{{{paramName}}}']{{/collectionFormat}} if !opts[:'{{paramName}}'].nil?
165+
{{/required}}
166+
{{/formParams}}
167+
168+
# http body (model)
169+
post_body = opts[:body] {{#bodyParam}}|| @api_client.object_to_http_body({{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}) {{/bodyParam}}
170+
171+
return_type = opts[:return_type] {{#returnType}}|| '{{{returnType}}}' {{/returnType}}
172+
173+
auth_names = opts[:auth_names] || [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}]
174+
data, status_code, headers = @api_client.call_api(:{{httpMethod}}, local_var_path,
175+
:header_params => header_params,
176+
:query_params => query_params,
177+
:form_params => form_params,
178+
:body => post_body,
179+
:auth_names => auth_names,
180+
:return_type => return_type)
181+
182+
if @api_client.config.debugging
183+
@api_client.config.logger.debug "API called: {{classname}}#{{operationId}}\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
184+
end
185+
return data, status_code, headers
186+
end
187+
{{/contents}}
188+
{{/operation}}
189+
end
190+
{{/operations}}
191+
end

0 commit comments

Comments
 (0)