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)