Class: Racknga::Middleware::InstanceName
- Inherits:
-
Object
- Object
- Racknga::Middleware::InstanceName
- Defined in:
- lib/racknga/middleware/instance_name.rb
Overview
This is a middleware that adds “X-Responsed-By” header to responses. It’s useful to determine responded server when your Rack applications are deployed behind load balancers.
Usage:
require "racknga"
use Racknga::Middleware::InstanceName
run YourApplication
Constant Summary collapse
- CURRENT_BRANCH_MARKER =
/\A\* /
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
Instance Method Summary collapse
- #application_name ⇒ Object
- #branch ⇒ Object
-
#call(environment) ⇒ Object
For Rack.
-
#initialize(application, options = {}) ⇒ InstanceName
constructor
A new instance of InstanceName.
- #revision ⇒ Object
- #ruby ⇒ Object
- #server ⇒ Object
- #user ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(application, options = {}) ⇒ InstanceName
Returns a new instance of InstanceName.
32 33 34 35 36 37 38 |
# File 'lib/racknga/middleware/instance_name.rb', line 32 def initialize(application, ={}) @application = application @options = @header = construct_header.freeze @headers = construct_headers.freeze end |
Instance Attribute Details
#header ⇒ Object (readonly)
Returns the value of attribute header.
31 32 33 |
# File 'lib/racknga/middleware/instance_name.rb', line 31 def header @header end |
Instance Method Details
#application_name ⇒ Object
51 52 53 |
# File 'lib/racknga/middleware/instance_name.rb', line 51 def application_name @options[:application_name] || @application.class.name end |
#branch ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/racknga/middleware/instance_name.rb', line 72 def branch current_branch = nil `git branch -a`.each_line do |line| case line when CURRENT_BRANCH_MARKER current_branch = line.sub(CURRENT_BRANCH_MARKER, "").strip break end end current_branch end |
#call(environment) ⇒ Object
For Rack.
41 42 43 44 45 46 47 48 49 |
# File 'lib/racknga/middleware/instance_name.rb', line 41 def call(environment) response = @application.call(environment).to_a [ response[0], response[1].merge(@headers), response[2], ] end |
#revision ⇒ Object
59 60 61 |
# File 'lib/racknga/middleware/instance_name.rb', line 59 def revision `git describe --abbrev=7 HEAD`.strip # XXX be SCM-agonostic end |
#ruby ⇒ Object
84 85 86 |
# File 'lib/racknga/middleware/instance_name.rb', line 84 def ruby RUBY_DESCRIPTION end |
#server ⇒ Object
63 64 65 |
# File 'lib/racknga/middleware/instance_name.rb', line 63 def server `hostname`.strip end |
#user ⇒ Object
67 68 69 |
# File 'lib/racknga/middleware/instance_name.rb', line 67 def user `id --user --name`.strip end |
#version ⇒ Object
55 56 57 |
# File 'lib/racknga/middleware/instance_name.rb', line 55 def version @options[:version] end |