Class: HireFire::Middleware

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

Constant Summary collapse

TEST_HEADERS =

Frozen headers to save some memory

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

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.

Parameters:

  • app. (ActionDispatch::Routing::RouteSet)


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

def initialize(app)
  @app   = app
  @token = ENV["HIREFIRE_TOKEN"]
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.

Parameters:

  • env (Hash)

    containing request information.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hirefire/middleware.rb', line 30

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.

Returns:

  • (text/html, application/json)

    based on whether the ‘test` or `info` url was requested.



50
51
52
53
54
55
56
# File 'lib/hirefire/middleware.rb', line 50

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