Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set nested scopes #221

Merged
merged 2 commits into from
Aug 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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