Skip to content

Commit

Permalink
added :tags and :tags_file for name_for_module param
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario.RuizSanchez committed Feb 6, 2019
1 parent 21ce577 commit 6b8534b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 19 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,16 @@ The output will generate methods like this:

How the module names will be created.

Accepts three different options: :path, :path_file and :fixed. By default :path.
Accepts five different options: :path, :path_file, :tags, :tags_file and :fixed. By default :path.

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. In case the tags are equal to the beginning of the operationId then it will be removed from the method name.

tags_file: It will be used the tags key to create the module name, for example the tags: \[users, list] will create the module UsersList and and each module will be in a new requests file. In case the tags are equal to the beginning of the operationId then it will be removed from the method name.

fixed: all the requests will be under the module Requests

```ruby
Expand Down Expand Up @@ -280,6 +284,37 @@ This is the output of the run:
- /petstore-simple.yaml.rb
```

In case using :tags

```ruby
require 'open_api_import'

OpenApiImport.from "./spec/fixtures/v2.0/yaml/uber.yaml", name_for_module: :tags, create_method_name: :path

```

It will generate just one file including every request under the module generated from the first folder of the path

```ruby
module Swagger
module UberApi
module V1_0_0
module Products

# operationId: unknown, method: get
# summary: Product Types
# description:
# The Products endpoint returns information about the Uber products offered at a given location.
# The response includes the display name and other details about each product, and lists the products in the proper display order.
# parameters description:
# latitude: (number) (required) Latitude component of location.
# longitude: (number) (required) Longitude component of location.
def self.get_products(latitude, longitude)
...
...
```


### include_responses

