Module: Goliath::Rack::SimpleAroundware

Includes:
Validator
Included in:
BarrierAroundware
Defined in:
lib/goliath/rack/simple_aroundware.rb

Overview

This module gives you ergonomics similar to traditional Rack middleware:

  • Use instance variables! Each SimpleAroundware is unique to its request.

  • You have accessors for env and (once in post_process) status, headers, body – no more shipping them around to every method.

If in your traditional rack middleware you’d do this:

class MyRackMiddleware
  def call(env)
    get_ready_to_be_totally_awesome()
    status, headers, body = @app.call(env)
    new_body = make_totally_awesome(body)
    [status, headers, new_body]
  end
end

You’d now do this:

class MyAwesomeAroundware
  include Goliath::Rack::SimpleAroundware
  def pre_process
    get_ready_to_be_totally_awesome()
  end
  def post_process
    new_body = make_totally_awesome(body)
    [status, headers, new_body]
  end
end

And you’d include it in your endpoint like this:

class AwesomeApi < Goliath::API
  use Goliath::Rack::SimpleAroundwareFactory, MyAwesomeAroundware
end

Examples:

# count incoming requests, outgoing responses, and
# outgoing responses by status code
class StatsdLogger
  include Goliath::Rack::SimpleAroundware
  def pre_process
    statsd_count("reqs.#{config['statsd_name']}.in")
    Goliath::Connection::AsyncResponse
  end
  def post_process
    statsd_count("reqs.#{config['statsd_name']}.out")
    statsd_count("reqs.#{config['statsd_name']}.#{status}")
    [status, headers, body]
  end
  def statsd_count(name, count=1, sampling_frac=nil)
    # ...
  end
end

class AwesomeApiWithLogging < Goliath::API
  use Goliath::Rack::Params
  use Goliath::Rack::SimpleAroundwareFactory, StatsdLogger
  def response(env)
    # ... do something awesome
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validator

safely, validation_error

Instance Attribute Details

#bodyObject

The response, set by the SimpleAroundware’s downstream



74
75
76
# File 'lib/goliath/rack/simple_aroundware.rb', line 74

def body
  @body
end

#envObject (readonly)

The request environment, set in the initializer



72
73
74
# File 'lib/goliath/rack/simple_aroundware.rb', line 72

def env
  @env
end

#headersObject

The response, set by the SimpleAroundware’s downstream



74
75
76
# File 'lib/goliath/rack/simple_aroundware.rb', line 74

def headers
  @headers
end

#statusObject

The response, set by the SimpleAroundware’s downstream



74
75
76
# File 'lib/goliath/rack/simple_aroundware.rb', line 74

def status
  @status
end

Instance Method Details

#accept_response(handle, resp_succ, resp) ⇒ Object

On receipt of an async result,

  • call the setter for that handle if any (on receipt of :shortened_url,



108
109
110
# File 'lib/goliath/rack/simple_aroundware.rb', line 108

def accept_response(handle, resp_succ, resp)
  self.downstream_resp = resp
end

#downstream_resp=(status_headers_body) ⇒ Object

Virtual setter for the downstream middleware/endpoint response



102
103
104
# File 'lib/goliath/rack/simple_aroundware.rb', line 102

def downstream_resp=(status_headers_body)
  @status, @headers, @body = status_headers_body
end

#initialize(env) ⇒ Goliath::Rack::SimpleAroundware

Parameters:

Returns:



78
79
80
# File 'lib/goliath/rack/simple_aroundware.rb', line 78

def initialize(env)
  @env = env
end

#post_processArray

Override this method in your middleware to perform any postprocessing. This will only be invoked when all deferred requests (including the response) have completed.

Returns:

  • (Array)

    array contains [status, headers, body]



97
98
99
# File 'lib/goliath/rack/simple_aroundware.rb', line 97

def post_process
  [status, headers, body]
end

#pre_processArray

Override this method in your middleware to perform any preprocessing (launching a deferred request, perhaps).

You must return Goliath::Connection::AsyncResponse if you want processing to continue

Returns:

  • (Array)

    array contains [status, headers, body]



88
89
90
# File 'lib/goliath/rack/simple_aroundware.rb', line 88

def pre_process
  Goliath::Connection::AsyncResponse
end