Skip to content

Commit e6dad15

Browse files
committed
Extract Endpoint#params: introduce DSL::Param and DSL::SetParam classes
1 parent b55d4ea commit e6dad15

3 files changed

Lines changed: 94 additions & 42 deletions

File tree

lib/rspec_api_documentation/dsl/endpoint.rb

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'rspec/core/formatters/base_formatter'
22
require 'rack/utils'
33
require 'rack/test/utils'
4+
require 'rspec_api_documentation/dsl/endpoint/params'
45

56
module RspecApiDocumentation::DSL
67
# DSL methods available inside the RSpec example.
@@ -63,11 +64,7 @@ def query_string
6364
end
6465

6566
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
7168
end
7269

7370
def header(name, value)
@@ -99,14 +96,6 @@ def status
9996
rspec_api_documentation_client.status
10097
end
10198

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-
11099
def path
111100
example.metadata[:route].gsub(/:(\w+)/) do |match|
112101
if extra_params.keys.include?($1)
@@ -146,34 +135,5 @@ def delete_extra_param(key)
146135
@extra_params.delete(key.to_sym) || @extra_params.delete(key.to_s)
147136
end
148137

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-
178138
end
179139
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'rspec_api_documentation/dsl/endpoint/set_param'
2+
3+
module RspecApiDocumentation
4+
module DSL
5+
module Endpoint
6+
class Params
7+
attr_reader :example_group, :example
8+
9+
def initialize(example_group, example:, extra_params:)
10+
@example_group = example_group
11+
@example = example
12+
@extra_params = extra_params
13+
end
14+
15+
def call
16+
parameters = example.metadata.fetch(:parameters, {}).inject({}) do |hash, param|
17+
SetParam.new(self, hash: hash, param: param).call
18+
end
19+
parameters.deep_merge!(extra_params)
20+
parameters
21+
end
22+
23+
private
24+
25+
attr_reader :extra_params
26+
27+
end
28+
end
29+
end
30+
end
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module RspecApiDocumentation
2+
module DSL
3+
module Endpoint
4+
class SetParam
5+
def initialize(parent, hash:, param:)
6+
@parent = parent
7+
@hash = hash
8+
@param = param
9+
end
10+
11+
def call
12+
return hash if path_params.include?(path_name)
13+
return hash unless method_name
14+
15+
hash.deep_merge build_param_hash(key_scope || [key])
16+
end
17+
18+
private
19+
20+
attr_reader :parent, :hash, :param
21+
delegate :example_group, :example, to: :parent
22+
23+
def key
24+
@key ||= param[:name]
25+
end
26+
27+
def key_scope
28+
@key_scope ||= param[:scope] && Array(param[:scope]).dup.push(key)
29+
end
30+
31+
def scoped_key
32+
@scoped_key ||= key_scope && key_scope.join('_')
33+
end
34+
35+
def custom_method_name
36+
param[:method]
37+
end
38+
39+
def path_name
40+
scoped_key || key
41+
end
42+
43+
def path_params
44+
example.metadata[:route].scan(/:(\w+)/).flatten
45+
end
46+
47+
def method_name
48+
@method_name ||= begin
49+
[custom_method_name, scoped_key, key].find do |name|
50+
name && example_group.respond_to?(name)
51+
end
52+
end
53+
end
54+
55+
def build_param_hash(keys)
56+
value = keys[1] ? build_param_hash(keys[1..-1]) : example_group.send(method_name)
57+
{ keys[0].to_s => value }
58+
end
59+
end
60+
end
61+
end
62+
end

0 commit comments

Comments
 (0)