Class: CopyTunerClient::RequestSync

Inherits:
Object
  • Object
show all
Defined in:
lib/copy_tuner_client/request_sync.rb

Overview

Rack middleware that synchronizes with CopyTuner during each request.

This is injected into the Rails middleware stack in development environments.

Constant Summary collapse

VIEW_PATH =
File.expand_path('../../../ui/views/', __FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ RequestSync

Returns a new instance of RequestSync.

Parameters:

  • app (Rack)

    the upstream app into whose responses to inject the editor

  • options (Hash)

Options Hash (options):

  • :cache (Cache)

    agent that should be flushed after each request



17
18
19
20
21
22
23
# File 'lib/copy_tuner_client/request_sync.rb', line 17

def initialize(app, options)
  @app = app
  @cache = options[:cache]
  @interval = options[:interval]
  @ignore_regex = options[:ignore_regex]
  @last_synced = options[:last_synced]
end

Instance Attribute Details

#last_syncedObject

Returns the value of attribute last_synced.



25
26
27
# File 'lib/copy_tuner_client/request_sync.rb', line 25

def last_synced
  @last_synced
end

Instance Method Details

#call(env) ⇒ Object

Invokes the upstream Rack application and flushes the cache after each request.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/copy_tuner_client/request_sync.rb', line 29

def call(env)
  if /^\/copytuner/ =~ ::Rack::Request.new(env).path_info
    dup._call(env)
  else
    @cache.download unless cancel_sync?(env)
    response = @app.call(env)
    @cache.flush    unless cancel_sync?(env)
    update_last_synced unless in_interval?
    response
  end
end