Sha256: f5cae091bee4978b4a4b1227cc8949950aaaf8ddcbaf531db97ad18c9a4e232f

Contents?: true

Size: 1.21 KB

Versions: 7

Compression:

Stored size: 1.21 KB

Contents

require 'wovnrb/custom_domain/custom_domain_lang'

module Wovnrb
  # Represents a list of custom domains with corresponding languages
  class CustomDomainLangs
    def initialize(setting)
      @custom_domain_langs = setting.map do |lang_code, config|
        parsed_uri = Addressable::URI.parse(add_protocol_if_needed(config['url']))
        CustomDomainLang.new(parsed_uri.host, parsed_uri.path, lang_code)
      end
    end

    def custom_domain_lang_by_lang(lang_code)
      @custom_domain_langs.find { |c| c.lang == lang_code }
    end

    def custom_domain_lang_by_url(uri)
      parsed_uri = Addressable::URI.parse(add_protocol_if_needed(uri))

      # "/" path will naturally match every URL, so by comparing longest paths first we will get the best match
      @custom_domain_langs
        .sort_by { |c| -c.path.length }
        .find { |c| c.match?(parsed_uri) }
    end

    def to_html_swapper_hash
      result = {}
      @custom_domain_langs.each do |custom_domain_lang|
        result[custom_domain_lang.host_and_path_without_trailing_slash] = custom_domain_lang.lang
      end
      result
    end

    private

    def add_protocol_if_needed(url)
      url.match?(%r{https?://}) ? url : "http://#{url}"
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
wovnrb-3.11.1 lib/wovnrb/custom_domain/custom_domain_langs.rb
wovnrb-3.11.0 lib/wovnrb/custom_domain/custom_domain_langs.rb
wovnrb-3.10.3 lib/wovnrb/custom_domain/custom_domain_langs.rb
wovnrb-3.10.2 lib/wovnrb/custom_domain/custom_domain_langs.rb
wovnrb-3.10.1 lib/wovnrb/custom_domain/custom_domain_langs.rb
wovnrb-3.10.0 lib/wovnrb/custom_domain/custom_domain_langs.rb
wovnrb-3.9.0 lib/wovnrb/custom_domain/custom_domain_langs.rb