Class: ActiveSpy::Rails::Base
- Inherits:
-
Object
- Object
- ActiveSpy::Rails::Base
- Defined in:
- lib/active_spy/rails/base.rb
Overview
Default template for callbacks handlers.
Instance Method Summary collapse
-
#after_create ⇒ Object
Handles a
createcallback, prepare and send the request to the event-runner. -
#after_destroy ⇒ Object
Handles an
destroycallback, prepare and send the request to the event-runner. -
#after_save ⇒ Object
Handles the generic
save. -
#after_update ⇒ Object
Handles an
updatecallback, prepare and send the request to the event-runner. -
#get_action(real_method) ⇒ Object
Returns the correct action for the method called in the model.
-
#get_request_params(action) ⇒ Object
Get the event request params for a given
method. -
#initialize(object) ⇒ Base
constructor
A new instance of Base.
-
#inject_is_new_method(object) ⇒ Object
Inject an attribute in the
object, calledis_new?and a setter for it. -
#prepare_request(request_params) ⇒ Object
Prepare a request with
request_params, validates the request and remove the injectedis_mew_method, because it’s not needed anymore. -
#remove_is_new_method(object) ⇒ Object
Remove a previously added
is_newattribute from a given object. -
#respond_to?(method) ⇒ Boolean
Overriding to avoid sending the object to server 2 times (in both before and after callabcks).
-
#send_event_request ⇒ Object
Sends the event request to the configured event-runner instance.
Constructor Details
#initialize(object) ⇒ Base
Returns a new instance of Base.
12 13 14 15 16 |
# File 'lib/active_spy/rails/base.rb', line 12 def initialize(object) @object = object inject_is_new_method(@object) @object.is_new = true if @object.new_record? end |
Instance Method Details
#after_create ⇒ Object
Handles a create callback, prepare and send the request to the event-runner.
50 51 52 53 54 |
# File 'lib/active_spy/rails/base.rb', line 50 def after_create request_params = get_request_params('create') prepare_request(request_params) send_event_request unless ActiveSpy::Configuration.development_mode end |
#after_destroy ⇒ Object
Handles an destroy callback, prepare and send the request to the event-runner.
66 67 68 69 70 |
# File 'lib/active_spy/rails/base.rb', line 66 def after_destroy request_params = get_request_params('destroy') prepare_request(request_params) send_event_request unless ActiveSpy::Configuration.development_mode end |
#after_save ⇒ Object
Handles the generic save. Determines which rail action was done, create or update and call the right callback.
43 44 45 46 |
# File 'lib/active_spy/rails/base.rb', line 43 def after_save action = get_action('save') send("after_#{action}") end |
#after_update ⇒ Object
Handles an update callback, prepare and send the request to the event-runner.
58 59 60 61 62 |
# File 'lib/active_spy/rails/base.rb', line 58 def after_update request_params = get_request_params('update') prepare_request(request_params) send_event_request unless ActiveSpy::Configuration.development_mode end |
#get_action(real_method) ⇒ Object
Returns the correct action for the method called in the model.
129 130 131 132 133 134 135 |
# File 'lib/active_spy/rails/base.rb', line 129 def get_action(real_method) if real_method == 'save' return 'create' if @object.is_new? return 'update' end 'destroy' end |
#get_request_params(action) ⇒ Object
Get the event request params for a given method.
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/active_spy/rails/base.rb', line 105 def get_request_params(action) # real_method = method.to_s.split('_').last # action = get_action(real_method) { type: @object.class.name, actor: @object.instance_variable_get('@actor'), realm: @object.instance_variable_get('@realm'), payload: @object.payload_for(action), action: action } end |
#inject_is_new_method(object) ⇒ Object
Inject an attribute in the object, called is_new? and a setter for it.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/active_spy/rails/base.rb', line 28 def inject_is_new_method(object) object.instance_eval do def is_new=(value) @is_new = value end def is_new? @is_new end end end |
#prepare_request(request_params) ⇒ Object
Prepare a request with request_params, validates the request and remove the injected is_mew_method, because it’s not needed anymore.
75 76 77 78 79 |
# File 'lib/active_spy/rails/base.rb', line 75 def prepare_request(request_params) @event_json = { event: request_params }.to_json ActiveSpy::Rails::Validation::Event.new(@event_json).validate! unless ActiveSpy::Configuration.skip_validations remove_is_new_method(@object) end |
#remove_is_new_method(object) ⇒ Object
Remove a previously added is_new attribute from a given object.
119 120 121 122 123 124 125 |
# File 'lib/active_spy/rails/base.rb', line 119 def remove_is_new_method(object) object.instance_eval do undef :is_new= undef :is_new? instance_variable_set(:@is_new, nil) end end |
#respond_to?(method) ⇒ Boolean
Overriding to avoid sending the object to server 2 times (in both before and after callabcks).
21 22 23 |
# File 'lib/active_spy/rails/base.rb', line 21 def respond_to?(method) method.include?('after_') end |
#send_event_request ⇒ Object
Sends the event request to the configured event-runner instance.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/active_spy/rails/base.rb', line 83 def send_event_request response = nil host = ActiveSpy::Configuration.event_host port = ActiveSpy::Configuration.event_port verify_ssl = ActiveSpy::Configuration.event_verify_ssl params = { content_type: :json } params[:verify_ssl] = verify_ssl if verify_ssl begin response = RestClient.post "#{host}:#{port}/events", @event_json, params rescue => e ::Rails.logger.info(e.response) end if response && defined?(Rails) && !ActiveSpy::Configuration.development_mode ::Rails.logger.info('[SPY] Event sent to event-runner.') ::Rails.logger.info("[SPY] Event-runner response code: #{response.code}") ::Rails.logger.info("[SPY] Event-runner response: #{response.body}") end end |