Skip to content

Commit

Permalink
added executable with command line options
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario.RuizSanchez committed Feb 11, 2019
1 parent 6b8534b commit d544a1a
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
38 changes: 37 additions & 1 deletion README.md
Expand Up @@ -57,7 +57,43 @@ Take in consideration open_api_import gem is using the 'rufo' gem that executes

## Usage

You have all the json and yaml examples that the Open API project supplies on /spec/fixtures/ folder. You can use any of those ones or your own Swagger or Open API file. To convert the Swagger or Open API file into a Request Hash:
After installation you can run using command line executable or importing from Ruby.

You have all the json and yaml examples that the Open API project supplies on /spec/fixtures/ folder. To test it you can use any of those ones or your own Swagger or Open API file.

### Executable

For help and see the options, run in command line / bash: `open_api_import -h`

Example:
```bash
open_api_import ./spec/fixtures/v2.0/yaml/uber.yaml -fp
```

This is the output:

```
Usage: open_api_import [open_api_file] [options]
Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses.
More info: https://github.com/MarioRuiz/open_api_import
In case no options supplied:
* It will be used the value of operation_id on snake_case for the name of the methods
* It will be used the first folder of the path to create the module name
-n, --no_responses if you don't want to add the examples of responses in the resultant file.
-m, --mock Add the first response on the request as mock_response
-p, --path_method it will be used the path and http method to create the method names
-o, --operationId_method It will be used the operationId field like it is to create the method names
-f, --create_files It will create a file per module
-T, --tags_module It will be used the tags key to create the module name
-F, --fixed_module all the requests will be under the module Requests
```


### Ruby file
Write your ruby code on a file and in command line/bash: `ruby my_file.rb`

To convert the Swagger or Open API file into a Request Hash:

```ruby
require 'open_api_import'
Expand Down
65 changes: 65 additions & 0 deletions bin/open_api_import
@@ -0,0 +1,65 @@
#!/usr/bin/env ruby
require 'optparse'
require 'open_api_import'

options = {
name_for_module: :path
}

optparse = OptionParser.new do |opts|
opts.banner = "Usage: open_api_import [open_api_file] [options]\n"
opts.banner+= "Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses.\n"
opts.banner+= "More info: https://github.com/MarioRuiz/open_api_import\n\n"
opts.banner+= "In case no options supplied: \n"
opts.banner+= " * It will be used the value of operation_id on snake_case for the name of the methods\n"
opts.banner+= " * It will be used the first folder of the path to create the module name\n"

opts.on("-n", "--no_responses", "if you don't want to add the examples of responses in the resultant file.") do
options[:include_responses] = false
end

opts.on("-m", "--mock", "Add the first response on the request as mock_response") do
options[:mock_response] = true
end

opts.on("-p", "--path_method", "it will be used the path and http method to create the method names") do
options[:create_method_name] = :path
end

opts.on("-o", "--operationId_method", "It will be used the operationId field like it is to create the method names") do
options[:create_method_name] = :operationId
end

opts.on("-f", "--create_files", "It will create a file per module") do
options[:create_files] = true
end

opts.on("-T", "--tags_module", "It will be used the tags key to create the module name") do
options[:name_for_module] = :tags
end

opts.on("-F", "--fixed_module", "all the requests will be under the module Requests") do
options[:name_for_module] = :fixed
end


end

optparse.parse!

if options.key?(:create_files)
if options[:name_for_module] == :path
options[:name_for_module] = :path_file
elsif options[:name_for_module] == :tags
options[:name_for_module] = :tags_file
end
options.delete(:create_files)
end

filename = ARGV.pop
if filename.to_s==''
puts optparse
puts "** Need to specify at least a file to import."
else
OpenApiImport.from filename, options
end
8 changes: 6 additions & 2 deletions lib/open_api_import.rb
Expand Up @@ -17,7 +17,7 @@ class OpenApiImport
# path: it will be used the path and http method, for example for a GET on path: /users/list, the method name will be get_users_list
# operation_id: it will be used the operationId field but using the snake_case version, for example for listUsers: list_users
# operationId: it will be used the operationId field like it is, for example: listUsers
# @param name_for_module [Symbol]. (:path, :path_file, :fixed) (default: :path). How the module names will be created.
# @param name_for_module [Symbol]. (:path, :path_file, :fixed, :tags, :tags_file) (default: :path). How the module names will be created.
# path: It will be used the first folder of the path to create the module name, for example the path /users/list will be in the module Users and all the requests from all modules in the same file.
# path_file: It will be used the first folder of the path to create the module name, for example the path /users/list will be in the module Users and each module will be in a new requests file.
# tags: It will be used the tags key to create the module name, for example the tags: [users,list] will create the module UsersList and all the requests from all modules in the same file.
Expand Down Expand Up @@ -443,7 +443,11 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
puts message
@logger.info message
else
files[module_requests] = output #for the last one
unless files.key?(module_requests)
files[module_requests] = Array.new
end
files[module_requests].concat(output) #for the last one
requires_txt = ""
message = "** Generated files that contain the code of the requests after importing the Swagger file: "
puts message
Expand Down
5 changes: 3 additions & 2 deletions open_api_import.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'open_api_import'
s.version = '0.3.0'
s.version = '0.4.1'
s.summary = "OpenApiImport -- Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses with all the examples. The file can be in JSON or YAML"
s.description = "OpenApiImport -- Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses with all the examples. The file can be in JSON or YAML"
s.authors = ["Mario Ruiz"]
Expand All @@ -11,11 +11,12 @@ Gem::Specification.new do |s|
s.license = 'MIT'
s.add_runtime_dependency 'oas_parser', '~> 0.15', '>= 0.15.1'
s.add_runtime_dependency 'rufo', '~> 0.4', '>= 0.4.1'
s.add_runtime_dependency 'nice_hash', ['>= 1.8.1']
s.add_runtime_dependency 'nice_hash', '~> 1.9', '>= 1.9.0'
s.add_development_dependency 'rspec', '~> 3.8', '>= 3.8.0'
s.add_development_dependency 'coveralls', '~> 0.8', '>= 0.8.22'
s.test_files = s.files.grep(%r{^(test|spec|features)/})
s.require_paths = ["lib"]
s.executables << 'open_api_import'
s.required_ruby_version = '>= 2.3'
s.post_install_message = "Thanks for installing! Visit us on https://github.com/MarioRuiz/open_api_import"
end
Expand Down

0 comments on commit d544a1a

Please sign in to comment.