module HealthCheck
  class MiddlewareHealthcheck

    def initialize(app)
      @app = app
    end

    def call(env)
      uri = env['PATH_INFO']
      if uri =~ /^#{HealthCheck.uri}\/([-_\w]*)\bmiddleware\b([-_\w]*)/
        checks = $1 + ($1 != '' && $2 != '' ? '_' : '') + $2
        checks = 'standard' if checks == ''
        response_type = uri[/\.(json|xml)/,1] || 'plain'
        response_method = 'response_' + response_type
        begin
          errors = HealthCheck::Utils.process_checks(checks)
        rescue => e
          errors = e.message.blank? ? e.class.to_s : e.message.to_s
        end
        if errors.blank?
          send(response_method, 200, HealthCheck.success, true)
        else
          msg = "health_check failed: #{errors}"
          send(response_method, 500, msg, false)
        end
      else
        @app.call(env)
      end
    end

    def response_json code, msg, healthy
      obj = { healthy: healthy, message: msg }
      [ code, { 'Content-Type' => 'application/json' }, [obj.to_json] ]
    end

    def response_xml code, msg, healthy
      obj = { healthy: healthy, message: msg }
      [ code, { 'Content-Type' => 'text/xml' }, [obj.to_xml] ]
    end

    def response_plain code, msg, healthy
      [ code, { 'Content-Type' => 'text/plain' }, [msg] ]
    end
  end
end

module HealthCheck
  class MiddlewareHealthcheck

    def initialize(app)
      @app = app
    end

    def call(env)
      uri = env['PATH_INFO']
      if uri =~ /^\/?#{HealthCheck.uri}\/([-_0-9a-zA-Z]*)middleware_?([-_0-9a-zA-Z]*)(\.(\w*))?/
        checks = $1 + ($1 != '' && $2 != '' ? '_' : '') + $2
        checks = 'standard' if checks == ''
        response_type = $4
        begin
          errors = HealthCheck::Utils.process_checks(checks)
        rescue => e
          errors = e.message.blank? ? e.class.to_s : e.message.to_s
        end
        healthy = errors.blank?
        msg = healthy ? HealthCheck.success : "health_check failed: #{errors}"
        if response_type == 'xml'
          content_type = 'text/xml'
          msg = { healthy: healthy, message: msg }.to_xml
        elsif response_type == 'json'
          content_type = 'application/json'
          msg = { healthy: healthy, message: msg }.to_json
        else
          content_type = 'text/plain'
        end
        [ (healthy ? 200 : 500), { 'Content-Type' => content_type }, [msg] ]
      else
        @app.call(env)
      end
    end

  end
end

Remove indirection to a response method; Fix url parsing
Remove indirection to a response method;
Fix url parsing
