Skip to content

Commit

Permalink
Allow headers to be cached along with the response
Browse files Browse the repository at this point in the history
Garner doesn't support this out of the box. I applied this monkey patch:
artsy/garner#54
  • Loading branch information
monfresh committed Sep 21, 2013
1 parent b3438b8 commit 733ca2d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions config/initializers/garner.rb
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

0 comments on commit 733ca2d

Please sign in to comment.