plek 1.10.0 → 1.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENCE +20 -0
  2. data/README.md +17 -15
  3. data/lib/plek.rb +69 -2
  4. data/lib/plek/version.rb +1 -1
  5. metadata +11 -9
data/LICENCE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Crown Copyright
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,27 +1,29 @@
1
- Plek
2
- ====
1
+ # Plek
3
2
 
4
- "Plek" is Afrikaans. It means "Location." Plek is used to generate the correct hostnames for internal GOV.UK services, eg:
3
+ "Plek" is Afrikaans. It means "Location". Plek is used to generate the correct
4
+ base URLs for internal GOV.UK services, eg:
5
5
 
6
6
  ```ruby
7
7
  Plek.find('frontend')
8
8
  ```
9
9
 
10
- returns `https://frontend.production.alphagov.co.uk`. This means we can use this in our code and let our environment configuration figure out the correct hosts for us at runtime.
10
+ returns `http://frontend.dev.gov.uk` on a development machine and
11
+ `https://frontend.production.alphagov.co.uk` on a production machine. This
12
+ means we can use this in our code and let our environment configuration figure
13
+ out the correct hosts for us at runtime.
11
14
 
12
- Hacking Plek URLs
13
- -----------------
15
+ ## Technical documentation
14
16
 
15
- Plek allows one to alter the URI returned using environment variables, eg:
17
+ See the [API docs](http://www.rubydoc.info/gems/plek) for full details of the API.
16
18
 
17
- ```shell
18
- PLEK_SERVICE_EXAMPLE_CHEESE_THING_URI=http://example.com bundle exec rails s
19
- ```
19
+ ### Running the test suite
20
20
 
21
- would set
21
+ `bundle exec rake`
22
22
 
23
- ```ruby
24
- Plek.find('example-cheese-thing')
25
- ```
23
+ ## Licence
24
+
25
+ [MIT License](LICENCE)
26
+
27
+ ## Versioning policy
26
28
 
27
- to `http://example.com`. Underscores in environment variables are converted to dashes in Plek names as demonstrated.
29
+ This is versioned according to [Semantic Versioning 2.0](http://semver.org/)
@@ -1,18 +1,60 @@
1
1
  require 'plek/version'
2
2
  require 'uri'
3
3
 
4
+ # Plek resolves service names to a corresponding base URL.
5
+ #
6
+ # It does this by combining the requested service name with information from
7
+ # environment variables. It will raise a {NoConfigurationError} if a required
8
+ # environment variable isn't set.
9
+ #
10
+ # == Development mode fallback defaults
11
+ #
12
+ # When running development mode (identified by either +RAILS_ENV+ or +RACK_ENV+
13
+ # environment variables being set to "development"), Plek provides some default
14
+ # values when the necessary environment variables aren't set detailed below.
4
15
  class Plek
16
+ # Raised when a required environment variable is not set.
5
17
  class NoConfigurationError < StandardError; end
18
+
19
+ # The fallback parent domain to use in development mode.
6
20
  DEV_DOMAIN = ENV['DEV_DOMAIN'] || 'dev.gov.uk'
21
+
22
+ # Domains to return http URLs for.
7
23
  HTTP_DOMAINS = [ DEV_DOMAIN ]
8
24
 
9
25
  attr_accessor :parent_domain
10
26
 
27
+ # Construct a new Plek instance.
28
+ #
29
+ # @param domain_to_use [String] Optionally override the parent domain to use.
30
+ # If unspecified, this uses the +GOVUK_APP_DOMAIN+ environment variable.
31
+ #
32
+ # In development mode, this falls back to {DEV_DOMAIN} if the environment
33
+ # variable is unset.
11
34
  def initialize(domain_to_use = nil)
12
35
  self.parent_domain = domain_to_use || env_var_or_dev_fallback("GOVUK_APP_DOMAIN", DEV_DOMAIN)
13
36
  end
14
37
 
15
- # Find the URI for a service/application. Return the URI as a string.
38
+ # Find the base URL for a service/application. This constructs the URL from
39
+ # the given hostname and the {#parent_domain}. If the {#parent_domain}
40
+ # matches the {DEV_DOMAIN}, the returned URL will be a http URL, otherwise it
41
+ # will be https.
42
+ #
43
+ # If PLEK_HOSTNAME_PREFIX is present in the environment, it will be prepended
44
+ # to the hostname.
45
+ #
46
+ # The URL for a given service can be overridden by setting a corresponding
47
+ # environment variable. eg if +PLEK_SERVICE_EXAMPLE_CHEESE_THING_URI+ was
48
+ # set, +Plek.new.find('example-cheese-thing')+ would return the value of that
49
+ # variable.
50
+ #
51
+ # @param service [String] the name of the service to lookup. This should be
52
+ # the hostname of the service.
53
+ # @param options [Hash]
54
+ # @option options [Boolean] :force_http If true, force the returned URL to be http.
55
+ # @option options [Boolean] :scheme_relative If true, return a URL without a
56
+ # scheme (eg `//foo.example.com`)
57
+ # @return [String] The base URL for the service.
16
58
  def find(service, options = {})
17
59
  name = name_for(service)
18
60
  if service_uri = defined_service_uri_for(name)
@@ -21,6 +63,10 @@ class Plek
21
63
 
22
64
  host = "#{name}.#{parent_domain}"
23
65
 
66
+ if host_prefix = ENV['PLEK_HOSTNAME_PREFIX']
67
+ host = "#{host_prefix}#{host}"
68
+ end
69
+
24
70
  if options[:scheme_relative]
25
71
  "//#{host}"
26
72
  elsif options[:force_http] or HTTP_DOMAINS.include?(parent_domain)
@@ -30,27 +76,44 @@ class Plek
30
76
  end
31
77
  end
32
78
 
33
- # Find the URI for a service/application. Return a URI object.
79
+ # Find the base URL for a service/application, and parse as a URI object.
80
+ # This wraps #find and returns the parsed result.
81
+ #
82
+ # @param args see {#find}
83
+ # @return [URI::HTTPS,URI::HTTP,URI::Generic] The base URL for the service
34
84
  def find_uri(*args)
35
85
  URI(find(*args))
36
86
  end
37
87
 
88
+ # Find the base URL for assets.
89
+ #
90
+ # @return [String] The assets base URL.
38
91
  def asset_root
39
92
  env_var_or_dev_fallback("GOVUK_ASSET_ROOT") { find("static") }
40
93
  end
41
94
 
95
+ # Find the base URL for the public website frontend.
96
+ #
97
+ # @return [String] The website base URL.
42
98
  def website_root
43
99
  env_var_or_dev_fallback("GOVUK_WEBSITE_ROOT") { find("www") }
44
100
  end
45
101
 
102
+ # Find the base URL for assets.
103
+ #
104
+ # @return [URI::HTTPS,URI::HTTP,URI::Generic] The assets base URL.
46
105
  def asset_uri
47
106
  URI(asset_root)
48
107
  end
49
108
 
109
+ # Find the base URL for the public website frontend.
110
+ #
111
+ # @return [URI::HTTPS,URI::HTTP,URI::Generic] The website base URL.
50
112
  def website_uri
51
113
  URI(website_root)
52
114
  end
53
115
 
116
+ # @api private
54
117
  def name_for(service)
55
118
  name = service.to_s.dup
56
119
  name.downcase!
@@ -66,10 +129,14 @@ class Plek
66
129
  # Plek.new.find('foo')
67
130
  alias_method :current, :new
68
131
 
132
+ # Convenience wrapper. The same as calling +Plek.new.find+.
133
+ # @see #find
69
134
  def find(*args)
70
135
  new.find(*args)
71
136
  end
72
137
 
138
+ # Convenience wrapper. The same as calling +Plek.new.find_uri+.
139
+ # @see #find_uri
73
140
  def find_uri(*args)
74
141
  new.find_uri(*args)
75
142
  end
@@ -1,3 +1,3 @@
1
1
  class Plek
2
- VERSION = '1.10.0'
2
+ VERSION = '1.11.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plek
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.10.0
4
+ version: 1.11.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-06 00:00:00.000000000 Z
12
+ date: 2015-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &16095960 !ruby/object:Gem::Requirement
16
+ requirement: &15585020 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *16095960
24
+ version_requirements: *15585020
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: gem_publisher
27
- requirement: &16095460 !ruby/object:Gem::Requirement
27
+ requirement: &15584460 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 1.1.1
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *16095460
35
+ version_requirements: *15584460
36
36
  description: Find the right hostname for each service in an environment-dependent
37
37
  manner
38
38
  email:
@@ -43,9 +43,11 @@ extra_rdoc_files: []
43
43
  files:
44
44
  - lib/plek/version.rb
45
45
  - lib/plek.rb
46
+ - LICENCE
46
47
  - README.md
47
48
  homepage:
48
- licenses: []
49
+ licenses:
50
+ - MIT
49
51
  post_install_message:
50
52
  rdoc_options: []
51
53
  require_paths:
@@ -58,7 +60,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
58
60
  version: '0'
59
61
  segments:
60
62
  - 0
61
- hash: 3983394480355850721
63
+ hash: -276749319035868702
62
64
  required_rubygems_version: !ruby/object:Gem::Requirement
63
65
  none: false
64
66
  requirements:
@@ -67,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
69
  version: '0'
68
70
  segments:
69
71
  - 0
70
- hash: 3983394480355850721
72
+ hash: -276749319035868702
71
73
  requirements: []
72
74
  rubyforge_project:
73
75
  rubygems_version: 1.8.11