Skip to content

Commit

Permalink
add notify
Browse files Browse the repository at this point in the history
  • Loading branch information
budipang committed Aug 27, 2018
1 parent f440c4a commit 988bd61
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
1 change: 1 addition & 0 deletions lib/toggleable/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Configuration
attr_accessor :palanca_host
attr_accessor :palanca_user
attr_accessor :palanca_password
attr_accessor :notify_endpoint ## optional for notify changes
attr_accessor :storage ## storage used. default: memory store
attr_accessor :namespace ## required for prefixing the keys. default: `toggleable``
attr_accessor :logger ## optional, it will not log if not configured.
Expand Down
17 changes: 14 additions & 3 deletions lib/toggleable/feature_toggler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
# frozen_string_literal: false

require 'singleton'
require 'rest-client'
Expand Down Expand Up @@ -121,11 +121,22 @@ def read_key_expired?(key)
end

def log_changes(mapping, actor)
previous_values = available_features
toggles = ''
values = ''
mapping.each do |key, val|
next if previous_values[key].to_s == val.to_s
Toggleable.configuration.logger.log(key: key, value: val, actor: actor)
toggles.concat("#{key},")
values.concat("#{val},")
end

notify_changes(toggles[0...-1], values[0...-1]) if Toggleable.configuration.notify_endpoint
end

def notify_changes(toggles, values)
url = "#{Toggleable.configuration.notify_endpoint}/notify_toggle?keys=#{toggles}&values=#{values}"
RestClient::Resource.new(url).get timeout: 2, open_timeout: 1
rescue StandardError
nil
end
end
end
1 change: 1 addition & 0 deletions spec/class_initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ def log(key:, value:, actor:)
Toggleable.configure do |t|
t.logger = logger
t.palanca_host = 'localhost:8027'
t.notify_endpoint = 'localhost:5858'
t.use_memoization = false
end
29 changes: 7 additions & 22 deletions spec/toggleable/feature_toggler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,12 @@

before do
stub_request(:post, "http://localhost:8027/_internal/toggle-features/bulk-update").to_return(status: 200, body: 'success')
stub_request(:get, "http://localhost:5858/notify_toggle?keys=other_key&values=true").to_return(status: 200, body: 'success')
end

describe '#mass_toggle! with memory store' do
let(:mapping_before) {
{
'key' => 'true',
'other_key' => 'false'
}
}

let(:mapping_after) {
{
'key' => 'true',
'other_key' => 'true'
}
}
Expand All @@ -103,7 +96,6 @@
before do
subject.register('key')
subject.register('other_key')
allow(subject).to receive(:available_features).and_return(mapping_before)
end

it do
Expand All @@ -113,30 +105,23 @@
end

describe '#mass_toggle! with redis' do
before do
allow(Toggleable.configuration).to receive(:storage).and_return(redis_storage)
end

let(:mapping_before) {
{
'key' => 'true',
'other_key' => 'false'
}
}

let(:mapping_after) {
{
'key' => 'true',
'other_key' => 'true'
}
}

let(:actor_id) { 1 }

before do
allow(Toggleable.configuration).to receive(:storage).and_return(redis_storage)
allow_any_instance_of(RestClient::Resource).to receive(:get).and_raise(StandardError)
end


before do
subject.register('key')
subject.register('other_key')
allow(subject).to receive(:available_features).and_return(mapping_before)
end

it do
Expand Down

0 comments on commit 988bd61

Please sign in to comment.