-
-
Save RouL/7605fa0d0de194205ce7a62fe80afecf to your computer and use it in GitHub Desktop.
Reconfigures Rails ActionDispatch's TLD handling dynamically based on the request host, so you don't have to mess with config.action_dispatch.tld_length for cross-device testing using xip.io and friends
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Reconfigures ActionDispatch's TLD handling dynamically based on the request | |
# host, so you don't have to mess with config.action_dispatch.tld_length for | |
# cross-device testing using xip.io and friends | |
# | |
# Examples: | |
# use Rack::HostBasedTldLength, /xip\.io/, 5 | |
class Rack::HostBasedTldLength | |
def initialize(app, host_pattern, host_tld_length) | |
@app = app | |
@host_pattern = Regexp.new(host_pattern) | |
@host_tld_length = host_tld_length | |
end | |
def call(env) | |
original_tld_length = tld_length | |
request = Rack::Request.new(env) | |
set_tld_length(@host_tld_length) if request.host =~ @host_pattern | |
@app.call(env) | |
ensure | |
set_tld_length(original_tld_length) | |
end | |
private | |
def tld_length | |
ActionDispatch::Http::URL.tld_length | |
end | |
def set_tld_length(length) | |
ActionDispatch::Http::URL.tld_length = length | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment