|
1 | 1 | require 'rspec/core/formatters/base_formatter' |
2 | 2 | require 'rack/utils' |
3 | 3 | require 'rack/test/utils' |
| 4 | +require 'rspec_api_documentation/dsl/endpoint/params' |
4 | 5 |
|
5 | 6 | module RspecApiDocumentation::DSL |
6 | 7 | # DSL methods available inside the RSpec example. |
@@ -63,11 +64,7 @@ def query_string |
63 | 64 | end |
64 | 65 |
|
65 | 66 | def params |
66 | | - parameters = example.metadata.fetch(:parameters, {}).inject({}) do |hash, param| |
67 | | - set_param(hash, param) |
68 | | - end |
69 | | - parameters.deep_merge!(extra_params) |
70 | | - parameters |
| 67 | + Params.new(self, example: example, extra_params: extra_params).call |
71 | 68 | end |
72 | 69 |
|
73 | 70 | def header(name, value) |
@@ -99,14 +96,6 @@ def status |
99 | 96 | rspec_api_documentation_client.status |
100 | 97 | end |
101 | 98 |
|
102 | | - def in_path?(param) |
103 | | - path_params.include?(param) |
104 | | - end |
105 | | - |
106 | | - def path_params |
107 | | - example.metadata[:route].scan(/:(\w+)/).flatten |
108 | | - end |
109 | | - |
110 | 99 | def path |
111 | 100 | example.metadata[:route].gsub(/:(\w+)/) do |match| |
112 | 101 | if extra_params.keys.include?($1) |
@@ -146,34 +135,5 @@ def delete_extra_param(key) |
146 | 135 | @extra_params.delete(key.to_sym) || @extra_params.delete(key.to_s) |
147 | 136 | end |
148 | 137 |
|
149 | | - def set_param(hash, param) |
150 | | - key = param[:name] |
151 | | - key_scope = param[:scope] && Array(param[:scope]).dup.push(key) |
152 | | - scoped_key = key_scope && key_scope.join('_') |
153 | | - custom_method_name = param[:method] |
154 | | - path_name = scoped_key || key |
155 | | - |
156 | | - return hash if in_path?(path_name) |
157 | | - |
158 | | - build_param_data = if custom_method_name && respond_to?(custom_method_name) |
159 | | - [key_scope || [key], custom_method_name] |
160 | | - elsif scoped_key && respond_to?(scoped_key) |
161 | | - [key_scope, scoped_key] |
162 | | - elsif respond_to?(key) |
163 | | - [key_scope || [key], key] |
164 | | - else |
165 | | - [] |
166 | | - end |
167 | | - # binding.pry if key == "street" |
168 | | - |
169 | | - return hash if build_param_data.empty? |
170 | | - hash.deep_merge(build_param_hash(*build_param_data)) |
171 | | - end |
172 | | - |
173 | | - def build_param_hash(keys, method_name) |
174 | | - value = keys[1] ? build_param_hash(keys[1..-1], method_name) : send(method_name) |
175 | | - { keys[0].to_s => value } |
176 | | - end |
177 | | - |
178 | 138 | end |
179 | 139 | end |
0 commit comments