plek 3.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 +55 -50
  5. metadata +20 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 32f009f7c15f4e0b0fa5a5b5f3866e327101ff3ed1a7cc4527e5d3346398bcf3
4
- data.tar.gz: 8ed2fcfc3453d92b3a049f0941bf6026d0f6294f33b51bab9e0d69bfa6608af7
3
+ metadata.gz: 1157f1b8a26836a70c428e595d27392858f6a858043dd44bfeda13314c4e7e99
4
+ data.tar.gz: 7b970caacd3c079d9bd31836fbc53dee83d98da3110982f67e16d618c89e5f91
5
5
  SHA512:
6
- metadata.gz: a7f2980ae077e50f4f277ad26ece95057c9f486f8f1883e24cb80f47f60b02fc35cd58ab7812642a7348a90d648b2223572609702220e281180e0f5e3b4a8610
7
- data.tar.gz: 48664f58cd9f619b78dea7db04afe260153ac596ce79977d5e07911ef23a68f0e308f9e71ff135ee4636c7ebc6eb9f85880bdf88ec94515a874bac2f2c32a57a
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 = '3.0.0'
2
+ VERSION = "4.1.0".freeze
3
3
  end
data/lib/plek.rb CHANGED
@@ -1,5 +1,5 @@
1
- require 'plek/version'
2
- require 'uri'
1
+ require "plek/version"
2
+ require "uri"
3
3
 
4
4
  # Plek resolves service names to a corresponding base URL.
5
5
  #
@@ -17,12 +17,9 @@ class Plek
17
17
  class NoConfigurationError < StandardError; end
18
18
 
19
19
  # The fallback parent domain to use in development mode.
20
- DEV_DOMAIN = 'dev.gov.uk'
20
+ DEV_DOMAIN = "dev.gov.uk".freeze
21
21
 
22
- # Domains to return http URLs for.
23
- HTTP_DOMAINS = [ DEV_DOMAIN ]
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)
66
- if service_uri = defined_service_uri_for(name)
74
+ name = clean_name(service)
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] or 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.
@@ -107,13 +117,6 @@ class Plek
107
117
  env_var_or_dev_fallback("GOVUK_ASSET_ROOT") { find("static") }
108
118
  end
109
119
 
110
- # Find the asset host used to serve assets to the public
111
- #
112
- # @return [String] The public-facing asset base URL
113
- def public_asset_host
114
- env_var_or_dev_fallback("GOVUK_ASSET_HOST") { find("static") }
115
- end
116
-
117
120
  # Find the base URL for the public website frontend.
118
121
  #
119
122
  # @return [String] The website base URL.
@@ -135,21 +138,16 @@ class Plek
135
138
  URI(website_root)
136
139
  end
137
140
 
138
- # @api private
139
- def name_for(service)
140
- name = service.to_s.dup
141
- name.downcase!
142
- name.strip!
143
- name.gsub!(/[^a-z\.-]+/, '')
144
- name
145
- end
146
-
141
+ # TODO: clean up all references to these and then remove them.
147
142
  class << self
148
143
  # This alias allows calls to be made in the old style:
149
144
  # Plek.current.find('foo')
150
145
  # as well as the new style:
151
146
  # Plek.new.find('foo')
152
- 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
153
151
 
154
152
  # Convenience wrapper. The same as calling +Plek.new.find+.
155
153
  # @see #find
@@ -164,10 +162,21 @@ class Plek
164
162
  end
165
163
  end
166
164
 
167
- private
165
+ private
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
168
177
 
169
178
  def env_var_or_dev_fallback(var_name, fallback_str = nil)
170
- if var = ENV[var_name]
179
+ if (var = ENV[var_name])
171
180
  var
172
181
  elsif ENV["RAILS_ENV"] == "production" || ENV["RACK_ENV"] == "production"
173
182
  raise(NoConfigurationError, "Expected #{var_name} to be set. Perhaps you should run your task through govuk_setenv <appname>?")
@@ -179,12 +188,8 @@ class Plek
179
188
  end
180
189
 
181
190
  def defined_service_uri_for(service)
182
- service_name = service.upcase.gsub(/-/,'_')
183
- var_name = "PLEK_SERVICE_#{service_name}_URI"
184
-
185
- if (uri = ENV[var_name] and ! uri.empty?)
186
- return uri
187
- end
188
- return nil
191
+ service_name = service.upcase.tr("-", "_")
192
+ uri = ENV.fetch("PLEK_SERVICE_#{service_name}_URI", "")
193
+ uri.empty? ? nil : uri
189
194
  end
190
195
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plek
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.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: 2019-05-29 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
- name: rake
14
+ name: climate_control
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: climate_control
42
+ name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop-govuk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 4.7.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 4.7.0
55
69
  description: Find the right hostname for each service in an environment-dependent
56
70
  manner
57
71
  email:
@@ -76,14 +90,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
90
  requirements:
77
91
  - - ">="
78
92
  - !ruby/object:Gem::Version
79
- version: '0'
93
+ version: '2.7'
80
94
  required_rubygems_version: !ruby/object:Gem::Requirement
81
95
  requirements:
82
96
  - - ">="
83
97
  - !ruby/object:Gem::Version
84
98
  version: '0'
85
99
  requirements: []
86
- rubygems_version: 3.0.1
100
+ rubygems_version: 3.3.22
87
101
  signing_key:
88
102
  specification_version: 4
89
103
  summary: Locations for services