From 2b7be0cd914d74bfc2e3296bf3614b9b0425d550 Mon Sep 17 00:00:00 2001 From: unleashy Date: Wed, 2 Oct 2019 11:42:18 -0300 Subject: [PATCH] Add .configuration to Grape::API (fixes #1906) (#1907) --- CHANGELOG.md | 1 + README.md | 11 +++++++++++ lib/grape/api.rb | 15 +++++++++++++++ spec/grape/api_spec.rb | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52a86c4a80..936ca5ee91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 96388ea59b..ebaf1ac834 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/lib/grape/api.rb b/lib/grape/api.rb index 0387bac9c7..fa707d2e92 100644 --- a/lib/grape/api.rb +++ b/lib/grape/api.rb @@ -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] diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 2dca918891..63c1623bb6 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -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)