Class: Plek
- Inherits:
-
Object
- Object
- Plek
- Extended by:
- Forwardable
- Defined in:
- lib/plek.rb,
lib/plek/version.rb
Overview
Plek resolves service names to a corresponding base URL.
It does this by combining the requested service name with information from environment variables. It will raise a NoConfigurationError if a required environment variable isn’t set.
Development mode fallback defaults
When running development mode (identified by either RAILS_ENV
or RACK_ENV
environment variables being set to “development”), Plek provides some default values when the necessary environment variables aren’t set detailed below.
Defined Under Namespace
Classes: NoConfigurationError
Constant Summary collapse
- DEV_DOMAIN =
The fallback parent domain to use in development mode.
"dev.gov.uk".freeze
- VERSION =
"5.2.0".freeze
Instance Attribute Summary collapse
-
#external_domain ⇒ Object
readonly
Returns the value of attribute external_domain.
-
#parent_domain ⇒ Object
readonly
Returns the value of attribute parent_domain.
Class Method Summary collapse
-
.asset_root ⇒ String
Convenience wrapper.
-
.external_url_for ⇒ String
Convenience wrapper.
-
.find ⇒ String
Convenience wrapper.
-
.website_root ⇒ String
Convenience wrapper.
Instance Method Summary collapse
-
#asset_root ⇒ String
Find the base URL for assets.
-
#external_url_for(service, options = {}) ⇒ Object
Find the external URL for a service/application.
-
#find(service, options = {}) ⇒ String
Find the base URL for a service/application.
-
#initialize(domain_to_use = nil, external_domain = nil) ⇒ Plek
constructor
Construct a new Plek instance.
-
#website_root ⇒ String
Find the base URL for the public website frontend.
Constructor Details
#initialize(domain_to_use = nil, external_domain = nil) ⇒ Plek
Construct a new Plek instance.
37 38 39 40 41 42 43 44 |
# File 'lib/plek.rb', line 37 def initialize(domain_to_use = nil, external_domain = nil) truth_re = /^[1ty]/i @parent_domain = domain_to_use || env_var_or_dev_fallback("GOVUK_APP_DOMAIN", DEV_DOMAIN) @external_domain = external_domain || ENV.fetch("GOVUK_APP_DOMAIN_EXTERNAL", @parent_domain) @host_prefix = ENV.fetch("PLEK_HOSTNAME_PREFIX", "") @unprefixable_hosts = ENV.fetch("PLEK_UNPREFIXABLE_HOSTS", "").split(",").map(&:strip) @use_http_for_single_label_domains = truth_re.match?(ENV.fetch("PLEK_USE_HTTP_FOR_SINGLE_LABEL_DOMAINS", "")) end |
Instance Attribute Details
#external_domain ⇒ Object (readonly)
Returns the value of attribute external_domain.
23 24 25 |
# File 'lib/plek.rb', line 23 def external_domain @external_domain end |
#parent_domain ⇒ Object (readonly)
Returns the value of attribute parent_domain.
23 24 25 |
# File 'lib/plek.rb', line 23 def parent_domain @parent_domain end |
Class Method Details
.asset_root ⇒ String
Convenience wrapper. The same as calling Plek.new.asset_root
.
138 |
# File 'lib/plek.rb', line 138 def_delegators :new, :find, :external_url_for, :asset_root, :website_root |
.external_url_for ⇒ String
Convenience wrapper. The same as calling Plek.new.external_url_for
.
138 |
# File 'lib/plek.rb', line 138 def_delegators :new, :find, :external_url_for, :asset_root, :website_root |
.find ⇒ String
Convenience wrapper. The same as calling Plek.new.find
.
138 |
# File 'lib/plek.rb', line 138 def_delegators :new, :find, :external_url_for, :asset_root, :website_root |
.website_root ⇒ String
Convenience wrapper. The same as calling Plek.new.website_root
.
138 |
# File 'lib/plek.rb', line 138 def_delegators :new, :find, :external_url_for, :asset_root, :website_root |
Instance Method Details
#asset_root ⇒ String
Find the base URL for assets.
108 109 110 |
# File 'lib/plek.rb', line 108 def asset_root env_var_or_dev_fallback("GOVUK_ASSET_ROOT") { find("static") } end |
#external_url_for(service, options = {}) ⇒ Object
Find the external URL for a service/application.
101 102 103 |
# File 'lib/plek.rb', line 101 def external_url_for(service, = {}) find(service, .merge(external: true)) end |
#find(service, options = {}) ⇒ String
Find the base URL for a service/application. This constructs the URL from the given hostname and the #parent_domain. If the #parent_domain matches the DEV_DOMAIN, the returned URL will be a http URL, otherwise it will be https.
If PLEK_HOSTNAME_PREFIX is present in the environment, it will be prepended to the hostname unless the hostname appears in the comma-separated list PLEK_UNPREFIXABLE_HOSTS.
If PLEK_USE_HTTP_FOR_SINGLE_LABEL_DOMAINS=1 in the environment, Plek will use “http” as the URL scheme instead of “https” for single-label domains. Single-label domains are domains with just a single name component, for example “frontend” or “content-store”, as opposed to “frontend.example.com” or “content-store.test.govuk.digital”.
The URL for a given service can be overridden by setting a corresponding environment variable. eg if PLEK_SERVICE_EXAMPLE_CHEESE_THING_URI
was set, Plek.new.find(‘example-cheese-thing’) would return the value of that variable. This overrides both the “internal” and “external” URL for the service. It is not possible to override them separately.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/plek.rb', line 74 def find(service, = {}) name = valid_service_name(service) if (service_uri = defined_service_uri_for(name)) return service_uri end name = "#{host_prefix}#{name}" unless unprefixable_hosts.include?(name) domain = [:external] ? external_domain : parent_domain domain_suffix = domain.empty? ? "" : ".#{domain}" scheme = if [:scheme_relative] "" elsif [:force_http] || http_domain?(domain) "http:" else "https:" end "#{scheme}//#{name}#{domain_suffix}".freeze end |
#website_root ⇒ String
Find the base URL for the public website frontend.
115 116 117 |
# File 'lib/plek.rb', line 115 def website_root env_var_or_dev_fallback("GOVUK_WEBSITE_ROOT") { find("www") } end |