Skip to content

Commit

Permalink
Merge 8414a12 into 45b1288
Browse files Browse the repository at this point in the history
  • Loading branch information
unleashy committed Sep 14, 2019
2 parents 45b1288 + 8414a12 commit 59a51cc
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ def override_all_methods!
end
end

def configure
config = @base_instance.configuration
if block_given?
yield config
self
else
config
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
65 changes: 65 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3754,6 +3754,71 @@ def before
end
end

describe '.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

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

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.configure do |config|
config[:hello] = 'hello'
config[:bread] = 'bread'
end

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 59a51cc

Please sign in to comment.