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

Add the ability to set the value of nested params #224

Merged
merged 2 commits into from
Sep 17, 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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ Special values:
* `:required => true` Will display a red '*' to show it's required
* `:scope => :the_scope` Will scope parameters in the hash, scoping can be nested. See example

The value of scoped parameters can be set with both scoped (`let(:order_item_item_id)`) and unscoped (`let(:item_id)`) methods. It always searches for the scoped method first and falls back to the unscoped method.

```ruby
resource "Orders" do
parameter :auth_token, "Authentication Token"
Expand All @@ -403,8 +405,8 @@ resource "Orders" do
parameter :item, "Order items", :scope => :order
parameter :item_id, "Item id", :scope => [:order, :item]

let(:name) { "My Order" }
let(:item_id) { 1 }
let(:name) { "My Order" } # OR let(:order_name) { "My Order" }
let(:item_id) { 1 } # OR let(:order_item_item_id) { 1 }

example "Creating an order" do
params.should eq({
Expand Down
3 changes: 2 additions & 1 deletion lib/rspec_api_documentation/dsl/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,12 @@ def delete_extra_param(key)

def set_param(hash, param)
key = param[:name]
return hash if in_path?(key)

keys = [param[:scope], key].flatten.compact
method_name = keys.join('_')

return hash if in_path?(method_name)

unless respond_to?(method_name)
method_name = key
return hash unless respond_to?(method_name)
Expand Down
6 changes: 6 additions & 0 deletions spec/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@
put "/orders/:id" do
parameter :type, "The type of drink you want.", :required => true
parameter :size, "The size of drink you want.", :required => true
parameter :id, 'The ID of the resource.', :required => true, scope: :data
parameter :note, "Any additional notes about your order."

let(:type) { "coffee" }
let(:size) { "medium" }
let(:data_id) { 2 }

let(:id) { 1 }

Expand All @@ -129,6 +131,10 @@
end
end

it 'should set the scoped data ID' do
expect(params['data']).to eq({'id' => 2})
end

it "should allow extra parameters to be passed in" do
expect(client).to receive(method).with(path, params.merge("extra" => true), nil)
do_request(:extra => true)
Expand Down