Skip to content

Conversation

xhh
Copy link
Contributor

@xhh xhh commented Apr 20, 2015

This PR is to make the folder/file structure of the generated Ruby code comply with the conventions of Ruby gems.

Taking Petstore as an example:

$ tree lib
lib
├── swagger_client
│   ├── api
│   │   ├── pet_api.rb
│   │   ├── store_api.rb
│   │   └── user_api.rb
│   ├── models
│   │   ├── category.rb
│   │   ├── order.rb
│   │   ├── pet.rb
│   │   ├── tag.rb
│   │   └── user.rb
│   ├── monkey.rb
│   ├── swagger
│   │   ├── configuration.rb
│   │   ├── request.rb
│   │   ├── response.rb
│   │   └── version.rb
│   └── swagger.rb
└── swagger_client.rb

1. Place all files in the lib folder
As suggested by RubyGems Basics:

The lib directory itself normally contains only one .rb file and a directory with the same name as the gem which contains the rest of the files.

2. Move source files in an enclosing namespace module to avoid potential conflicts
This is a practice used by most gems (e.g. stripe-ruby).
As in the Petstore sample, use SwaggerClient::PetApi and SwaggerClient::Pet to access API and model classes.

3. Rename gem name of Petstore sample from swagger-client to swagger_client
according to the naming conventions from RubyGems guides.

@fehguy
Copy link
Contributor

fehguy commented Apr 21, 2015

Hi @xhh thank you for this. I'd like to avoid the importPath in CodegenModel. Can you explain that a bit more?

@xhh
Copy link
Contributor Author

xhh commented Apr 21, 2015

@fehguy that is for the require statement to require/import all the models and apis, for example the petstore sample:

# Models
require 'swagger_client/models/user'
require 'swagger_client/models/category'
require 'swagger_client/models/pet'
require 'swagger_client/models/tag'
require 'swagger_client/models/order'

# APIs
require 'swagger_client/api/user_api'
require 'swagger_client/api/pet_api'
require 'swagger_client/api/store_api'

The template generating the above is:

# Models
{{#models}}
{{#model}}
require '{{importPath}}'
{{/model}}
{{/models}}

# APIs
{{#apiInfo}}
{{#apis}}
require '{{importPath}}'
{{/apis}}
{{/apiInfo}}

The models variable is used here to generate all model requires, which need to set the actual require path, i.e. importPath. For apis, the importPath key is added to the corresponding Map.

Any other ways to generate the require/import paths for all models? If there are I'm happy to make the changes.

@xhh
Copy link
Contributor Author

xhh commented Apr 23, 2015

There is another way to require ruby files dynamically, similar to the PHP approach, which does not need to iterate the list of models/apis (so the importPath is not needed).

If that approach looks OK I can make the change. However, I'd like to know the concerns of avoiding importPath here. Is there a plan to decommission import, or because importPath is not used by codegen of other languages?

@xhh
Copy link
Contributor Author

xhh commented May 5, 2015

@fehguy let me know what else needs to be done for this if you have time

@xhh
Copy link
Contributor Author

xhh commented May 18, 2015

just rebased this

@wing328
Copy link
Contributor

wing328 commented May 19, 2015

Could build and install the Petstore Ruby gem locally without any issue/warning.
Tested with get_pet_by_id using the local 'swagger-client' gem without issue.

@csaunders
Copy link

Here's a note about something I noticed while playing with swagger codegen:

A bit of a concern with the current Ruby API consumer is that the generated libraries aren't thread-safe. An easy way would be to move the configuration values over to thread locals, but a better thing would be a configuration object or client that is initialized instead.

The result is perhaps a client that requires a bit more setup but it gets rid of the class methods that rely on shared global state.


I just did a cursory look through the patch and the resulting code seems more ruby-esque which is super awesome.

@fehguy
Copy link
Contributor

fehguy commented May 19, 2015

Hi @xhh I'd like to merge this but looking to see if we can avoid modifying the CodegenModel

@xhh
Copy link
Contributor Author

xhh commented May 20, 2015

@fehguy i managed to avoid modifying CodegenModel by setting the key to the models item in this commit: 312f1c6

fehguy added a commit that referenced this pull request May 20, 2015
Organize the generated Ruby code into a module structure
@fehguy fehguy merged commit deca6a0 into swagger-api:develop_2.0 May 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants