class EsReadModel::RackSubscriber

Attributes

status[R]

Public Class Methods

new(app, options) click to toggle source
# File lib/es_readmodel/rack_subscriber.rb, line 11
def initialize(app, options)
  @app = app
  @listener = options[:listener]
  @subscriber = Subscriber.new(options)
  Thread.new { @subscriber.subscribe }
end

Public Instance Methods

call(env) click to toggle source
# File lib/es_readmodel/rack_subscriber.rb, line 18
def call(env)
  @request = Rack::Request.new(env)
  if env['PATH_INFO'] == '/status'
    status, headers, body = json_response(200, @subscriber.status)
  else
    env['readmodel.state'] = @subscriber.state
    env['readmodel.available'] = @subscriber.status[:available]
    env['readmodel.status'] = 'OK'
    status, headers, body = @app.call(env)
  end
  @listener.call({
    level:  'info',
    tag:    'http.request',
    msg:    "#{env['REQUEST_METHOD']} #{@request.fullpath}",
    status: status
  })
  [status, headers, body]
end

Private Instance Methods

json_response(status_code, body) click to toggle source
# File lib/es_readmodel/rack_subscriber.rb, line 39
def json_response(status_code, body)
  result = body.merge({
    _links: { self: @request.fullpath }
  })
  [
    status_code,
    {
      'Content-Type' => 'application/json'
    },
    [result.to_json]
  ]
end