plek 4.0.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +11 -1
  3. data/lib/plek/version.rb +1 -1
  4. data/lib/plek.rb +49 -38
  5. metadata +8 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dce9ccd2cdfbd98e5f5d400b89afc268e7ac85eede91425d6999a45d8c8241e7
4
- data.tar.gz: fb644ee1e4abefa8d133083e6dae182c4d6292cd9079a39e890a7d81a7d7846e
3
+ metadata.gz: 1157f1b8a26836a70c428e595d27392858f6a858043dd44bfeda13314c4e7e99
4
+ data.tar.gz: 7b970caacd3c079d9bd31836fbc53dee83d98da3110982f67e16d618c89e5f91
5
5
  SHA512:
6
- metadata.gz: 152b9b257a2d0ccc8dc1a0854e0e63ee75464173561d4b6eb781caafbb9a89abcd56ff9bb0cd31bb24f297a23a45d07f6bbd45ceb7911ff34ca88b8539391211
7
- data.tar.gz: df7e4c1d134ede43491e7bb1e8ff0f1568bf3e9b39a64a2487fae48f800fdda709ca43f092bbaa1eca451a6e3324a7d5c33d73b6e23ea9947032d2efb6e85dd5
6
+ metadata.gz: bffcec46ce28fbfdc43d276ddea36fc741dca07a3db9292952d5f7717ecd0eed704a15a96c0ca223f29260b903843374e1c6e317a1e0ba8193dd332cfa07f4bd
7
+ data.tar.gz: 74501f62f53fb5263e6a41d679604685ca07147599c42d48afbd9921936f18e7c7f900d163ad5255bbfbd7022f0cef8ae1551cfdd61b67deafa528afc57bb92f
data/README.md CHANGED
@@ -52,14 +52,24 @@ To domain is based on the environment, and defaults to 'dev.gov.uk'. The environ
52
52
 
53
53
  You can prepend strings to the hostnames generated using: `PLEK_HOSTNAME_PREFIX`.
54
54
 
55
+ If `PLEK_HOSTNAME_PREFIX` is present, it will be prepended to the hostname
56
+ unless the hostname appears in the comma-separated list
57
+ `PLEK_UNPREFIXABLE_HOSTS`.
58
+
55
59
  Override the asset URL with: `GOVUK_ASSET_ROOT`. The default is to generate a URL for the `static` service.
56
60
 
57
61
  Override the website root with `GOVUK_WEBSITE_ROOT`. The default is to generate a URL for the `www` service.
58
62
 
63
+ If `PLEK_USE_HTTP_FOR_SINGLE_LABEL_DOMAINS=1` (or anything beginning with `t`
64
+ or `y`), Plek will use `http` as the URL scheme instead of `https` for
65
+ single-label domains. Single-label domains are domains with just a single name
66
+ component, for example `frontend` or `content-store`, as opposed to
67
+ `frontend.example.com` or `content-store.test.govuk.digital`.
68
+
59
69
  ## Licence
60
70
 
61
71
  [MIT License](LICENCE)
62
72
 
63
73
  ## Versioning policy
64
74
 
