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
24
25
# File 'lib/copy_tuner_client/request_sync.rb', line 17

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

Instance Attribute Details

#last_syncedObject

Returns the value of attribute last_synced.



27
28
29
# File 'lib/copy_tuner_client/request_sync.rb', line 27

def last_synced
  @last_synced
end

Instance Method Details

#call(env) ⇒ Object

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/copy_tuner_client/request_sync.rb', line 31

def call(env)
  if /^\/copytuner/ =~ ::Rack::Request.new(env).path_info
    dup._call(env)
  else
    first_request = @first
    if first_request
      @first = false
      @cache.download
    end

    cancel_sync = cancel_sync?(env)
    response = @app.call(env)
    @poller.start_sync unless first_request || cancel_sync

    update_last_synced unless in_interval?
    response
  end
end