Class: PublishingPlatformLocation

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/publishing_platform_location.rb,
lib/publishing_platform_location/version.rb

Defined Under Namespace

Classes: NoConfigurationError

Constant Summary collapse

DEV_DOMAIN =

The fallback parent domain to use in development mode.

"dev.publishing-platform.co.uk"
VERSION =
"0.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain_to_use = nil, external_domain = nil) ⇒ PublishingPlatformLocation

Construct a new PublishingPlatformLocation instance.



16
17
18
19
20
# File 'lib/publishing_platform_location.rb', line 16

def initialize(domain_to_use = nil, external_domain = nil)
  @parent_domain = domain_to_use || env_var_or_fallback("PUBLISHING_PLATFORM_APP_DOMAIN", DEV_DOMAIN) # empty string for internal services
  @external_domain = external_domain || ENV.fetch("PUBLISHING_PLATFORM_APP_DOMAIN_EXTERNAL", @parent_domain)
  @host_prefix = ENV.fetch("PUBLISHING_PLATFORM_LOCATION_HOSTNAME_PREFIX", "")  
end

Instance Attribute Details

#external_domainObject (readonly)

Returns the value of attribute external_domain.



13
14
15
# File 'lib/publishing_platform_location.rb', line 13

def external_domain
  @external_domain
end

#host_prefixObject (readonly)

Returns the value of attribute host_prefix.



13
14
15
# File 'lib/publishing_platform_location.rb', line 13

def host_prefix
  @host_prefix
end

#parent_domainObject (readonly)

Returns the value of attribute parent_domain.



13
14
15
# File 'lib/publishing_platform_location.rb', line 13

def parent_domain
  @parent_domain
end

Instance Method Details

#external_url_for(service, options = {}) ⇒ Object

Find the external URL for a service/application.



45
46
47
# File 'lib/publishing_platform_location.rb', line 45

def external_url_for(service, options = {})
  find(service, options.merge(external: true))
end

#find(service, options = {}) ⇒ Object

Find the base URL for a service/application.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/publishing_platform_location.rb', line 23

def find(service, options = {})
  name = valid_service_name(service)

  name = "#{host_prefix}#{name}"

  if (service_uri = defined_service_uri_for(name))
    return service_uri
  end    

  domain = options[:external] ? external_domain : parent_domain
  domain_suffix = domain.empty? ? "" : ".#{domain}" # empty string for internal services

  scheme = if options[:force_http] || http_domain?(domain)
             "http:"
           else
             "https:"
           end

  "#{scheme}//#{name}#{domain_suffix}".freeze
end

#website_rootObject

Find the base URL for the public website frontend.



50
51
52
# File 'lib/publishing_platform_location.rb', line 50

def website_root
  env_var_or_fallback("PUBLISHING_PLATFORM_WEBSITE_ROOT") { find("www") }
end