Class: Orange::Middleware::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/orange-core/middleware/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, core, *args) ⇒ Base

Initialize will set the core and downstream app, then call init subclasses should override init instead of initialize. If the subclass defines a stack_init, then it will be registered as a stack_loaded event.

Parameters:

  • app (Object)

    a downstream app

  • core (Orange::Core)

    the orange core

  • args (optional, Array)

    any arguments



20
21
22
23
24
25
# File 'lib/orange-core/middleware/base.rb', line 20

def initialize(app, core, *args)
  @app = app
  @core = core
  init(*args)
  core.middleware(self)
end

Instance Method Details

#call(env) ⇒ Array

The standard Rack “call”. By default, Orange Middleware wraps the env into an Orange::Packet and passes it on to #packet_call. Subclasses will typically override packet_call rather than overriding call directly.

Orange Middleware should expect to have this method ignored by upstream Orange-aware apps in favor of calling packet_call directly.

Parameters:

  • env (Hash)

    the hash of environment variables given by the rack interface.

Returns:

  • (Array)

    the standard Rack striplet of status, headers and content



42
43
44
45
# File 'lib/orange-core/middleware/base.rb', line 42

def call(env)
  packet = Orange::Packet.new(@core, env)
  packet_call(packet)
end

#init(*args) ⇒ void

This method returns an undefined value.

A stub method that subclasses can override to handle initialization For initialization



30
31
# File 'lib/orange-core/middleware/base.rb', line 30

def init(*args)
end

#inspectString

Help stack traces

Returns:

  • (String)

    string representing this middleware (#to_s)



92
93
94
# File 'lib/orange-core/middleware/base.rb', line 92

def inspect
  self.to_s
end

#orangeOrange::Core

Accessor for @core, which is the stack’s instance of Orange::Core

Returns:



88
# File 'lib/orange-core/middleware/base.rb', line 88

def orange;     @core;    end

#packet_call(packet) ⇒ Array

Like the standard call, but with the env hash already wrapped into a Packet This is called automatically as part of #call, so subclasses can have a packet without having to initialize it. It will be called directly by Orange-aware upstream middleware, skipping the step of initializing the packet during #call.

Passing the packet downstream should be done with #pass rather than the Rack standard @app.call, since #pass will take the packet and do a #packet_call if possible.

Parameters:

Returns:

  • (Array)

    the standard Rack striplet of status, headers and content



57
58
59
# File 'lib/orange-core/middleware/base.rb', line 57

def packet_call(packet)
  pass packet
end

#pass(packet) ⇒ Array

Pass will sent the packet to the downstream app by calling call or packet call. Calling pass on a packet is the preferred way to call downstream apps, as it will call packet_call directly if possible (to avoid reinitializing the packet)

Parameters:

Returns:

  • (Array)

    the standard Rack striplet of status, headers and content



66
67
68
69
70
71
72
# File 'lib/orange-core/middleware/base.rb', line 66

def pass(packet)
  if @app.respond_to?(:packet_call)
    @app.packet_call(packet)
  else
    recapture(@app.call(packet.env), packet)
  end
end

#recapture(response, packet) ⇒ Array

After the pass has been completed, we should recapture the contents and make sure they are placed in the packet, in case the downstream app is not Orange aware.

Parameters:

  • the (Array)

    standard Rack striplet of status, headers and content

  • packet (Orange::Packet)

    the packet to pass to downstream apps

Returns:

  • (Array)

    the standard Rack striplet of status, headers and content



79
80
81
82
83
84
# File 'lib/orange-core/middleware/base.rb', line 79

def recapture(response, packet)
  packet[:status]  = response[0]
  packet[:headers] = response[1]
  packet[:content] = response[2].first
  response
end