65
- This is versioned according to [Semantic Versioning 2.0](http://semver.org/)
75
+ This is versioned according to [Semantic Versioning 2.0](https://semver.org/)
data/lib/plek/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Plek
2
- VERSION = "4.0.0".freeze
2
+ VERSION = "4.1.0".freeze
3
3
  end
data/lib/plek.rb CHANGED
@@ -19,10 +19,7 @@ class Plek
19
19
  # The fallback parent domain to use in development mode.
20
20
  DEV_DOMAIN = "dev.gov.uk".freeze
21
21
 
22
- # Domains to return http URLs for.
23
- HTTP_DOMAINS = [DEV_DOMAIN].freeze
24
-
25
- attr_accessor :parent_domain, :external_domain
22
+ attr_reader :parent_domain, :external_domain
26
23
 
27
24
  # Construct a new Plek instance.
28
25
  #
@@ -37,8 +34,12 @@ class Plek
37
34
  # +GOVUK_APP_DOMAIN_EXTERNAL+ and if that is unavailable the parent domain
38
35
  # will be used
39
36
  def initialize(domain_to_use = nil, external_domain = nil)
40
- self.parent_domain = domain_to_use || env_var_or_dev_fallback("GOVUK_APP_DOMAIN", DEV_DOMAIN)
41
- self.external_domain = external_domain || ENV["GOVUK_APP_DOMAIN_EXTERNAL"] || parent_domain
37
+ truth_re = /^[1ty]/i
38
+ @parent_domain = domain_to_use || env_var_or_dev_fallback("GOVUK_APP_DOMAIN", DEV_DOMAIN)
39
+ @external_domain = external_domain || ENV.fetch("GOVUK_APP_DOMAIN_EXTERNAL", @parent_domain)
40
+ @host_prefix = ENV.fetch("PLEK_HOSTNAME_PREFIX", "")
41
+ @unprefixable_hosts = ENV.fetch("PLEK_UNPREFIXABLE_HOSTS", "").split(",").map(&:strip)
42
+ @use_http_for_single_label_domains = truth_re.match?(ENV.fetch("PLEK_USE_HTTP_FOR_SINGLE_LABEL_DOMAINS", ""))
42
43
  end
43
44
 
44
45
  # Find the base URL for a service/application. This constructs the URL from
@@ -47,12 +48,20 @@ class Plek
47
48
  # will be https.
48
49
  #
49
50
  # If PLEK_HOSTNAME_PREFIX is present in the environment, it will be prepended
50
- # to the hostname.
51
+ # to the hostname unless the hostname appears in the comma-separated list
52
+ # PLEK_UNPREFIXABLE_HOSTS.
53
+ #
54
+ # If PLEK_USE_HTTP_FOR_SINGLE_LABEL_DOMAINS=1 in the environment, Plek will use
55
+ # "http" as the URL scheme instead of "https" for single-label domains.
56
+ # Single-label domains are domains with just a single name component, for
57
+ # example "frontend" or "content-store", as opposed to
58
+ # "frontend.example.com" or "content-store.test.govuk.digital".
51
59
  #
52
60
  # The URL for a given service can be overridden by setting a corresponding
53
61
  # environment variable. eg if +PLEK_SERVICE_EXAMPLE_CHEESE_THING_URI+ was
54
62
  # set, +Plek.new.find('example-cheese-thing')+ would return the value of that
55
- # variable.
63
+ # variable. This overrides both the "internal" and "external" URL for the
64
+ # service. It is not possible to override them separately.
56
65
  #
57
66
  # @param service [String] the name of the service to lookup. This should be
58
67
  # the hostname of the service.
@@ -62,24 +71,25 @@ class Plek
62
71
  # scheme (eg `//foo.example.com`)
63
72
  # @return [String] The base URL for the service.
64
73
  def find(service, options = {})
65
- name = name_for(service)
74
+ name = clean_name(service)
66
75
  if (service_uri = defined_service_uri_for(name))
67
76
  return service_uri
68
77
  end
69
78
 
70
- host = "#{name}.#{options[:external] ? external_domain : parent_domain}"
79
+ name = "#{host_prefix}#{name}" unless unprefixable_hosts.include?(name)
71
80
 
72
- if (host_prefix = ENV["PLEK_HOSTNAME_PREFIX"])
73
- host = "#{host_prefix}#{host}"
74
- end
81
+ domain = options[:external] ? external_domain : parent_domain
82
+ domain_suffix = domain.empty? ? "" : ".#{domain}"
75
83
 
76
- if options[:scheme_relative]
77
- "//#{host}".freeze
78
- elsif options[:force_http] || HTTP_DOMAINS.include?(parent_domain)
79
- "http://#{host}".freeze
80
- else
81
- "https://#{host}".freeze
82
- end
84
+ scheme = if options[:scheme_relative]
85
+ ""
86
+ elsif options[:force_http] || http_domain?(domain)
87
+ "http:"
88
+ else
89
+ "https:"
90
+ end
91
+
92
+ "#{scheme}//#{name}#{domain_suffix}".freeze
83
93
  end
84
94
 
85
95
  # Find the external URL for a service/application.
@@ -128,21 +138,16 @@ class Plek
128
138
  URI(website_root)
129
139
  end
130
140
 
131
- # @api private
132
- def name_for(service)
133
- name = service.to_s.dup
134
- name.downcase!
135
- name.strip!
136
- name.gsub!(/[^a-z\.-]+/, "")
137
- name
138
- end
139
-
141
+ # TODO: clean up all references to these and then remove them.
140
142
  class << self
141
143
  # This alias allows calls to be made in the old style:
142
144
  # Plek.current.find('foo')
143
145
  # as well as the new style:
144
146
  # Plek.new.find('foo')
145
- alias_method :current, :new
147
+ def current(...)
148
+ warn "Plek.current is deprecated and will be removed. Use Plek.new or Plek.find instead."
149
+ new(...)
150
+ end
146
151
 
147
152
  # Convenience wrapper. The same as calling +Plek.new.find+.
148
153
  # @see #find
@@ -159,6 +164,17 @@ class Plek
159
164
 
160
165
  private
161
166
 
167
+ attr_reader :host_prefix, :unprefixable_hosts, :use_http_for_single_label_domains
168
+
169
+ # TODO: clean up call sites throughout alphagov and then delete clean_name.
170
+ def clean_name(service)
171
+ service.to_s.downcase.strip.gsub(/[^a-z.-]+/, "")
172
+ end
173
+
174
+ def http_domain?(domain)
175
+ domain == DEV_DOMAIN || domain == "" && use_http_for_single_label_domains
176
+ end
177
+
162
178
  def env_var_or_dev_fallback(var_name, fallback_str = nil)
163
179
  if (var = ENV[var_name])
164
180
  var
@@ -172,13 +188,8 @@ private
172
188
  end
173
189
 
174
190
  def defined_service_uri_for(service)
175
- service_name = service.upcase.gsub(/-/, "_")
176
- var_name = "PLEK_SERVICE_#{service_name}_URI"
177
-
178
- if (uri = ENV[var_name]) && !uri.empty?
179
- return uri
180
- end
181
-
182
- nil
191
+ service_name = service.upcase.tr("-", "_")
192
+ uri = ENV.fetch("PLEK_SERVICE_#{service_name}_URI", "")
193
+ uri.empty? ? nil : uri
183
194
  end
184
195
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plek
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-22 00:00:00.000000000 Z
11
+ date: 2022-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: climate_control
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: rubocop-govuk
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 4.7.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: 4.7.0
69
69
  description: Find the right hostname for each service in an environment-dependent
70
70
  manner
71
71
  email:
@@ -90,14 +90,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
90
  requirements:
91
91
  - - ">="
92
92
  - !ruby/object:Gem::Version
93
- version: '0'
93
+ version: '2.7'
94
94
  required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  requirements:
96
96
  - - ">="
97
97
  - !ruby/object:Gem::Version
98
98
  version: '0'
99
99
  requirements: []
100
- rubygems_version: 3.0.3
100
+ rubygems_version: 3.3.22
101
101
  signing_key:
102
102
  specification_version: 4
103
103
  summary: Locations for services