Class: ConsulWatcher::WatchType::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/consul_watcher/watch_type/key.rb

Instance Method Summary collapse

Constructor Details

#initialize(destination_config) ⇒ Key

Returns a new instance of Key.



8
# File 'lib/consul_watcher/watch_type/key.rb', line 8

def initialize(destination_config) end

Instance Method Details

#get_changes(previous_watch_json, current_watch_json) ⇒ Object



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

def get_changes(previous_watch_json, current_watch_json)
  json_diff = HashDiff.diff(json_to_hash(previous_watch_json),
                            json_to_hash(current_watch_json),
                            array_path: true)
  json_diff.each.collect do |change|
    # change[1] = change[1].join('/')
    change[2] = Base64.decode64(change[2]) if change[2].is_a? String
    change[3] = Base64.decode64(change[3]) if change[3].is_a? String
    { 
      'watch_type' => 'key',
      'id' => id(change),
      'diff' => change
    }
  end
  #json_diff.reject { |change| change[0] == '~' && change[1][-1] == 'ModifyIndex' }
end

#id(change) ⇒ Object



27
28
29
# File 'lib/consul_watcher/watch_type/key.rb', line 27

def id(change)
  "key.#{change[1][0].tr('/', '.')}"
end

#json_to_hash(json) ⇒ Object



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

def json_to_hash(json)
  json = '{}' if json.nil? || json == "null\n"
  JSON.parse(json).map do |kv|
    { kv['Key'] => kv.reject { |key, _value| key == 'Key' } }
  end.reduce({}, :merge)
end