Class: Wovnrb::CustomDomainLangs

Inherits:
Object
  • Object
show all
Defined in:
lib/wovnrb/custom_domain/custom_domain_langs.rb

Overview

Represents a list of custom domains with corresponding languages

Instance Method Summary collapse

Constructor Details

#initialize(setting) ⇒ CustomDomainLangs

Returns a new instance of CustomDomainLangs.



6
7
8
9
10
11
# File 'lib/wovnrb/custom_domain/custom_domain_langs.rb', line 6

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

Instance Method Details

#custom_domain_lang_by_lang(lang_code) ⇒ Object



13
14
15
# File 'lib/wovnrb/custom_domain/custom_domain_langs.rb', line 13

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

#custom_domain_lang_by_url(uri) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/wovnrb/custom_domain/custom_domain_langs.rb', line 17

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

#to_html_swapper_hashObject



26
27
28
29
30
31
32
# File 'lib/wovnrb/custom_domain/custom_domain_langs.rb', line 26

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