class ActiveSpy::Rails::HookList
Class used to hold all the events hook’s paths and later sync them with an event runner instance.
Attributes
Simple attribute reader for hooks
Public Class Methods
Proxy all methods called in the {ActiveSpy::Hook} to {ActiveSpy::Hook} instance. Just a syntax sugar.
# File lib/active_spy/rails/hook_list.rb, line 28 def self.method_missing(method, *args, &block) instance.send(method, *args, &block) end
Initialize an empty hook list
# File lib/active_spy/rails/hook_list.rb, line 15 def initialize host = ActiveSpy::Configuration.event_host port = ActiveSpy::Configuration.event_port name = ActiveSpy::Configuration.name.downcase.gsub(' ', '-').strip @verify_ssl = ActiveSpy::Configuration.event_verify_ssl @base_service_url = "#{host}:#{port}/services/#{name}" @hooks = [] end
Public Instance Methods
forward {<<} method to the hook list.
# File lib/active_spy/rails/hook_list.rb, line 40 def <<(other) @hooks << other end
Properly creates the hooks_to_add
in the event runner.
# File lib/active_spy/rails/hook_list.rb, line 120 def add_hooks(hooks_to_add) url = "#{@base_service_url}/hooks" hooks_to_add.each do |hook| hook = { 'hook' => { 'class' => hook['class'], 'post_path' => ActiveSpy::Engine.routes.url_helpers.notifications_path(hook['post_class'].downcase), } } if @verify_ssl RestClient::Request.execute(method: :post, url: url, payload: hook.to_json, headers: { content_type: :json }, verify_ssl: @verify_ssl) else RestClient.post url, hook.to_json, content_type: :json end end end
Clear the hook list.
# File lib/active_spy/rails/hook_list.rb, line 34 def clear @hooks = [] end
Properly delete the hooks_to_delete
in the event runner.
# File lib/active_spy/rails/hook_list.rb, line 107 def delete_hooks(hooks_to_delete) hooks_to_delete.each do |hook| url = "#{@base_service_url}/hooks/#{hook['id']}" if @verify_ssl RestClient::Request.execute(method: :delete, url: url, verify_ssl: @verify_ssl) else RestClient.delete url end end end
Select from the hooks defined in the app those that should be created in the event runner.
# File lib/active_spy/rails/hook_list.rb, line 89 def get_hooks_to_add(old_hooks) hooks_to_add = [] @hooks.each do |hook| found = false old_hooks.each do |old_hook| if hook['class'] == old_hook['class'] && old_hook['active'] found = true break end end next if found hooks_to_add << hook end hooks_to_add end
Select from old hooks those that should be deleted from event runner.
# File lib/active_spy/rails/hook_list.rb, line 70 def get_hooks_to_delete(old_hooks) hooks_to_delete = [] old_hooks.each do |old_hook| found = false @hooks.each do |hook| if hook['class'] == old_hook['class'] && old_hook['active'] found = true break end end next if found hooks_to_delete << old_hook end hooks_to_delete end
Get the old hooks list for this service from the event-runner
# File lib/active_spy/rails/hook_list.rb, line 58 def get_old_hooks request = if @verify_ssl RestClient::Request.execute(method: :get, url: @base_service_url, verify_ssl: @verify_ssl) else RestClient.get(@base_service_url) end JSON.load(request)['hooks'] end
Register in event runner all the hooks defined in the list. If some of them already exists, they will be excluded and readded.
# File lib/active_spy/rails/hook_list.rb, line 47 def register @hooks = @hooks.map(&:to_hook).flatten old_hooks = get_old_hooks hooks_to_delete = get_hooks_to_delete(old_hooks) hooks_to_add = get_hooks_to_add(old_hooks) delete_hooks(hooks_to_delete) if hooks_to_delete.any? add_hooks(hooks_to_add) unless hooks_to_add.empty? end