Class: SwaggerBuilder
- Inherits:
-
Object
- Object
- SwaggerBuilder
- Defined in:
- lib/schemaless_rest_api/swagger_builder.rb
Overview
Class to build Swagger json description of entities
Class Method Summary collapse
- .any_json_request ⇒ Object
- .build_info(models) ⇒ Object
- .build_swagger_for(models, host_with_port) ⇒ Object
- .generate_paths_for(models) ⇒ Object
- .id_params(model) ⇒ Object
- .model_paths(model) ⇒ Object
- .paths_at_id(model) ⇒ Object
- .summary ⇒ Object
Class Method Details
.any_json_request ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/schemaless_rest_api/swagger_builder.rb', line 82 def any_json_request [ { "description": "Arbitrary JSON object payload", "in": "body", "name": "body", "required": true, "schema": { "type": "object" } } ] end |
.build_info(models) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/schemaless_rest_api/swagger_builder.rb', line 21 def build_info(models) info = {} info["description"] = "Schemaless REST API providing CRUD for #{models.keys.length} models: #{models.keys.join("\n")}" info end |
.build_swagger_for(models, host_with_port) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/schemaless_rest_api/swagger_builder.rb', line 10 def build_swagger_for(models, host_with_port) doc = {} doc["swagger"] = "2.0" doc["info"] = build_info(models) doc["host"] = host_with_port doc["schemes"] = "http" doc["basePath"] = "/#{ENV["base_path"]}" doc["paths"] = generate_paths_for models JSON.generate(doc) end |
.generate_paths_for(models) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/schemaless_rest_api/swagger_builder.rb', line 28 def generate_paths_for(models) paths = { "/" => summary } models.each_key do |model| paths["/#{model.downcase}"] = model_paths(model) paths["/#{model.downcase}/{modelId}"] = paths_at_id(model) end paths end |
.id_params(model) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/schemaless_rest_api/swagger_builder.rb', line 154 def id_params(model) [ { "name": "modelId", "in": "path", "description": "ID of #{model} to return", "required": true, "type": "string" } ] end |
.model_paths(model) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/schemaless_rest_api/swagger_builder.rb', line 49 def model_paths(model) { post: { parameters: any_json_request, responses: { "201": { "description": "Response with id of created #{model}" } } }, get: { parameters: [ { name: "params", description: "Any key can be used to filter #{model} by", schema: { type: "object", # If the parameter values are of specific type, e.g. string: additionalProperties: { type: "string" } } } ], responses: { "200": { "description": "List of all #{model} objects" } } } } end |
.paths_at_id(model) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/schemaless_rest_api/swagger_builder.rb', line 96 def paths_at_id(model) { get: { description: "Data of #{model} at passed id", parameters: id_params(model), responses: { "200": { "description": "Data of #{model} requested" }, "404": { "description": "#{model} not found" } } }, put: { description: "Update id of #{model} to be provided data", parameters: [ { "description": "Arbitrary JSON object payload", "in": "body", "name": "body", "required": true, "schema": { "type": "object" } }, { "name": "modelId", "in": "path", "description": "ID of #{model} to update", "required": true, "type": "string" } ], responses: { "204": { "description": "Response" }, "404": { "description": "#{model} not found" } } }, delete: { description: "Delete #{model} at passed id", parameters: id_params(model), responses: { "204": { "description": "Response" }, "404": { "description": "#{model} not found" } } } } end |
.summary ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/schemaless_rest_api/swagger_builder.rb', line 37 def summary { get: { responses: { "200": { "description": "Basic basic summary of API - could be used as health check" } } } } end |