From 090c13ad35ce7613174af2575dc59774f5e0f37a Mon Sep 17 00:00:00 2001 From: Kevin Traver Date: Sat, 8 Aug 2015 18:28:27 -0600 Subject: [PATCH 1/2] Ability to set nested scopes --- lib/rspec_api_documentation/dsl/endpoint.rb | 10 +++++++--- spec/dsl_spec.rb | 11 +++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/rspec_api_documentation/dsl/endpoint.rb b/lib/rspec_api_documentation/dsl/endpoint.rb index e9376de2..9bebfdb2 100644 --- a/lib/rspec_api_documentation/dsl/endpoint.rb +++ b/lib/rspec_api_documentation/dsl/endpoint.rb @@ -150,9 +150,13 @@ def set_param(hash, param) key = param[:name] return hash if !respond_to?(key) || in_path?(key) - if param[:scope] - hash[param[:scope].to_s] ||= {} - hash[param[:scope].to_s][key] = send(key) + if scope = param[:scope] + if scope.is_a?(Array) + hash.merge!(scope.reverse.inject({key => send(key)}) { |a,n| { n.to_s => a }}) + else + hash[scope.to_s] ||= {} + hash[scope.to_s][key] = send(key) + end else hash[key] = send(key) end diff --git a/spec/dsl_spec.rb b/spec/dsl_spec.rb index 2cf04ac2..30d07384 100644 --- a/spec/dsl_spec.rb +++ b/spec/dsl_spec.rb @@ -364,6 +364,7 @@ parameter :order_type, "Type of order" parameter :amount, "Amount of order", scope: :order parameter :name, "Name of order", scope: :order + parameter :street, "order location country", scope: [:order,:location,:address] context "no extra params" do @@ -396,6 +397,16 @@ example_request "should deep merge the optional parameter hash", {:order_type => 'big', :order => {:name => 'Friday Order'}} end + + context "extra options for do_request with nested scope" do + before do + expect(client).to receive(:post).with("/orders", {"order" => {"location" => {"address" => {"street" => "123 Main St"}}}}, nil) + end + + let(:street) { '123 Main St' } + + example_request "should deep merge the optional parameter hash with nested scope" + end end end From 2e08148e2bbe953fab5747df729303d597d76e97 Mon Sep 17 00:00:00 2001 From: Kevin Traver Date: Sun, 9 Aug 2015 11:22:23 -0700 Subject: [PATCH 2/2] Refactor using @cover suggestion --- lib/rspec_api_documentation/dsl/endpoint.rb | 25 ++++++++++++--------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/rspec_api_documentation/dsl/endpoint.rb b/lib/rspec_api_documentation/dsl/endpoint.rb index 9bebfdb2..d980a948 100644 --- a/lib/rspec_api_documentation/dsl/endpoint.rb +++ b/lib/rspec_api_documentation/dsl/endpoint.rb @@ -148,20 +148,23 @@ def delete_extra_param(key) def set_param(hash, param) key = param[:name] - return hash if !respond_to?(key) || in_path?(key) + return hash if in_path?(key) - if scope = param[:scope] - if scope.is_a?(Array) - hash.merge!(scope.reverse.inject({key => send(key)}) { |a,n| { n.to_s => a }}) - else - hash[scope.to_s] ||= {} - hash[scope.to_s][key] = send(key) - end - else - hash[key] = send(key) + keys = [param[:scope], key].flatten.compact + method_name = keys.join('_') + + unless respond_to?(method_name) + method_name = key + return hash unless respond_to?(method_name) end - hash + hash.deep_merge(build_param_hash(keys, method_name)) end + + def build_param_hash(keys, method_name) + value = keys[1] ? build_param_hash(keys[1..-1], method_name) : send(method_name) + { keys[0].to_s => value } + end + end end