Sha256: c60f75e47d5f41d94e20c6e535a278d590501cb48fc10fc64cf0bba048424666

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module CopyTunerClient
  # Rack middleware that synchronizes with CopyTuner during each request.
  #
  # This is injected into the Rails middleware stack in development environments.
  class RequestSync
    # @param app [Rack] the upstream app into whose responses to inject the editor
    # @param options [Hash]
    # @option options [Cache] :cache agent that should be flushed after each request
    def initialize(app, options)
      @app  = app
      @cache = options[:cache]
      @interval = options[:interval]
      @last_synced = options[:last_synced]
    end

    attr_accessor :last_synced

    # Invokes the upstream Rack application and flushes the cache after each
    # request.
    def call(env)
      @cache.download unless asset_request?(env) or in_interval?
      response = @app.call(env)
      @cache.flush    unless asset_request?(env) or in_interval?
      update_last_synced unless in_interval?
      response
    end

    private
    def asset_request?(env)
      env['PATH_INFO'] =~ /^\/assets/
    end

    def in_interval?
      return false if @last_synced.nil?
      return false if @interval <= 0
      @last_synced + @interval > Time.now.utc
    end

    def update_last_synced
      @last_synced = Time.now.utc
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
copy_tuner_client-0.0.3 lib/copy_tuner_client/request_sync.rb
copy_tuner_client-0.0.2 lib/copy_tuner_client/request_sync.rb