Class: ActiveSpy::Rails::HookList
- Inherits:
-
Object
- Object
- ActiveSpy::Rails::HookList
- Includes:
- Singleton
- Defined in:
- lib/active_spy/rails/hook_list.rb
Overview
Class used to hold all the events hook’s paths and later sync them with an event runner instance.
Instance Attribute Summary collapse
-
#hooks ⇒ Object
readonly
Simple attribute reader for hooks.
Class Method Summary collapse
-
.method_missing(method, *args, &block) ⇒ Object
Proxy all methods called in the Hook to Hook instance.
Instance Method Summary collapse
-
#<<(other) ⇒ Object
forward #<< method to the hook list.
-
#add_hooks(hooks_to_add) ⇒ Object
# Properly creates the
hooks_to_add
in the event runner. -
#clear ⇒ Object
Clear the hook list.
-
#delete_hooks(hooks_to_delete) ⇒ Object
Properly delete the
hooks_to_delete
in the event runner. -
#get_hooks_to_add(old_hooks) ⇒ Object
Select from the hooks defined in the app those that should be created in the event runner.
-
#get_hooks_to_delete(old_hooks) ⇒ Object
Select from old hooks those that should be deleted from event runner.
-
#get_old_hooks ⇒ Object
Get the old hooks list for this service from the event-runner.
-
#initialize ⇒ HookList
constructor
Initialize an empty hook list.
-
#register ⇒ Object
Register in event runner all the hooks defined in the list.
Constructor Details
#initialize ⇒ HookList
Initialize an empty hook list
15 16 17 18 19 20 21 22 23 |
# 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 |
Instance Attribute Details
#hooks ⇒ Object (readonly)
Simple attribute reader for hooks
11 12 13 |
# File 'lib/active_spy/rails/hook_list.rb', line 11 def hooks @hooks end |
Class Method Details
.method_missing(method, *args, &block) ⇒ Object
Proxy all methods called in the Hook to Hook instance. Just a syntax sugar.
28 29 30 |
# File 'lib/active_spy/rails/hook_list.rb', line 28 def self.method_missing(method, *args, &block) instance.send(method, *args, &block) end |
Instance Method Details
#<<(other) ⇒ Object
forward #<< method to the hook list.
40 41 42 |
# File 'lib/active_spy/rails/hook_list.rb', line 40 def <<(other) @hooks << other end |
#add_hooks(hooks_to_add) ⇒ Object
# Properly creates the hooks_to_add
in the event runner.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# 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 ⇒ Object
Clear the hook list.
34 35 36 |
# File 'lib/active_spy/rails/hook_list.rb', line 34 def clear @hooks = [] end |
#delete_hooks(hooks_to_delete) ⇒ Object
Properly delete the hooks_to_delete
in the event runner.
107 108 109 110 111 112 113 114 115 116 |
# 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 |
#get_hooks_to_add(old_hooks) ⇒ Object
Select from the hooks defined in the app those that should be created in the event runner.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# 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 |
#get_hooks_to_delete(old_hooks) ⇒ Object
Select from old hooks those that should be deleted from event runner.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# 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_old_hooks ⇒ Object
Get the old hooks list for this service from the event-runner
58 59 60 61 62 63 64 65 66 |
# 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 ⇒ Object
Register in event runner all the hooks defined in the list. If some of them already exists, they will be excluded and readded.
47 48 49 50 51 52 53 54 |
# 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 |