class Shack::Middleware

Constants

HEADER_NAME

Attributes

configuration[W]

Public Class Methods

config() click to toggle source
# File lib/shack/middleware.rb, line 56
def config
  @configuration ||= Configuration.new
end
configure(&block) click to toggle source

Allow configuration of the Middleware

Shack::Middleware.configure do |shack|
  shack.sha = "thisiasha"
  shack.hide_stamp = true
end
# File lib/shack/middleware.rb, line 67
def configure(&block)
  block.call(config)
end
new(app, sha = "") click to toggle source
# File lib/shack/middleware.rb, line 5
def initialize(app, sha = "")
  @app = app
  @sha = sha unless (sha || "").empty?
end

Public Instance Methods

call(env) click to toggle source
# File lib/shack/middleware.rb, line 10
def call(env)
  # Make it easy to get sha from other processes
  Shack.sha = sha
  status, headers, body = @app.call(env)
  return [status, headers, body] unless sha
  headers[HEADER_NAME] = sha

  if result = inject_stamp(status, headers, body)
    result
  else
    [status, headers, body]
  end
end
config() click to toggle source
# File lib/shack/middleware.rb, line 24
def config
  self.class.config
end
inject_stamp(status, headers, body) click to toggle source
# File lib/shack/middleware.rb, line 28
def inject_stamp(status, headers, body)
  return nil if !!config.hide_stamp?
  return nil unless Stamp.stampable?(headers)
  response = Rack::Response.new([], status, headers)

  if String === body
    response.write stamped(body)
  else
    body.each do |fragment|
      response.write stamped(fragment)
    end
  end
  body.close if body.respond_to? :close
  response.finish
end
sha() click to toggle source

Initialiser over config-sha.

# File lib/shack/middleware.rb, line 45
def sha
  @sha || config.sha
end
stamped(body) click to toggle source
# File lib/shack/middleware.rb, line 49
def stamped(body)
  Stamp.new(body, sha, config).result
end