Skip to content

Commit

Permalink
close #2
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRuiz committed Mar 4, 2020
1 parent 28a06c0 commit c52b9e7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ The output of this gem will be following the specification of Request Hashes: ht

The Request Hashes generated will be able to be used with any Ruby Http Client and it is adapted even better with nice_http gem: https://github.com/MarioRuiz/nice_http

A beautiful way to access and use the generated files is by using *pry*: https://github.com/pry/pry

To be able to generate random requests take a look at the documentation for nice_hash gem: https://github.com/MarioRuiz/nice_hash

This is an example of a generated request hash:
Expand Down Expand Up @@ -108,6 +110,7 @@ In case no options supplied:
-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
-s, --silent It will display only errors
-c, --create_constants For required arguments, it will create keyword arguments assigning by default a constant.
```


Expand Down Expand Up @@ -487,6 +490,14 @@ It will include this on the output file:
...
```

### create_constants

The methods will be generated using keyword arguments and for required arguments, it will create keyword arguments assigning by default a constant.

```ruby
def self.get_products(latitude: LATITUDE, longitude: LONGITUDE)
```

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/open_api_import.
Expand Down
4 changes: 4 additions & 0 deletions bin/open_api_import
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ optparse = OptionParser.new do |opts|
options[:silent] = true
end

opts.on("-c", "--create_constants", "For required arguments, it will create keyword arguments assigning by default a constant.") do
options[:create_constants] = true
end


end

Expand Down
60 changes: 48 additions & 12 deletions lib/open_api_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ class OpenApiImport
# 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, :tags, :tags_file) (default: :path). How the module names will be created.
# @param create_constants [Boolean]. (default: false) For required arguments, it will create keyword arguments assigning by default a constant.
# @param silent [Boolean]. (default: false) It will display only errors.
# 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.
# 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.
# fixed: all the requests will be under the module Requests
##############################################################################################
def self.from(swagger_file, create_method_name: :operation_id, include_responses: true, mock_response: false, name_for_module: :path, silent: false)
def self.from(swagger_file, create_method_name: :operation_id, include_responses: true, mock_response: false, name_for_module: :path, silent: false, create_constants: false)
begin
f = File.new("#{swagger_file}_open_api_import.log", "w")
f.sync = true
Expand Down Expand Up @@ -53,6 +54,7 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
file_errors = file_to_convert + ".errors.log"
File.delete(file_errors) if File.exist?(file_errors)
import_errors = ""
required_constants = []

begin
definition = OasParser::Definition.resolve(swagger_file)
Expand Down Expand Up @@ -278,7 +280,12 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
path_txt.gsub!("{#{p[:name]}}", "\#{#{param_name}}")
end
unless params_path.include?(param_name)
params_path << param_name
if create_constants
params_path << "#{param_name}: #{param_name.upcase}"
required_constants << param_name.upcase
else
params_path << param_name
end
#params_required << param_name if p[:required].to_s=="true"
description_parameters << "# #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s=="true"} #{p[:description]}"
end
Expand Down Expand Up @@ -400,13 +407,27 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
unless params_query.empty?
path_txt += "?"
params_required.each do |pr|
if params_query.include?(pr)
if create_method_name == :operationId
path_txt += "#{pr}=\#{#{pr}}&"
params << "#{pr}"
else
path_txt += "#{pr}=\#{#{pr.to_s.snake_case}}&"
params << "#{pr.to_s.snake_case}"
if create_constants
if params_query.include?(pr)
if create_method_name == :operationId
path_txt += "#{pr}=\#{#{pr}}&"
params << "#{pr}: #{pr.upcase}"
required_constants << pr.upcase
else
path_txt += "#{pr}=\#{#{pr.to_s.snake_case}}&"
params << "#{pr.to_s.snake_case}: #{pr.to_s.snake_case.upcase}"
required_constants << pr.to_s.snake_case.upcase
end
end
else
if params_query.include?(pr)
if create_method_name == :operationId
path_txt += "#{pr}=\#{#{pr}}&"
params << "#{pr}"
else
path_txt += "#{pr}=\#{#{pr.to_s.snake_case}}&"
params << "#{pr.to_s.snake_case}"
end
end
end
end
Expand Down Expand Up @@ -435,7 +456,12 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
paramst = []
prms = path_txt.scan(/[^#]{(\w+)}/)
prms.each do |p|
paramst<<p[0].to_s.snake_case
#if create_constants
# paramst<<"#{p[0].to_s.snake_case}: #{p[0].to_s.snake_case.upcase}"
# required_constants << p[0].to_s.snake_case.upcase
#else
paramst<<p[0].to_s.snake_case
#end
path_txt.gsub!("{#{p[0]}}", "\#{#{p[0].to_s.snake_case}}")
end
paramst.concat params
Expand Down Expand Up @@ -547,7 +573,6 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
end
end
end

output_footer = []

output_footer << "end" unless (module_requests == "") && ([:path, :path_file, :tags, :tags_file].include?(name_for_module))
Expand Down Expand Up @@ -589,7 +614,18 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
end

requests_file_path = file_to_convert + ".rb"
File.open(requests_file_path, "w") { |file| file.write(requires_txt) }
if required_constants.size > 0
rconsts = "# Required constants\n"
required_constants.uniq!
required_constants.each do |rq|
rconsts += "#{rq} ||= ENV['#{rq}'] ||=''\n"
end
rconsts += "\n\n"
else
rconsts = ''
end

File.open(requests_file_path, "w") { |file| file.write(rconsts + requires_txt) }
res_rufo = `rufo #{requests_file_path}`
message = "** File that contains all the requires for all Request files: \n"
message += " - #{requests_file_path} "
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.9.2'
s.version = '0.10.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
9 changes: 9 additions & 0 deletions spec/open_api_import/open_api_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@
expect(File.read("#{file_name}.rb")).to match /def\sself\.list_pets/
end

it 'adds constants if create_constants' do
file_name = './spec/fixtures/v2.0/yaml/uber.yaml'
File.delete("#{file_name}.rb") if File.exist?("#{file_name}.rb")
OpenApiImport.from file_name, create_method_name: :path, create_constants: true
expect(File.exist?("#{file_name}.rb")).to eq true
content = File.read("#{file_name}.rb")
expect(content).to include('def self.get_products(latitude: LATITUDE, longitude: LONGITUDE)')
end

end


Expand Down

0 comments on commit c52b9e7

Please sign in to comment.