Class: CypressRails::Server::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/cypress-rails/server/middleware.rb

Defined Under Namespace

Classes: Counter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, server_errors, extra_middleware = []) ⇒ Middleware

Returns a new instance of Middleware.



29
30
31
32
33
34
35
36
# File 'lib/cypress-rails/server/middleware.rb', line 29

def initialize(app, server_errors, extra_middleware = [])
  @app = app
  @extended_app = extra_middleware.inject(@app) { |ex_app, klass|
    klass.new(ex_app)
  }
  @counter = Counter.new
  @server_errors = server_errors
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



27
28
29
# File 'lib/cypress-rails/server/middleware.rb', line 27

def error
  @error
end

Instance Method Details

#call(env) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cypress-rails/server/middleware.rb', line 50

def call(env)
  if env["PATH_INFO"] == "/__identify__"
    [200, {}, [@app.object_id.to_s]]
  else
    @counter.increment(env["REQUEST_URI"])
    begin
      @extended_app.call(env)
    rescue *@server_errors => e
      @error ||= e
      raise e
    ensure
      @counter.decrement(env["REQUEST_URI"])
    end
  end
end

#clear_errorObject



46
47
48
# File 'lib/cypress-rails/server/middleware.rb', line 46

def clear_error
  @error = nil
end

#pending_requestsObject



38
39
40
# File 'lib/cypress-rails/server/middleware.rb', line 38

def pending_requests
  @counter.value
end

#pending_requests?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/cypress-rails/server/middleware.rb', line 42

def pending_requests?
  @counter.positive?
end