From 12e6ef14f1d8f669b248c3765be5fb53a6caf4e9 Mon Sep 17 00:00:00 2001 From: unleashy Date: Thu, 12 Sep 2019 15:35:49 -0300 Subject: [PATCH 1/4] Add .with_configuration to Grape::API (fixes #1906) Includes an intentionally failing test for the inheritance of config to mounted APIs, to be implemented --- lib/grape/api.rb | 6 ++++++ spec/grape/api_spec.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/grape/api.rb b/lib/grape/api.rb index 0387bac9c..bf77fd4a7 100644 --- a/lib/grape/api.rb +++ b/lib/grape/api.rb @@ -42,6 +42,12 @@ def override_all_methods! end end + def with_configuration(config) + config.each do |key, value| + base_instance.configuration[key] = value + end + end + # This is the interface point between Rack and Grape; it accepts a request # from Rack and ultimately returns an array of three values: the status, # the headers, and the body. See [the rack specification] diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 2dca91889..2f18f6125 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -3754,6 +3754,42 @@ def before end end + describe '.with_configuration' do + it 'passes configuration from outside' do + subject.with_configuration(hello: 'hello', bread: 'bread') + subject.get '/hello-bread' do + "#{configuration[:hello]} #{configuration[:bread]}" + end + + get '/hello-bread' + expect(last_response.body).to eq 'hello bread' + end + + it 'inherits configuration on mounted APIs' do + a = Class.new(Grape::API) do + get '/hello' do + configuration[:hello] + end + end + + b = Class.new(Grape::API) do + get '/bread' do + configuration[:bread] + end + end + + subject.mount a + a.mount b + subject.with_configuration(hello: 'hello', bread: 'bread') + + get '/hello' + expect(last_response.body).to eq 'hello' + + get '/bread' + expect(last_response.body).to eq 'bread' + end + end + context 'catch-all' do before do api1 = Class.new(Grape::API) From ca345ea13458382a897d9e0ecfca6d1d11d98686 Mon Sep 17 00:00:00 2001 From: unleashy Date: Thu, 12 Sep 2019 19:01:44 -0300 Subject: [PATCH 2/4] Return self in with_configuration --- lib/grape/api.rb | 1 + spec/grape/api_spec.rb | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/lib/grape/api.rb b/lib/grape/api.rb index bf77fd4a7..d95cd1c3c 100644 --- a/lib/grape/api.rb +++ b/lib/grape/api.rb @@ -46,6 +46,7 @@ def with_configuration(config) config.each do |key, value| base_instance.configuration[key] = value end + self end # This is the interface point between Rack and Grape; it accepts a request diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 2f18f6125..58007bf3b 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -3755,6 +3755,10 @@ def before end describe '.with_configuration' do + it 'returns self' do + expect(subject.with_configuration({})).to be subject + end + it 'passes configuration from outside' do subject.with_configuration(hello: 'hello', bread: 'bread') subject.get '/hello-bread' do From 983c4b3e7c57ba9fd8af514d1fe90e30e1c01b06 Mon Sep 17 00:00:00 2001 From: unleashy Date: Sat, 14 Sep 2019 16:16:20 -0300 Subject: [PATCH 3/4] Change .with_configuration to .configure has a new design using blocks for configuration --- lib/grape/api.rb | 11 +++++++---- spec/grape/api_spec.rb | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/lib/grape/api.rb b/lib/grape/api.rb index d95cd1c3c..212b6d513 100644 --- a/lib/grape/api.rb +++ b/lib/grape/api.rb @@ -42,11 +42,14 @@ def override_all_methods! end end - def with_configuration(config) - config.each do |key, value| - base_instance.configuration[key] = value + def configure + config = @base_instance.configuration + if block_given? + yield config + self + else + config end - self end # This is the interface point between Rack and Grape; it accepts a request diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 58007bf3b..1a7fc71ba 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -3754,13 +3754,35 @@ def before end end - describe '.with_configuration' do - it 'returns self' do - expect(subject.with_configuration({})).to be subject + fdescribe '.configure' do + context 'when given a block' do + it 'returns self' do + expect(subject.configure {}).to be subject + end + + it 'calls the block passing the config' do + call = [false, nil] + subject.configure do |config| + call = [true, config] + end + + expect(call[0]).to be true + expect(call[1]).not_to be_nil + end end - it 'passes configuration from outside' do - subject.with_configuration(hello: 'hello', bread: 'bread') + context 'when not given a block' do + it 'returns a configuration object' do + expect(subject.configure).to respond_to(:[], :[]=) + end + end + + it 'allows configuring the api' do + subject.configure do |config| + config[:hello] = 'hello' + config[:bread] = 'bread' + end + subject.get '/hello-bread' do "#{configuration[:hello]} #{configuration[:bread]}" end @@ -3784,7 +3806,10 @@ def before subject.mount a a.mount b - subject.with_configuration(hello: 'hello', bread: 'bread') + subject.configure do |config| + config[:hello] = 'hello' + config[:bread] = 'bread' + end get '/hello' expect(last_response.body).to eq 'hello' From 8414a124627d200bf718c711284620d4828494af Mon Sep 17 00:00:00 2001 From: unleashy Date: Sat, 14 Sep 2019 16:32:15 -0300 Subject: [PATCH 4/4] Remove spec focus --- spec/grape/api_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 1a7fc71ba..193fbd6d9 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -3754,7 +3754,7 @@ def before end end - fdescribe '.configure' do + describe '.configure' do context 'when given a block' do it 'returns self' do expect(subject.configure {}).to be subject