Class: Blinkbox::CommonConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/blinkbox/common_config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_dir: "config", logger: nil) ⇒ CommonConfig

Generates a CommonConfig object assuming that there is a `reference.properties` in the `config_fir` specified. If an `application.properties` is also present it will load that too (overwriting any repeated property keys) and finally if the environment variable `CONFIG_URL` is set it will load the properties from that (overwriting the others) whether the URL is an absolute, relative (to the working directory, not the `config_dir`) or an HTTP URI.

Parameters:

  • config_dir (String)

    The directory where a `reference.properties` exists and optionally an `application.conf` exists. Relative paths are taken from the working directory.

  • logger (#info, NilClass)

    A logger to which information about what properties have been loaded will be sent. Must respond to #info, can also be `nil`.

Raises:

  • (ArgumentError)

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/blinkbox/common_config.rb', line 18

def initialize(config_dir: "config", logger: nil)
  raise ArgumentError, "The logger given doesn't respond to #info." unless logger.nil? || logger.respond_to?(:info)

  reference_prop_file = File.expand_path(File.join(config_dir, "reference.properties"))
  application_prop_file = File.expand_path(File.join(config_dir, "application.properties"))
  raise RuntimeError, "No reference file at #{reference_prop_file}" unless File.exist?(reference_prop_file)

  @options = JavaProperties::Properties.new(reference_prop_file)
  logger.info "Loaded configuration from #{reference_prop_file}" unless logger.nil?
  @sources = [reference_prop_file]

  if File.exist?(application_prop_file)
    @options.load(application_prop_file)
    logger.info "Loaded configuration from #{application_prop_file}" unless logger.nil?
    @sources.unshift(application_prop_file)
  end

  if ENV['CONFIG_URL']
    if ENV['CONFIG_URL'] =~ %r{^https?://}
      res = Net::HTTP.get_response(URI.parse(ENV['CONFIG_URL']))
      raise "The CONFIG_URL points to a resource that returns an HTTP Status code of #{res.code}, not a 200." unless res.code == "200"

      remote_props = Tempfile.new('remote_properties')
      begin
        remote_props.write(res.body)
        remote_props.close
        @options.load(remote_props)
      ensure
        remote_props.unlink
      end
    else
      @options.load(ENV['CONFIG_URL'])
    end
    logger.info "Loaded configuration from #{ENV['CONFIG_URL']}" unless logger.nil?
    @sources.unshift(ENV['CONFIG_URL'])
  end
end

Instance Attribute Details

#sourcesObject (readonly)

List of sources where properties have been loaded, with the most important first


9
10
11
# File 'lib/blinkbox/common_config.rb', line 9

def sources
  @sources
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates a CommonConfig object from a hash.


59
60
61
62
63
# File 'lib/blinkbox/common_config.rb', line 59

def self.from_hash(hash)
  config = self.allocate
  config.instance_variable_set(:'@options', hash)
  config
end

Instance Method Details

#[](key) ⇒ Object Also known as: get

Accessor for the properties stored in the instance. Accepts strings or symbols indifferently.


68
69
70
# File 'lib/blinkbox/common_config.rb', line 68

def [](key)
  @options[key]
end

#tree(root) ⇒ Object

Retrieves a hash of all properties which are beneath this starting element in the tree.

# logging.udp.host = “127.0.0.1” # logging.udp.port = 1234 # logging = this won't be returned

properties.tree(:logging) # => { :'udp.host' => “127.0.0.1”, :'udp.port' => 1234 }

properties.tree(:log) # => {}


87
88
89
90
91
92
93
94
95
96
# File 'lib/blinkbox/common_config.rb', line 87

def tree(root)
  hash = {}
  @options.each { |key, value|
    len = root.length + 1
    if key.to_s.slice(0, len) == root.to_s + '.'
      hash[key.to_s[len..-1].to_sym] = value
    end
  }
  hash
end