Skip to content

Commit

Permalink
Merge pull request #224 from cover/master
Browse files Browse the repository at this point in the history
Add the ability to set the value of nested params
  • Loading branch information
oestrich committed Sep 17, 2015
2 parents de69a7a + ccad82f commit 1c7f917
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
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

0 comments on commit 1c7f917

Please sign in to comment.