Class: HireFire::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/hirefire/middleware.rb

Constant Summary collapse

INFO_HEADERS =
{
  "Content-Type" => "application/json",
  "Cache-Control" => "must-revalidate, private, max-age=0"
}
TEST_HEADERS =
{
  "Content-Type" => "text/html"
}

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Initialize the HireFire::Middleware and store the ‘app` in `@app` and `ENV` in `@token` for convenience.



20
21
22
23
24
25
26
# File 'lib/hirefire/middleware.rb', line 20

def initialize(app)
  @app   = app
  @token = ENV["HIREFIRE_TOKEN"]
  if defined?(Rails) && Rails.application.config.relative_url_root
    @path_prefix = Regexp.new("^" + Regexp.escape(Rails.application.config.relative_url_root))
  end
end

Instance Method Details

#call(env) ⇒ Object

Will respond to the request here if either the ‘test` or the `info` url was requested. Otherwise, fall through to the rest of the middleware below HireFire::Middleware.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hirefire/middleware.rb', line 33

def call(env)
  @env = env

  if test?
    [ 200, TEST_HEADERS, self ]
  elsif info?
    [ 200, INFO_HEADERS, self ]
  else
    @app.call(env)
  end
end

#each(&block) ⇒ text/html, application/json

Returns text/html when the ‘test` url is requested. This is purely to see whether the URL works through the HireFire command-line utility.

Returns a JSON String when the ‘info` url is requested. This url will be requested every minute by HireFire in order to fetch dyno data.



53
54
55
56
57
58
59
# File 'lib/hirefire/middleware.rb', line 53

def each(&block)
  if test?
    block.call "HireFire Middleware Found!"
  elsif info?
    block.call(dynos)
  end
end