Module: ConsulWatcher

Defined in:
lib/consul_watcher.rb,
lib/consul_watcher/diff.rb,
lib/consul_watcher/class_helper.rb,
lib/consul_watcher/storage/disk.rb,
lib/consul_watcher/destination/jq.rb,
lib/consul_watcher/storage/consul.rb,
lib/consul_watcher/watch_type/key.rb,
lib/consul_watcher/destination/amqp.rb,
lib/consul_watcher/watch_type/checks.rb

Overview

This will be a module to store previous consul watch json to compare with previous watch data

Defined Under Namespace

Modules: ClassHelper, Destination, Storage, WatchType Classes: Diff

Class Method Summary collapse

Class Method Details

.classname_to_file(classname) ⇒ Object

Dynamically require handler class from passed in handler class



44
45
46
# File 'lib/consul_watcher.rb', line 44

def self.classname_to_file(classname)
  classname.gsub('::', '/').gsub(/([a-zA-Z])([A-Z])/, '\1_\2').downcase
end

.get_destination(destination_config) ⇒ Object



37
38
39
40
41
# File 'lib/consul_watcher.rb', line 37

def self.get_destination(destination_config)
  classname = destination_config['classname'] || 'ConsulWatcher::Destination::Jq'
  require classname_to_file(classname)
  Object.const_get(classname).new(destination_config)
end

.get_storage(storage_config) ⇒ Object



25
26
27
28
29
# File 'lib/consul_watcher.rb', line 25

def self.get_storage(storage_config)
  classname = storage_config['classname'] || 'ConsulWatcher::Storage::Disk'
  require classname_to_file(classname)
  Object.const_get(classname).new(storage_config)
end

.get_watch_type(watch_type_config) ⇒ Object



31
32
33
34
35
# File 'lib/consul_watcher.rb', line 31

def self.get_watch_type(watch_type_config)
  classname = watch_type_config['classname'] || 'ConsulWatcher::WatchType::Checks'
  require classname_to_file(classname)
  Object.const_get(classname).new(watch_type_config)
end

.watch(watch_type, storage_name, config) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/consul_watcher.rb', line 10

def self.watch(watch_type, storage_name, config)
  storage = get_storage(config['storage']) if config['storage']
  watch_type = get_watch_type(config['watch_type']) if config['watch_type']
  filters = get_filters(config['filters']) if config['filters']
  destination = get_destination(config['destination']) if config['destination']

  current_watch_json = $stdin.read
  previous_watch_json = storage.fetch(storage_name)
  changes = watch_type.get_changes(previous_watch_json, current_watch_json)
  changes.each do |change|
    destination.send(change)
  end
  storage.push(storage_name, current_watch_json)
end