publishing_platform_location 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cea489133b10f18be67c20af744c161aa686bf4c73e204b3098ea64a2420179e
4
+ data.tar.gz: d5c354319edcb0c9bbd6ef5a21097f3fc62218cbe7086e10c0445d2042a262ac
5
+ SHA512:
6
+ metadata.gz: 4bc9629278aadbfc40ac6528ce68be1b8a01ab93bfa124a93a37764b04c26d23e345e795e562d3364319dca22a669887469cf50cb287ab91f94990c9ec2ca9ad
7
+ data.tar.gz: cb498ea0b259573b7e359b697de94a5bd5fc7a92ddef343828b88ca68cb98e4b56baa68275c77ad8696499c72a7bf9b490303b90b9d983199f60415cd6c76bbc
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Publishing Platform
4
+ Copyright (c) 2011 Crown Copyright
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # publishing_platform_location
2
+ Generates URLs for publishing platform services based on environment.
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class PublishingPlatformLocation
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "publishing_platform_location/version"
4
+ require "forwardable"
5
+
6
+ class PublishingPlatformLocation
7
+ # Raised when a required environment variable is not set.
8
+ class NoConfigurationError < StandardError; end
9
+
10
+ # The fallback parent domain to use in development mode.
11
+ DEV_DOMAIN = "dev.publishing-platform.co.uk"
12
+
13
+ attr_reader :parent_domain, :external_domain
14
+
15
+ # Construct a new PublishingPlatformLocation instance.
16
+ def initialize(domain_to_use = nil, external_domain = nil)
17
+ @parent_domain = domain_to_use || env_var_or_fallback("PUBLISHING_PLATFORM_APP_DOMAIN", DEV_DOMAIN) # empty string for internal services
18
+ @external_domain = external_domain || ENV.fetch("PUBLISHING_PLATFORM_APP_DOMAIN_EXTERNAL", @parent_domain)
19
+ end
20
+
21
+ # Find the base URL for a service/application.
22
+ def find(service, options = {})
23
+ name = valid_service_name(service)
24
+
25
+ domain = options[:external] ? external_domain : parent_domain
26
+ domain_suffix = domain.empty? ? "" : ".#{domain}" # empty string for internal services
27
+
28
+ scheme = if options[:force_http] || http_domain?(domain)
29
+ "http:"
30
+ else
31
+ "https:"
32
+ end
33
+
34
+ "#{scheme}//#{name}#{domain_suffix}".freeze
35
+ end
36
+
37
+ # Find the external URL for a service/application.
38
+ def external_url_for(service, options = {})
39
+ find(service, options.merge(external: true))
40
+ end
41
+
42
+ # Find the base URL for the public website frontend.
43
+ def website_root
44
+ env_var_or_fallback("PUBLISHING_PLATFORM_WEBSITE_ROOT") { find("www") }
45
+ end
46
+
47
+ class << self
48
+ extend Forwardable
49
+
50
+ def_delegators :new, :find, :external_url_for, :website_root
51
+ end
52
+
53
+ private
54
+
55
+ def valid_service_name(name)
56
+ service_name = name.to_s
57
+ return service_name if service_name.match?(/\A[a-z1-9.-]+\z/)
58
+
59
+ raise ArgumentError, "PublishingPlatformLocation expects a service name to only contain lowercase a-z, numbers . (period) and - (dash) characters."
60
+ end
61
+
62
+ def http_domain?(domain)
63
+ [DEV_DOMAIN, ""].include?(domain) # internal services
64
+ end
65
+
66
+ def env_var_or_fallback(var_name, fallback_str = nil)
67
+ if (var = ENV[var_name])
68
+ var
69
+ elsif ENV["RAILS_ENV"] == "production" || ENV["RACK_ENV"] == "production"
70
+ raise(NoConfigurationError, "Expected #{var_name} to be set.")
71
+ elsif block_given?
72
+ yield
73
+ else
74
+ fallback_str
75
+ end
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: publishing_platform_location
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Publishing Platform
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-06-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: climate_control
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: publishing_platform_rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Generates URLs for publishing platform services based on environment.
42
+ email:
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - LICENSE
48
+ - README.md
49
+ - lib/publishing_platform_location.rb
50
+ - lib/publishing_platform_location/version.rb
51
+ homepage:
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '3.0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.3.7
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Generates URLs for publishing platform services based on environment.
74
+ test_files: []