-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow headers to be cached along with the response
Garner doesn't support this out of the box. I applied this monkey patch: artsy/garner#54
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,52 @@ | ||
require "garner" | ||
require "garner/mixins/mongoid" | ||
require "garner/mixins/rack" | ||
|
||
module Mongoid | ||
module Document | ||
include Garner::Mixins::Mongoid::Document | ||
end | ||
end | ||
|
||
# Monkey patch to cache headers until this is built into Garner | ||
# https://github.com/artsy/garner/issues/54 | ||
module Garner | ||
module Cache | ||
class Identity | ||
TRACKED_HEADERS = %w{ Link X-Total-Count X-Total-Pages X-Current-Page X-Next-Page X-Previous-Page } | ||
|
||
alias_method :_fetch, :fetch | ||
def fetch(&block) | ||
if @key_hash[:tracked_grape_headers] | ||
result, headers = _fetch do | ||
result = yield | ||
headers = @ruby_context.header.slice(*TRACKED_HEADERS) if @ruby_context.respond_to?(:header) | ||
[result, headers] | ||
end | ||
|
||
(headers || {}).each do |key, value| | ||
@ruby_context.header(key, value) if @ruby_context.respond_to?(:header) | ||
end | ||
|
||
result | ||
else | ||
_fetch(&block) | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
||
module Garner | ||
module Mixins | ||
module Rack | ||
|
||
alias_method :_garner, :garner | ||
def garner(&block) | ||
response, headers = _garner.key({ tracked_grape_headers: true }, &block) | ||
end | ||
|
||
end | ||
end | ||
end |