Skip to content

Commit

Permalink
Merge 12e6ef1 into 45b1288
Browse files Browse the repository at this point in the history
  • Loading branch information
unleashy committed Sep 12, 2019
2 parents 45b1288 + 12e6ef1 commit b48258f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
36 changes: 36 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b48258f

Please sign in to comment.