Skip to content

Commit

Permalink
Add .configuration to Grape::API (fixes #1906) (#1907)
Browse files Browse the repository at this point in the history
  • Loading branch information
unleashy authored and dblock committed Oct 2, 2019
1 parent 45b1288 commit 2b7be0c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Your contribution here.
* [#1904](https://github.com/ruby-grape/grape/pull/1904): Allows Grape to load files on startup rather than on the first call - [@myxoh](https://github.com/myxoh).
* [#1907](https://github.com/ruby-grape/grape/pull/1907): Adds outside configuration to Grape with `configure` - [@unleashy](https://github.com/unleashy).

#### Fixes

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,17 @@ Grape.configure do |config|
end
```

You can also configure a single API:

```ruby
API.configure do |config|
config[key] = value
end
```

This will be available inside the API with `configuration`, as if it were
[mount configuration](#mount-configuration).

## Parameters

Request parameters are available through the `params` hash object. This includes `GET`, `POST`
Expand Down
15 changes: 15 additions & 0 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ def override_all_methods!
end
end

# Configure an API from the outside. If a block is given, it'll pass a
# configuration hash to the block which you can use to configure your
# API. If no block is given, returns the configuration hash.
# The configuration set here is accessible from inside an API with
# `configuration` as normal.
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
38 changes: 38 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3754,6 +3754,44 @@ 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
end

context 'catch-all' do
before do
api1 = Class.new(Grape::API)
Expand Down

0 comments on commit 2b7be0c

Please sign in to comment.