From b5fd1f15634b6238d07cafdec30cc0b84817376d Mon Sep 17 00:00:00 2001 From: Justin Smestad Date: Mon, 22 Jan 2018 14:21:52 -0700 Subject: [PATCH] Parameters accept a value option for fixed values. This is very common in JSONAPI, where the `type` field is a fixed value of the resource name. Having fixed values supported makes the API more clear and uses `let` for dynamic values that need to be evaluated. --- Gemfile.lock | 10 +++++----- lib/rspec_api_documentation/dsl/endpoint/set_param.rb | 9 ++++++++- spec/dsl_spec.rb | 5 ++++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index cb825ac2..73364087 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -65,19 +65,19 @@ GEM sparkr (>= 0.2.0) term-ansicolor yard (~> 0.8.7.5) - json (1.8.3) + json (1.8.6) method_source (0.8.2) mime-types (3.0) mime-types-data (~> 3.2015) mime-types-data (3.2015.1120) - mini_portile2 (2.0.0) + mini_portile2 (2.3.0) minitest (5.8.4) multi_json (1.11.2) multi_test (0.1.2) multipart-post (2.0.0) mustache (1.0.3) - nokogiri (1.6.7.2) - mini_portile2 (~> 2.0.0.rc2) + nokogiri (1.8.1) + mini_portile2 (~> 2.3.0) pry (0.10.3) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -157,4 +157,4 @@ DEPENDENCIES webmock (~> 1.7) BUNDLED WITH - 1.15.3 + 1.16.1 diff --git a/lib/rspec_api_documentation/dsl/endpoint/set_param.rb b/lib/rspec_api_documentation/dsl/endpoint/set_param.rb index 760212b2..63ba0462 100644 --- a/lib/rspec_api_documentation/dsl/endpoint/set_param.rb +++ b/lib/rspec_api_documentation/dsl/endpoint/set_param.rb @@ -36,6 +36,10 @@ def custom_method_name param[:method] end + def set_value + param[:value] + end + def path_name scoped_key || key end @@ -51,11 +55,14 @@ def method_name scoped_key elsif key && example_group.respond_to?(key) key + elsif key && set_value + key end end def build_param_hash(keys) - value = keys[1] ? build_param_hash(keys[1..-1]) : example_group.send(method_name) + value = param[:value] if param.has_key?(:value) + value ||= keys[1] ? build_param_hash(keys[1..-1]) : example_group.send(method_name) { keys[0].to_s => value } end end diff --git a/spec/dsl_spec.rb b/spec/dsl_spec.rb index 77756733..94253414 100644 --- a/spec/dsl_spec.rb +++ b/spec/dsl_spec.rb @@ -62,6 +62,7 @@ parameter :size, "The size of drink you want.", :required => true parameter :note, "Any additional notes about your order.", method: :custom_note parameter :name, :scope => :order, method: :custom_order_name + parameter :quantity, "The quantity of drinks you want.", value: '3' response_field :type, "The type of drink you ordered.", :scope => :order response_field :size, "The size of drink you ordered.", :scope => :order @@ -88,6 +89,7 @@ { :name => "size", :description => "The size of drink you want.", :required => true }, { :name => "note", :description => "Any additional notes about your order.", method: :custom_note }, { :name => "name", :description => "Order name", :scope => :order, method: :custom_order_name }, + { :name => "quantity", :description => "The quantity of drinks you want.", value: '3' } ] ) end @@ -114,7 +116,8 @@ "type" => "coffee", "size" => "medium", "note" => "Made in India", - "order" => { "name" => "Jakobz" } + "order" => { "name" => "Jakobz" }, + "quantity" => "3" }) end end