Class: Rspec::Autoswagger::Parts::Path
- Inherits:
-
Object
- Object
- Rspec::Autoswagger::Parts::Path
- Defined in:
- lib/rspec/autoswagger/parts/path.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#example ⇒ Object
readonly
Returns the value of attribute example.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#response_name ⇒ Object
readonly
Returns the value of attribute response_name.
Instance Method Summary collapse
- #generate_hash ⇒ Object
- #generate_parameters ⇒ Object
- #get_converted_path(path) ⇒ Object
-
#initialize(rspec_core_obj, example, response_name) ⇒ Path
constructor
A new instance of Path.
- #method ⇒ Object
- #operation_id ⇒ Object
- #param_model_name ⇒ Object
- #params ⇒ Object
- #status ⇒ Object
- #tags ⇒ Object
Constructor Details
#initialize(rspec_core_obj, example, response_name) ⇒ Path
Returns a new instance of Path.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 9 def initialize(rspec_core_obj, example, response_name) @response = rspec_core_obj.response @request = rspec_core_obj.request begin @description = rspec_core_obj.description rescue RSpec::Core::ExampleGroup::WrongScopeError @description = '' end @example = example @response_name = response_name end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
7 8 9 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 7 def description @description end |
#example ⇒ Object (readonly)
Returns the value of attribute example.
7 8 9 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 7 def example @example end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 7 def path @path end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
7 8 9 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 7 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 7 def response @response end |
#response_name ⇒ Object (readonly)
Returns the value of attribute response_name.
7 8 9 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 7 def response_name @response_name end |
Instance Method Details
#generate_hash ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 124 def generate_hash hash = {} path_sym = path hash[path] = {} hash[path][method] = {} hash[path][method] = {} hash[path][method]["tags"] = hash[path][method]["summary"] = '' hash[path][method]["description"] = description params, param_definitions = generate_parameters hash[path][method]["parameters"] = params hash[path][method]["produces"] = ['application/json'] hash[path][method]["responses"] = {} hash[path][method]["responses"][status] = {} hash[path][method]["responses"][status]["description"] = response_description hash[path][method]["responses"][status]["schema"] = { "$ref" => "#/definitions/#{response_name}" } [hash, param_definitions] end |
#generate_parameters ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 74 def generate_parameters schema = {} params_arr = [] params.each do |name, value| type = convert_value_to_type(value) param_type = predict_param_type(name) if param_type == 'body' if type == 'array' value = value.map { |v| v.to_h } if value.first.class.to_s == 'ActiveSupport::HashWithIndifferentAccess' type_hash = SwaggerModel::SwaggerV2.parse_array(value, "dummy", "dummy") type_hash.delete('example') schema.merge!({ name => { 'type' => type, 'items' => type_hash } }) else schema.merge!({ name => { 'type' => type } }) end else if type == 'array' value = value.map { |v| v.to_h } if value.first.class.to_s == 'ActiveSupport::HashWithIndifferentAccess' type_hash = SwaggerModel::SwaggerV2.parse_array(value, "dummy", "dummy") type_hash.delete('example') param_hash = { 'name' => name, 'in' => param_type, 'type' => type, 'items' => type_hash } else param_hash = { 'name' => name, 'in' => param_type, 'type' => type } end param_hash['required'] = true if param_type == 'path' params_arr << param_hash end end param_definitions = {} unless schema.empty? params_arr << { 'name' => 'body', 'in' => 'body', 'schema' => { "$ref" => "#/definitions/#{param_model_name}" } } param_definitions = { param_model_name => { 'type' => 'object', 'properties' => schema } } end [params_arr, param_definitions] end |
#get_converted_path(path) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 31 def get_converted_path(path) path.split("/").map do |path_element| if Util.detect_uuid(path_element) "{id}" elsif Util.detect_uuid(path_element) "{id}" else path_element end end.join("/") end |
#method ⇒ Object
54 55 56 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 54 def method request.method.downcase end |
#operation_id ⇒ Object
58 59 60 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 58 def operation_id (method + path.gsub('/', '_').gsub(/{|}/, '')).camelize end |
#param_model_name ⇒ Object
62 63 64 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 62 def param_model_name (response_name + '_request_parameter').camelize end |
#params ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 66 def params return @params if @params.present? @params = request.parameters.dup @params.delete('controller') @params.delete('action') @params end |
#status ⇒ Object
50 51 52 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 50 def status response.status end |
#tags ⇒ Object
43 44 45 46 47 48 |
# File 'lib/rspec/autoswagger/parts/path.rb', line 43 def tag = request.parameters['controller'] base_path = Info.generate_hash['basePath'] tag = ('/' + tag).gsub(base_path.to_s, '') [tag] end |