Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Middleware Response Handler
class HealthCheck
class Middleware
def initialize(application)
@application = application
end
def call(environment)
if environment['PATH_INFO'] == '/health-check'
if HealthCheck.healthy?
[200, {}, ['OK']]
else
[500, {}, ['PROBLEM']]
end
else
@application.call(environment)
end
end
end
end
module Mobile
class Format
def initialize(application)
@application = application
end
def call(environment)
request = Rack::Request.new(environment)
user_agent = ::Mobile::UserAgent.new(request.env['HTTP_USER_AGENT'])
if request.params['mobile'] == '1' || user_agent.mobile?
request.env['HTTP_ACCEPT'] = 'text/html+mobile'
end
status, headers, body = @application.call(request.env)
response = Rack::Response.new(body, status, headers)
response.finish
end
end
end
module Mobile
class Format
def initialize(application)
@application = application
end
def call(environment)
request = Rack::Request.new(environment)
set_mobile_accept_header(request) if serve_mobile?(request)
response = response(request)
response.finish
end
private
def user_agent(request)
Mobile::UserAgent.new(request.env['HTTP_USER_AGENT'])
end
def mobile_requested?(request)
request.params['mobile'] == '1'
end
def serve_mobile?(request)
mobile_requested?(request) || user_agent(request).mobile?
end
def set_mobile_accept_header(request)
request.env['HTTP_ACCEPT'] = 'text/html+mobile'
end
def response(request)
status, headers, body = @application.call(request.env)
Rack::Response.new(body, status, headers)
end
end
end
module Mobile
class Format
def initialize(application)
@application = application
end
def call(environment)
@environment = environment
set_mobile_accept_header if serve_mobile?
response.finish
end
private
def request
@request ||= Rack::Request.new(@environment)
end
def user_agent
Mobile::UserAgent.new(request.env['HTTP_USER_AGENT'])
end
def mobile_requested?
request.params['mobile'] == '1'
end
def serve_mobile?
mobile_requested? || user_agent.mobile?
end
def set_mobile_accept_header
request.env['HTTP_ACCEPT'] = 'text/html+mobile'
end
def response
status, headers, body = @application.call(request.env)
Rack::Response.new(body, status, headers)
end
end
end
module Mobile
class Format
def initialize(application)
@application = application
end
def call(environment)
responder = Mobile::Format::Responder.new(@application, environment)
responder.respond
end
class Responder
def initialize(application, environment)
@application = application
@environment = environment
end
def respond
set_mobile_accept_header if serve_mobile?
response.finish
end
private
def request
@request ||= Rack::Request.new(@environment)
end
def user_agent
::Mobile::UserAgent.new(request.env['HTTP_USER_AGENT'])
end
def mobile_requested?
request.params['mobile'] == '1'
end
def serve_mobile?
mobile_requested? || user_agent.mobile?
end
def set_mobile_accept_header
request.env['HTTP_ACCEPT'] = 'text/html+mobile'
end
def response
@response ||= begin
status, headers, body = @application.call(request.env)
Rack::Response.new(body, status, headers)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.