blinkbox-common_config 0.4.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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NmMzMTM2YmJmY2I5OWIyM2Y4YWJhMTY1M2QxMGZhZGRiY2U4NmNmYQ==
5
+ data.tar.gz: !binary |-
6
+ MzI5NmJiZjFmMjFiNmUzNmE3NWM2Yzc5MmZlZWUzM2NkNzhkM2EwZQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NGUzYTcxNjY5OTg1ZTI0NWRkNWIwYmRmZGZkMjI3NDgyZWI3MDYzODUxOWVk
10
+ NTQ0YThkNWY2NWIwNzM0Y2QzNjAyYWYzNDA1NDY4ZGNhN2I5NTJlMzk4NmRk
11
+ MDUwZmY1OTU4M2YwNTE1NGE3N2M3ZmEyMjgzMjhlMTkyZTdkMDQ=
12
+ data.tar.gz: !binary |-
13
+ Njc1ZGIxNzBhZGExMzAxNTNmNWM5NDU0OThkZjcxZjRmZjI5MTk0NTJlY2Uy
14
+ YWM3NzMwMDM0MmY2YjgzMGFhOTkyYmQ5MDc1NTJhOGExZDk3MmJhNjdlY2Ey
15
+ OGYzYjA5MDc0NjY2MjkxYmU2ZjcyNGRmMjZjN2U0MGM5NTU2Yjk=
@@ -0,0 +1,53 @@
1
+ # Changelog
2
+
3
+ ## Open Source release (2015-01-28 14:11:21)
4
+
5
+ Today we have decided to publish parts of our codebase on Github under the [MIT licence](LICENCE). The licence is very permissive, but we will always appreciate hearing if and where our work is used!
6
+
7
+ ## 0.4.0 ([#6](https://git.mobcastdev.com/Platform/common_config.rb/pull/6) 2014-10-03 15:41:55)
8
+
9
+ No deep setting
10
+
11
+ ### Breaking change
12
+
13
+ - Should not have changed this to using deep setting (other apps depend on it being single keys, and this is more flexible)
14
+
15
+ ## 0.3.1 ([#3](https://git.mobcastdev.com/Platform/common_config.rb/pull/3) 2014-08-18 09:46:37)
16
+
17
+ Depth
18
+
19
+ ### Clarification
20
+
21
+ - Made improvements to the tests and code so that the form of nested properties when using the `tree` method is well defined.
22
+
23
+ ## 0.3.0 ([#4](https://git.mobcastdev.com/Platform/common_config.rb/pull/4) 2014-09-25 10:00:48)
24
+
25
+ Symbols and booleans
26
+
27
+ ### New feature
28
+
29
+ - Converts `"true"` and `"false"` to boolean values.
30
+ - Converts `":thing"` to `:thing` symbols.
31
+
32
+ ## 0.2.0 ([#2](https://git.mobcastdev.com/Platform/common_config.rb/pull/2) 2014-08-13 16:33:04)
33
+
34
+ Use units in properties
35
+
36
+ ### New Feature
37
+
38
+ - Properties in the format `5 seconds` will be converted to [Unit](https://github.com/olbrich/ruby-units) objects so that they can be easily manipulated:
39
+
40
+ ```ruby
41
+ property = Unit("6 minutes")
42
+
43
+ p property.convert_to("seconds").scalar
44
+ ### => 360
45
+ ```
46
+
47
+ ## 0.1.0
48
+
49
+ Initial release
50
+
51
+ ### New features
52
+
53
+ - Reads `config/reference.properties`, overwrites with `config/application.properties` (if it exists) then overwrites with contents of the file or URL at `ENV['CONFIG_URL']` (if it exists/responds with HTTP 200)
@@ -0,0 +1,17 @@
1
+ # Blinkbox::CommonConfig
2
+
3
+ Put your reference properties in `config/reference.properties` and any environmental stuff which makes sense for a development environment in `config/application.properties`, then load them and access them with:
4
+
5
+ ```ruby
6
+ # $ gem install blinkbox-common_config
7
+ require "blinkbox/common_config"
8
+
9
+ properties = Blinkbox::CommonConfig.new
10
+
11
+ # Accessible with symbols
12
+ properties[:'rabbitmq.url']
13
+ # Or strings
14
+ properties["rabbitmq.url"]
15
+ ```
16
+
17
+ It will also load the properties file referenced in the environment variable `CONFIG_URL` so long as the file exists, or the http(s) URI responds with a 200 status code.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.0
@@ -0,0 +1,98 @@
1
+ require "java_properties"
2
+ require "java_properties/data_types"
3
+ require "net/http"
4
+ require "tempfile"
5
+
6
+ module Blinkbox
7
+ class CommonConfig
8
+ # List of sources where properties have been loaded, with the most important first
9
+ attr_reader :sources
10
+
11
+ # Generates a CommonConfig object assuming that there is a `reference.properties` in the `config_fir` specified. If
12
+ # an `application.properties` is also present it will load that too (overwriting any repeated property keys) and finally
13
+ # if the environment variable `CONFIG_URL` is set it will load the properties from that (overwriting the others) whether
14
+ # the URL is an absolute, relative (to the working directory, not the `config_dir`) or an HTTP URI.
15
+ #
16
+ # @param [String] config_dir The directory where a `reference.properties` exists and optionally an `application.conf` exists. Relative paths are taken from the working directory.
17
+ # @param [#info, NilClass] logger A logger to which information about what properties have been loaded will be sent. Must respond to #info, can also be `nil`.
18
+ def initialize(config_dir: "config", logger: nil)
19
+ raise ArgumentError, "The logger given doesn't respond to #info." unless logger.nil? || logger.respond_to?(:info)
20
+
21
+ reference_prop_file = File.expand_path(File.join(config_dir, "reference.properties"))
22
+ application_prop_file = File.expand_path(File.join(config_dir, "application.properties"))
23
+ raise RuntimeError, "No reference file at #{reference_prop_file}" unless File.exist?(reference_prop_file)
24
+
25
+ @options = JavaProperties::Properties.new(reference_prop_file)
26
+ logger.info "Loaded configuration from #{reference_prop_file}" unless logger.nil?
27
+ @sources = [reference_prop_file]
28
+
29
+ if File.exist?(application_prop_file)
30
+ @options.load(application_prop_file)
31
+ logger.info "Loaded configuration from #{application_prop_file}" unless logger.nil?
32
+ @sources.unshift(application_prop_file)
33
+ end
34
+
35
+ if ENV['CONFIG_URL']
36
+ if ENV['CONFIG_URL'] =~ %r{^https?://}
37
+ res = Net::HTTP.get_response(URI.parse(ENV['CONFIG_URL']))
38
+ raise "The CONFIG_URL points to a resource that returns an HTTP Status code of #{res.code}, not a 200." unless res.code == "200"
39
+
40
+ remote_props = Tempfile.new('remote_properties')
41
+ begin
42
+ remote_props.write(res.body)
43
+ remote_props.close
44
+ @options.load(remote_props)
45
+ ensure
46
+ remote_props.unlink
47
+ end
48
+ else
49
+ @options.load(ENV['CONFIG_URL'])
50
+ end
51
+ logger.info "Loaded configuration from #{ENV['CONFIG_URL']}" unless logger.nil?
52
+ @sources.unshift(ENV['CONFIG_URL'])
53
+ end
54
+ end
55
+
56
+ # Creates a CommonConfig object from a hash.
57
+ #
58
+ # @params [Hash] hash The hash which will become the config. Symbol or string key values are cool.
59
+ def self.from_hash(hash)
60
+ config = self.allocate
61
+ config.instance_variable_set(:'@options', hash)
62
+ config
63
+ end
64
+
65
+ # Accessor for the properties stored in the instance. Accepts strings or symbols indifferently.
66
+ #
67
+ # @params [String, Symbol] key The name of the property to retrieve.
68
+ def [](key)
69
+ @options[key]
70
+ end
71
+ alias :get :[]
72
+
73
+ # Retrieves a hash of all properties which are beneath this starting element in the tree.
74
+ #
75
+ # # logging.udp.host = "127.0.0.1"
76
+ # # logging.udp.port = 1234
77
+ # # logging = this won't be returned
78
+ #
79
+ # properties.tree(:logging)
80
+ # # => { :'udp.host' => "127.0.0.1", :'udp.port' => 1234 }
81
+ #
82
+ # properties.tree(:log)
83
+ # # => {}
84
+ #
85
+ # @params [String, Symbol] root The root key to look underneath.
86
+ # @returns [Hash] Returns a has
87
+ def tree(root)
88
+ hash = {}
89
+ @options.each { |key, value|
90
+ len = root.length + 1
91
+ if key.to_s.slice(0, len) == root.to_s + '.'
92
+ hash[key.to_s[len..-1].to_sym] = value
93
+ end
94
+ }
95
+ hash
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,29 @@
1
+ require "ruby_units"
2
+
3
+ module JavaProperties
4
+ module Encoding
5
+ class << self
6
+ alias :raw_decode :decode
7
+ def decode(string)
8
+ string = raw_decode(string)
9
+
10
+ case string
11
+ when /^:(\w+)$/
12
+ Regexp.last_match[1].to_sym
13
+ when /^(?:true|false)$/i
14
+ string.downcase == "true"
15
+ when /^\d+\ .+$/
16
+ Unit(string)
17
+ when /^\d+$/
18
+ string.to_i
19
+ when /^\d+\.\d+$/
20
+ string.to_f
21
+ when /^"(.*)"$/
22
+ Regexp.last_match[1]
23
+ else
24
+ string
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blinkbox-common_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - JP Hastings-Spital
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: java_properties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: ruby-units
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Simple helper for loading configuration in the blinkbox Books format
84
+ email:
85
+ - jphastings@blinkbox.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files:
89
+ - CHANGELOG.md
90
+ - README.md
91
+ files:
92
+ - CHANGELOG.md
93
+ - README.md
94
+ - VERSION
95
+ - lib/blinkbox/common_config.rb
96
+ - lib/java_properties/data_types.rb
97
+ homepage: ''
98
+ licenses: []
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.4.5
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Simple helper for loading configuration in the blinkbox Books format
120
+ test_files: []