Skip to content

Helper method to access controller context in middleware #1918

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 1 commit into from
Oct 22, 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 @@ -3,6 +3,7 @@
#### Features

* Your contribution here.
* [#1918](https://github.com/ruby-grape/grape/pull/1918): Helper methods to access controller context from middleware - [@NikolayRys](https://github.com/NikolayRys).
* [#1915](https://github.com/ruby-grape/grape/pull/1915): Micro optimizations in allocating hashes and arrays - [@dnesteryuk](https://github.com/dnesteryuk).
* [#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).
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2497,6 +2497,17 @@ class Twitter::API < Grape::API
end
```

Inside the `rescue_from` block, the environment of the original controller method(`.self` receiver) is accessible through the `#context` method.

```ruby
class Twitter::API < Grape::API
rescue_from :all do |e|
user_id = context.params[:user_id]
error!("error for #{user_id}")
end
end
```

#### Rescuing exceptions inside namespaces

You could put `rescue_from` clauses inside a namespace and they will take precedence over ones
Expand Down Expand Up @@ -3121,6 +3132,8 @@ end

Use [Doorkeeper](https://github.com/doorkeeper-gem/doorkeeper), [warden-oauth2](https://github.com/opperator/warden-oauth2) or [rack-oauth2](https://github.com/nov/rack-oauth2) for OAuth2 support.

You can access the controller params, headers, and helpers through the context with the `#context` method inside any auth middleware inherited from `Grape::Middlware::Auth::Base`.

## Describing and Inspecting an API

Grape routes can be reflected at runtime. This can notably be useful for generating documentation.
Expand Down Expand Up @@ -3433,6 +3446,8 @@ class API < Grape::API
end
```

You can access the controller params, headers, and helpers through the context with the `#context` method inside any middleware inherited from `Grape::Middlware::Base`.

### Rails Middleware

Note that when you're using Grape mounted on Rails you don't have to use Rails middleware because it's already included into your middleware stack.
Expand Down
1 change: 1 addition & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ module Middleware
autoload :Error
autoload :Globals
autoload :Stack
autoload :Helpers
end

module Auth
Expand Down
6 changes: 2 additions & 4 deletions lib/grape/middleware/auth/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ module Grape
module Middleware
module Auth
class Base
include Helpers

attr_accessor :options, :app, :env

def initialize(app, **options)
@app = app
@options = options
end

def context
env[Grape::Env::API_ENDPOINT]
end

def call(env)
dup._call(env)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/grape/middleware/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Grape
module Middleware
class Base
include Helpers

attr_reader :app, :env, :options
TEXT_HTML = 'text/html'.freeze

Expand Down
10 changes: 10 additions & 0 deletions lib/grape/middleware/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Grape
module Middleware
# Common methods for all types of Grape middleware
module Helpers
def context
env[Grape::Env::API_ENDPOINT]
end
end
end
end
8 changes: 8 additions & 0 deletions spec/grape/middleware/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@
end
end

describe '#context' do
subject { Grape::Middleware::Base.new(blank_app) }
it 'allows access to response context' do
subject.call(Grape::Env::API_ENDPOINT => { header: 'some header' })
expect(subject.context).to eq(header: 'some header')
end
end

context 'options' do
it 'persists options passed at initialization' do
expect(Grape::Middleware::Base.new(blank_app, abc: true).options[:abc]).to be true
Expand Down