Skip to content

Commit

Permalink
Merge pull request #221 from kevintraver/nested-scope
Browse files Browse the repository at this point in the history
Ability to set nested scopes
  • Loading branch information
oestrich committed Aug 11, 2015
2 parents e9e1198 + 2e08148 commit 5f55530
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/rspec_api_documentation/dsl/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +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 param[:scope]
hash[param[:scope].to_s] ||= {}
hash[param[:scope].to_s][key] = send(key)
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
11 changes: 11 additions & 0 deletions spec/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 5f55530

Please sign in to comment.