Skip to content

Add .configuration to Grape::API (fixes #1906) #1907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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