Skip to content
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

Added support of unchanged requests #433

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 lib/her/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "her/middleware/first_level_parse_json"
require "her/middleware/second_level_parse_json"
require "her/middleware/accept_json"
require "her/middleware/cache_unmodified"

module Her
module Middleware
Expand Down
74 changes: 74 additions & 0 deletions lib/her/middleware/cache_unmodified.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Easy way to begin support 304 status, so on ther side if data was not changed
# You will load it from local cache. Of course, other side must support
# Of course, other side must support 304 status also.
# Here is Rails controller example with this support:
#
# class Post < ApplicationController
# before_action :check_changes
#
# private
#
# def check_changes
# return unless request.headers['HTTP_IF_MODIFIED_SINCE']
# return if request.headers['HTTP_IF_MODIFIED_SINCE'].to_datetime > resource.updated_at
# render body: nil, status: 304
# end
# end
class Her::Middleware::CacheUnmodified < Faraday::Middleware
SAVE_METHODS = [:get, :head].freeze

attr_reader :cache, :options
attr_accessor :url

# apply with optinos:
# cache # default value is Rails.cache if Rails is used
# cache_key_prefix # defauilt is nil

def initialize(app, options={})
@app = app
@cache = options[:cache]
@cache ||= Rails.cache if defined?(Rails)
@options = options
end

def call(env)
return @app.call(env) unless SAVE_METHODS.include?(env[:method])
self.url = env.url.to_s
env[:request_headers]["If-Modified-Since"] ||= cached_time if cached_time

@app.call(env).on_complete do
if env[:status] == 304
if cached_body
env[:body] = cached_body
# with out this hack Her crushes
env[:status] = 200
end
elsif env[:status] == 200
cache.write cache_key_body, env[:body], expires_in: cache_key_ttl
cache.write cache_key_time, Time.zone.now.httpdate, expires_in: cache_key_ttl
end
end
end

private

def cache_key_time
[options[:cache_key_prefix], "time", url].compact.join("/")
end

def cache_key_body
[options[:cache_key_prefix], "body", url].compact.join("/")
end

def cached_time
cache.read(cache_key_time)
end

def cached_body
cache.read(cache_key_body)
end

def cache_key_ttl
1.week.freeze
end
end