Skip to content

Instantly share code, notes, and snippets.

@RouL
Forked from toolmantim/host_based_tld_length.rb
Created June 21, 2017 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RouL/7605fa0d0de194205ce7a62fe80afecf to your computer and use it in GitHub Desktop.
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
# 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