If you want to add the examples of responses in the resultant file.
Expand Down
51 changes: 34 additions & 17 deletions lib/open_api_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,39 +134,56 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
method_name = (met.to_s + "_" + path.path.to_s).snake_case
method_name.chop! if method_name[-1] == "_"
elsif create_method_name == :operation_id
method_name = (cont[:operationId]).to_s.snake_case
if (name_for_module == :tags or name_for_module == :tags_file) and cont.key?(:tags) and cont[:tags].is_a?(Array) and cont[:tags].size>0
metnametmp = cont[:operationId].gsub(/^#{cont[:tags].join}[\s_]*/, '')
else
metnametmp = cont[:operationId]
end
method_name = metnametmp.to_s.snake_case
else
method_name = cont[:operationId]
if (name_for_module == :tags or name_for_module == :tags_file) and cont.key?(:tags) and cont[:tags].is_a?(Array) and cont[:tags].size>0
method_name = cont[:operationId].gsub(/^#{cont[:tags].join}[\s_]*/, '')
else
method_name = cont[:operationId]
end
end

path_txt = path.path.dup.to_s
if name_for_module == :path or name_for_module == :path_file
if [:path, :path_file, :tags, :tags_file].include?(name_for_module)
old_module_requests = module_requests
# to remove version from path fex: /v1/Customer
path_requests = path_txt.gsub(/^\/v[\d\.]*\//i, "")
# to remove version from path fex: /1.0/Customer
path_requests = path_requests.gsub(/^\/[\d\.]*\//i, "")
if (path_requests == path_txt) && (path_txt.scan("/").size == 1)
# no folder in path
module_requests = "Root"
if [:path, :path_file].include?(name_for_module)
# to remove version from path fex: /v1/Customer
path_requests = path_txt.gsub(/^\/v[\d\.]*\//i, "")
# to remove version from path fex: /1.0/Customer
path_requests = path_requests.gsub(/^\/[\d\.]*\//i, "")
if (path_requests == path_txt) && (path_txt.scan("/").size == 1)
# no folder in path
module_requests = "Root"
else
res_path = path_requests.scan(/(\w+)/)
module_requests = res_path[0][0].camel_case
end
else
res_path = path_requests.scan(/(\w+)/)
module_requests = res_path[0][0].camel_case
if cont.key?(:tags) and cont[:tags].is_a?(Array) and cont[:tags].size>0
module_requests = cont[:tags].join(" ").camel_case
else
module_requests = "Unknown"
end
end
if old_module_requests != module_requests
output << "end" unless old_module_requests == "" or name_for_module == :path_file
if name_for_module == :path
output << "end" unless old_module_requests == "" or name_for_module == :path_file or name_for_module == :tags_file
if name_for_module == :path or name_for_module == :tags
# to add the end for the previous module unless is the first one
output << "module #{module_requests}"
else #:path_file
else #:path_file, :tags_file
if old_module_requests != ""
unless files.key?(old_module_requests)
files[old_module_requests] = Array.new
end
files[old_module_requests].concat(output)
output = Array.new
end
output << "module #{module_requests}" unless files.key?(module_requests) # dont add in case already existed
output << "module #{module_requests}" unless files.key?(module_requests) # don't add in case already existed
end
end
end
Expand Down Expand Up @@ -413,7 +430,7 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
output_footer = []
output_footer << "end" unless (module_requests == "") && (name_for_module == :path or name_for_module == :path_file)
output_footer << "end" unless (module_requests == "") && ([:path, :path_file, :tags, :tags_file].include?(name_for_module))
output_footer << "end" << "end" << "end"
if files.size == 0
Expand Down
2 changes: 1 addition & 1 deletion open_api_import.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'open_api_import'
s.version = '0.2.3'
s.version = '0.3.0'
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 Down
32 changes: 32 additions & 0 deletions spec/open_api_import/open_api_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
regexp = /module\sPets$/
expect(File.read("#{file_name}.rb")).to match regexp
end

it 'creates the module names correctly when name_for_module is :tags' do
file_name = './spec/fixtures/v2.0/yaml/uber.yaml'
OpenApiImport.from file_name, name_for_module: :tags
regexp = /module\sProducts$/
expect(File.read("#{file_name}.rb")).to match regexp
end

it 'creates the file names correctly when name_for_module is :path_file' do
file_name = './spec/fixtures/v2.0/yaml/petstore-simple.yaml'
Expand All @@ -65,13 +72,29 @@
expect(File.exist?("#{file_name}.rb")).to eq true
end

it 'creates the file names correctly when name_for_module is :tags_file' do
file_name = './spec/fixtures/v2.0/yaml/uber.yaml'
OpenApiImport.from file_name, name_for_module: :tags_file
expect(File.exist?("#{file_name}_Products.rb")).to eq true
expect(File.exist?("#{file_name}_Estimates.rb")).to eq true
expect(File.exist?("#{file_name}_User.rb")).to eq true
end

it 'creates the module names correctly when name_for_module is :path_file' do
file_name = './spec/fixtures/v2.0/yaml/petstore-simple.yaml'
OpenApiImport.from file_name, name_for_module: :path_file
expect(File.read("#{file_name}_Pets.rb")).to match /module\sPets$/
expect(File.read("#{file_name}_Root.rb")).to match /module\sRoot$/
end

it 'creates the module names correctly when name_for_module is :tags_file' do
file_name = './spec/fixtures/v2.0/yaml/uber.yaml'
OpenApiImport.from file_name, name_for_module: :tags_file
expect(File.read("#{file_name}_Products.rb")).to match /module\sProducts$/
expect(File.read("#{file_name}_Estimates.rb")).to match /module\sEstimates$/
expect(File.read("#{file_name}_User.rb")).to match /module\sUser$/
end

it 'creates a file that requires all request files when name_for_module is :path_file' do
file_name = './spec/fixtures/v2.0/yaml/petstore-simple.yaml'
OpenApiImport.from file_name, name_for_module: :path_file
Expand All @@ -80,6 +103,15 @@
expect(content).to include 'require_relative "petstore-simple.yaml_Pets"'
end

it 'creates a file that requires all request files when name_for_module is :tags_file' do
file_name = './spec/fixtures/v2.0/yaml/uber.yaml'
OpenApiImport.from file_name, name_for_module: :tags_file
content = File.read("#{file_name}.rb")
expect(content).to include 'require_relative "uber.yaml_Products"'
expect(content).to include 'require_relative "uber.yaml_Estimates"'
expect(content).to include 'require_relative "uber.yaml_User"'
end

it 'logs warning when unsupported http method is on the swagger file' do
file_name = './spec/fixtures/wrong/petstore-minimal_not_supported_method.yaml'
OpenApiImport.from file_name
Expand Down

0 comments on commit 6b8534b

Please sign in to comment.