tag_manager.rb 791 B

123456789101112131415161718192021222324252627282930313233
  1. # frozen_string_literal: true
  2. require 'singleton'
  3. class TagManager
  4. include Singleton
  5. include RoutingHelper
  6. def web_domain?(domain)
  7. domain.nil? || domain.delete_suffix('/').casecmp(Rails.configuration.x.web_domain).zero?
  8. end
  9. def local_domain?(domain)
  10. domain.nil? || domain.delete_suffix('/').casecmp(Rails.configuration.x.local_domain).zero?
  11. end
  12. def normalize_domain(domain)
  13. return if domain.nil?
  14. uri = Addressable::URI.new
  15. uri.host = domain.delete_suffix('/')
  16. uri.normalized_host
  17. end
  18. def local_url?(url)
  19. uri = Addressable::URI.parse(url).normalize
  20. domain = uri.host + (uri.port ? ":#{uri.port}" : '')
  21. TagManager.instance.web_domain?(domain)
  22. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  23. false
  24. end
  25. end