{"id":14986590,"url":"https://github.com/marioruiz/open_api_import","last_synced_at":"2025-04-11T20:32:30.160Z","repository":{"id":47942632,"uuid":"167353289","full_name":"MarioRuiz/open_api_import","owner":"MarioRuiz","description":"Ruby gem open_api_import -- Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses","archived":false,"fork":false,"pushed_at":"2025-04-03T10:30:25.000Z","size":122,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T23:37:46.798Z","etag":null,"topics":["gem","hash","open-api","open-api-2","open-api-v3","parser","request","ruby","swagger"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MarioRuiz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-24T11:02:17.000Z","updated_at":"2025-04-03T10:25:15.000Z","dependencies_parsed_at":"2024-09-25T00:31:23.222Z","dependency_job_id":null,"html_url":"https://github.com/MarioRuiz/open_api_import","commit_stats":{"total_commits":83,"total_committers":2,"mean_commits":41.5,"dds":"0.42168674698795183","last_synced_commit":"2ea8e0e17eb3b0a03d00f3d23bc7996f3502ab59"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarioRuiz%2Fopen_api_import","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarioRuiz%2Fopen_api_import/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarioRuiz%2Fopen_api_import/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarioRuiz%2Fopen_api_import/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MarioRuiz","download_url":"https://codeload.github.com/MarioRuiz/open_api_import/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248476524,"owners_count":21110304,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gem","hash","open-api","open-api-2","open-api-v3","parser","request","ruby","swagger"],"created_at":"2024-09-24T14:13:11.392Z","updated_at":"2025-04-11T20:32:29.524Z","avatar_url":"https://github.com/MarioRuiz.png","language":"Ruby","readme":"# OpenApiImport\n\n[![Gem Version](https://badge.fury.io/rb/open_api_import.svg)](https://rubygems.org/gems/open_api_import)\n[![Build Status](https://travis-ci.com/MarioRuiz/open_api_import.svg?branch=master)](https://github.com/MarioRuiz/open_api_import)\n[![Coverage Status](https://coveralls.io/repos/github/MarioRuiz/open_api_import/badge.svg?branch=master)](https://coveralls.io/github/MarioRuiz/open_api_import?branch=master)\n\nImport 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.\n\nThe Request Hash will include also the pattern (regular expressions) of the fields,  parameters, default values...\n\nThe output of this gem will be following the specification of Request Hashes: https://github.com/MarioRuiz/Request-Hash\n\nThe 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\n\nA beautiful way to access and use the generated files is by using *pry*: https://github.com/pry/pry\n\nTo be able to generate random requests take a look at the documentation for nice_hash gem: https://github.com/MarioRuiz/nice_hash\n\nThis is an example of a generated request hash: \n\n```ruby\n\n        # operationId: addPet, method: post\n        # summary: Example\n        # description: Creates a new pet in the store.  Duplicates are allowed\n        # required data: name\n        def self.add_pet()\n          {\n            path: \"/api/pets\",\n            data_required: [\n              :name,\n            ],\n            data_examples: [\n              {\n                name: \"\",\n                tag: \"\",\n              },\n            ],\n            responses: {\n              '200': {message: \"pet response\"},\n              'default': {message: \"unexpected error\"},\n            },\n          }\n        end\n\n\n```\n\nIf you want to automatically generate RSpec tests for every end point of your Swagger file, use the create_tests gem: https://github.com/MarioRuiz/create_tests\n\n```ruby\nrequire 'create_tests'\n  \nCreateTests.from \"./requests/uber.yaml.rb\"\n```\n\nThis is the output of the previous run:\n\n```\n- Logs: ./requests/uber.yaml.rb_create_tests.log\n** Pay attention, if any of the test files exist or the help file exist only will be added the tests, methods that are missing.\n- Settings: ./settings/general.rb\n- Test created: ./spec/User/profile_user_spec.rb\n- Test created: ./spec/User/activity_user_spec.rb\n- Test created: ./spec/Products/list_products_spec.rb\n- Test created: ./spec/Estimates/price_estimates_spec.rb\n- Test created: ./spec/Estimates/time_estimates_spec.rb\n- Helper: ./spec/helper.rb\n```\n\n## Installation\n\nInstall it yourself as:\n\n    $ gem install open_api_import\n\n\nTake in consideration open_api_import gem is using the 'rufo' gem that executes in command line the `rufo` command. In case you experience any trouble with it, visit: https://github.com/ruby-formatter/rufo\n\n## Usage\n\nAfter installation you can run using command line executable or importing from Ruby.\n\nYou 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. \n\n### Executable\n\nFor help and see the options, run in command line / bash: `open_api_import -h`\n\nExample: \n```bash\n open_api_import ./spec/fixtures/v2.0/yaml/uber.yaml -fp\n```\n\nThis is the output of `open_api_import -h`:\n\n```\nUsage: open_api_import [open_api_file] [options]\nImport a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses.\nMore info: https://github.com/MarioRuiz/open_api_import\n\nIn case no options supplied:\n  * It will be used the value of operation_id on snake_case for the name of the methods\n  * It will be used the first folder of the path to create the module name\n    -n, --no_responses               if you don't want to add the examples of responses in the resultant file.\n    -m, --mock                       Add the first response on the request as mock_response\n    -p, --path_method                it will be used the path and http method to create the method names\n    -o, --operationId_method         It will be used the operationId field like it is to create the method names\n    -f, --create_files               It will create a file per module\n    -T, --tags_module                It will be used the tags key to create the module name\n    -F, --fixed_module               all the requests will be under the module Requests\n    -s, --silent                     It will display only errors\n    -c, --create_constants           For required arguments, it will create keyword arguments assigning by default a constant.\n```\n\n\n### Ruby file\nWrite your ruby code on a file and in command line/bash: `ruby my_file.rb`\n\nTo convert the Swagger or Open API file into a Request Hash:\n\n```ruby\n  require 'open_api_import'\n  \n  OpenApiImport.from \"./spec/fixtures/v2.0/yaml/uber.yaml\"\n\n  OpenApiImport.from \"my_file.json\"\n\n```\n\nThe supported HTTP methods are: `GET`, `POST`, `PUT`, `DELETE` and `PATCH`\n\nThe requests will be organized by modules generated from the content in the Swagger file. \nFor example this would be generated when run this: `OpenApiImport.from \"./spec/fixtures/v2.0/yaml/petstore-simple.yaml\"`\n\n```ruby\n##################################################\n# Swagger Petstore\n# version: 1.0.0\n# description:\n#     A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification\n##################################################\nmodule Swagger\n  module SwaggerPetstore\n    module V1_0_0\n      module Pets\n        # operationId: findPetById, method: get\n        # summary:\n        # description:\n        #     Returns a user based on a single ID, if the user does not have access to the pet\n        # parameters description:\n        #    id: (integer) ID of pet to fetch\n        def self.find_pet_by_id(id)\n          {\n            path: \"/api/pets/#{id}\",\n            method: :get,\n            responses: {\n...\n...\n\n```\n\n## Parameters\n\nThe parameters can be supplied alone or with other parameters. In case a parameter is not supplied then it will be used the default value.\n\n### create_method_name\n\nHow the name of the methods will be generated.\n\nAccepts three different options: :path, :operation_id and :operationId. By default :operation_id. \n\n  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\n\n  operation_id: it will be used the operationId field but using the snake_case version, for example for listUsers: list_users\n\n  operationId: it will be used the operationId field like it is, for example: listUsers\n\n```ruby\n  require 'open_api_import'\n\n  OpenApiImport.from \"./spec/fixtures/v2.0/yaml/petstore-simple.yaml\", create_method_name: :path\n\n```\n\nThe output will generate methods like this:\n\n```ruby\n        # operationId: findPets, method: get\n        # summary:\n        # description:\n        #     Returns all pets from the system that the user has access to\n        # parameters description:\n        #    tags: (array) tags to filter by\n        #    limit: (integer) maximum number of results to return\n        def self.get_pets(tags: \"\", limit: \"\")\n          {\n            path: \"/api/pets?tags=#{tags}\u0026limit=#{limit}\u0026\",\n... \n...\n```\n\nif create_method_name is :operation_id\n\n```ruby\n  require 'open_api_import'\n\n  OpenApiImport.from \"./spec/fixtures/v2.0/yaml/petstore-simple.yaml\", create_method_name: :operation_id\n\n```\n\nThe output will generate methods like this:\n\n```ruby\n        # operationId: findPets, method: get\n        # summary:\n        # description:\n        #     Returns all pets from the system that the user has access to\n        # parameters description:\n        #    tags: (array) tags to filter by\n        #    limit: (integer) maximum number of results to return\n        def self.find_pets(tags: \"\", limit: \"\")\n          {\n            path: \"/api/pets?tags=#{tags}\u0026limit=#{limit}\u0026\",\n... \n...\n```\n\nif create_method_name is :operationId\n\n```ruby\n  require 'open_api_import'\n\n  OpenApiImport.from \"./spec/fixtures/v2.0/yaml/petstore-simple.yaml\", create_method_name: :operationId\n\n```\n\nThe output will generate methods like this:\n\n```ruby\n        # operationId: findPets, method: get\n        # summary:\n        # description:\n        #     Returns all pets from the system that the user has access to\n        # parameters description:\n        #    tags: (array) tags to filter by\n        #    limit: (integer) maximum number of results to return\n        def self.findPets(tags: \"\", limit: \"\")\n          {\n            path: \"/api/pets?tags=#{tags}\u0026limit=#{limit}\u0026\",\n... \n...\n```\n\n### name_for_module\n\nHow the module names will be created.\n\nAccepts five different options: :path, :path_file, :tags, :tags_file and :fixed. By default :path. \n\n  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.\n  \n  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.\n\n  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.\n  \n  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.\n\n  fixed: all the requests will be under the module Requests\n\n```ruby\n  require 'open_api_import'\n\n  OpenApiImport.from \"./spec/fixtures/v2.0/yaml/petstore-simple.yaml\", name_for_module: :fixed\n\n```\n\nIt will generate just one file including all requests under the Requests module\n\n```ruby\nmodule Swagger\n  module SwaggerPetstore\n    module V1_0_0\n      module Requests\n\n        # operationId: findPets, method: get\n        # summary:\n        # description:\n        #     Returns all pets from the system that the user has access to\n        # parameters description:\n        #    tags: (array) tags to filter by\n        #    limit: (integer) maximum number of results to return\n        def self.find_pets(tags: \"\", limit: \"\")\n...\n...\n```\n\nIn case using :path\n\n```ruby\n  require 'open_api_import'\n\n  OpenApiImport.from \"./spec/fixtures/v2.0/yaml/petstore-simple.yaml\", name_for_module: :path\n\n```\n\nIt will generate just one file including every request under the module generated from the first folder of the path\n\n```ruby\nmodule Swagger\n  module SwaggerPetstore\n    module V1_0_0\n      module Pets\n\n        # operationId: findPetById, method: get\n        # summary:\n        # description:\n        #     Returns a user based on a single ID, if the user does not have access to the pet\n        # parameters description:\n        #    id: (integer) ID of pet to fetch\n        def self.find_pet_by_id(id)\n...\n...\n```\n\nIn case using :path_file\n\n```ruby\n  require 'open_api_import'\n\n  OpenApiImport.from \"./spec/fixtures/v2.0/yaml/petstore-simple.yaml\", name_for_module: :path_file\n\n```\n\nIt will generate one file per module including every request under the module generated from the first folder of the path. Also it will be generated one file that will have all the `require_relative` for the generated request files.\n\nThis is the output of the run:\n\n```\n** Generated files that contain the code of the requests after importing the Swagger file:\n  - /petstore-simple.yaml_Root.rb\n  - /petstore-simple.yaml_Pets.rb\n** File that contains all the requires for all Request files:\n  - /petstore-simple.yaml.rb\n```\n\nIn case using :tags\n\n```ruby\n  require 'open_api_import'\n\n  OpenApiImport.from \"./spec/fixtures/v2.0/yaml/uber.yaml\", name_for_module: :tags, create_method_name: :path\n\n```\n\nIt will generate just one file including every request under the module generated from the first folder of the path\n\n```ruby\nmodule Swagger\n  module UberApi\n    module V1_0_0\n      module Products\n\n        # operationId: unknown, method: get\n        # summary: Product Types\n        # description:\n        #     The Products endpoint returns information about the Uber products offered at a given location. \n        #     The response includes the display name and other details about each product, and lists the products in the proper display order.\n        # parameters description:\n        #    latitude: (number) (required) Latitude component of location.\n        #    longitude: (number) (required) Longitude component of location.\n        def self.get_products(latitude, longitude)\n...\n...\n```\n\n\n### include_responses\n\nIf you want to add the examples of responses in the resultant file.\n\nAccepts true or false, by default is true.\n\n```ruby\n  require 'open_api_import'\n\n  OpenApiImport.from \"./spec/fixtures/v2.0/yaml/petstore-simple.yaml\", include_responses: false\n\n```\n\nA method that will be included in the output:\n\n```ruby\n        # operationId: findPetById, method: get\n        # summary:\n        # description:\n        #     Returns a user based on a single ID, if the user does not have access to the pet\n        # parameters description:\n        #    id: (integer) ID of pet to fetch\n        def self.find_pet_by_id(id)\n          {\n            path: \"/api/pets/#{id}\",\n            method: :get,\n          }\n        end\n```\n\nIn case we run this\n\n```ruby\n  require 'open_api_import'\n\n  OpenApiImport.from \"./spec/fixtures/v2.0/yaml/petstore-simple.yaml\", include_responses: true\n\n```\n\nA method that will be included in the output:\n\n```ruby\n        # operationId: findPetById, method: get\n        # summary:\n        # description:\n        #     Returns a user based on a single ID, if the user does not have access to the pet\n        # parameters description:\n        #    id: (integer) ID of pet to fetch\n        def self.find_pet_by_id(id)\n          {\n            path: \"/api/pets/#{id}\",\n            method: :get,\n            responses: {\n              '200': {\n                message: \"pet response\",\n                data: {\n                  name: \"string\",\n                  tag: \"string\",\n                  id: 0,\n                },\n              },\n              'default': {\n                message: \"unexpected error\",\n                data: {\n                  code: 0,\n                  message: \"string\",\n                },\n              },\n            },\n          }\n        end\n```\n\n### mock_response\n\nAdd the first response on the request as mock_response to be used.\n\nAdmits true or false. By default false.\n\nIn case using nice_http gem: if NiceHttp.use_mocks = true will use it instead of getting the real response from the WS.\n\n```ruby\n  require 'open_api_import'\n\n  OpenApiImport.from \"./spec/fixtures/v2.0/yaml/petstore.yaml\", mock_response: true\n\n```\n\nIt will include this on the output file: \n\n```ruby\n        # operationId: listPets, method: get\n        # summary: List all pets\n        # description:\n        # parameters description:\n        #    limit: (integer) How many items to return at one time (max 100)\n        def self.list_pets(limit: \"\")\n          {\n            path: \"/v1/pets?limit=#{limit}\u0026\",\n            method: :get,\n            mock_response: {\n              code: \"200\",\n              message: \"A paged array of pets\",\n              data: [\n                {\n                  id: 0,\n                  name: \"string\",\n                  tag: \"string\",\n                },\n              ],\n            },\n...\n...\n```\n\n### create_constants\n\nThe methods will be generated using keyword arguments and for required arguments, it will create keyword arguments assigning by default a constant.\n\n```ruby\ndef self.get_products(latitude: LATITUDE, longitude: LONGITUDE)\n```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/open_api_import.\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarioruiz%2Fopen_api_import","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarioruiz%2Fopen_api_import","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarioruiz%2Fopen_api_import/lists"}