active_spy 1.1.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/active_spy.gemspec +4 -3
- data/app/controllers/active_spy/notifications_controller.rb +3 -0
- data/lib/active_spy/configuration.rb +16 -0
- data/lib/active_spy/rails/base.rb +10 -3
- data/lib/active_spy/rails/hook_list.rb +2 -2
- data/lib/active_spy/rails/listener.rb +4 -4
- data/lib/active_spy/rails/spy.rb +9 -11
- data/lib/active_spy/rails/validation.rb +87 -0
- data/lib/active_spy/spy/spy_list.rb +0 -1
- data/lib/active_spy.rb +14 -10
- data/lib/rails/generators/active_spy/install/install_generator.rb +2 -2
- data/lib/rails/generators/active_spy/install/templates/initializer.rb +0 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60c31bd6d9203cda5c3e771fe53fd1963562ba19
|
4
|
+
data.tar.gz: 3fa992ecbb2b3b5d255776df58890f0eccc4cafd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 524b7de465121d72f2157620b84795cef249deb1d62d07bbb166287fa190401a211dbae46b46a86a76207636a1e171dbc6d078b39c2fa7dc2312669c8299424a
|
7
|
+
data.tar.gz: 8bf369915690be1c13311c59572b30cbd7579bdd1d49ca09719c93c43ee7187c3548e92d7410a4138a3063df6dd5ecf892f9f96408e8ed2cf387135a447b195e
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/active_spy.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: active_spy 1.
|
5
|
+
# stub: active_spy 1.2.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "active_spy"
|
9
|
-
s.version = "1.
|
9
|
+
s.version = "1.2.0"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Douglas Camata"]
|
14
|
-
s.date = "2014-07-
|
14
|
+
s.date = "2014-07-29"
|
15
15
|
s.description = " Watch for a method call in any class and run before/after callbacks.\n You can even watch your Rails models for events (like create, update,\n destroy), send these events to a event-runner instance and it redirect these\n events to other apps that are subscrived for them. This gem also provides\n classes that you can use to process the received events too.\n"
|
16
16
|
s.email = "d.camata@gmail.com"
|
17
17
|
s.extra_rdoc_files = [
|
@@ -39,6 +39,7 @@ Gem::Specification.new do |s|
|
|
39
39
|
"lib/active_spy/rails/listener.rb",
|
40
40
|
"lib/active_spy/rails/railtie.rb",
|
41
41
|
"lib/active_spy/rails/spy.rb",
|
42
|
+
"lib/active_spy/rails/validation.rb",
|
42
43
|
"lib/active_spy/spy/spy.rb",
|
43
44
|
"lib/active_spy/spy/spy_list.rb",
|
44
45
|
"lib/rails/generators/active_spy/install/install_generator.rb",
|
@@ -43,6 +43,22 @@ module ActiveSpy
|
|
43
43
|
@event_host
|
44
44
|
end
|
45
45
|
|
46
|
+
# Set if the gem is in development mode or not
|
47
|
+
#
|
48
|
+
# @param [Boolean] development moded state to set
|
49
|
+
#
|
50
|
+
# @return [Boolean] development moded state to set
|
51
|
+
def development_mode(mode = nil)
|
52
|
+
@development_mode = mode unless mode.nil?
|
53
|
+
@development_mode
|
54
|
+
end
|
55
|
+
|
56
|
+
# Imperative method to set development mode.
|
57
|
+
#
|
58
|
+
def development_mode!
|
59
|
+
@development_mode = true
|
60
|
+
end
|
61
|
+
|
46
62
|
# Set the default event-runner port
|
47
63
|
#
|
48
64
|
# @param [String] port to set
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'rest-client'
|
2
2
|
require 'singleton'
|
3
|
+
require 'json'
|
3
4
|
|
4
5
|
module ActiveSpy
|
5
6
|
# Module to hold Rails specific classes and helpers.
|
@@ -40,13 +41,19 @@ module ActiveSpy
|
|
40
41
|
# after callback is not explicitly defined.
|
41
42
|
#
|
42
43
|
def method_missing(method, *_args, &_block)
|
44
|
+
event_json = { event: get_request_params(method) }.to_json
|
45
|
+
ActiveSpy::Rails::Validation::Event.new(event_json).validate!
|
46
|
+
send_event_request(event_json) unless ActiveSpy::Configuration.development_mode
|
47
|
+
remove_is_new_method(@object)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Sends the event request to the configured event-runner instance.
|
51
|
+
#
|
52
|
+
def send_event_request(event_json)
|
43
53
|
host = ActiveSpy::Configuration.event_host
|
44
54
|
port = ActiveSpy::Configuration.event_port
|
45
|
-
|
46
|
-
event_json = { event: get_request_params(method) }.to_json
|
47
55
|
RestClient.post "#{host}:#{port}/events", event_json,
|
48
56
|
content_type: :json
|
49
|
-
remove_is_new_method(@object)
|
50
57
|
end
|
51
58
|
|
52
59
|
# Get the event request params for a given +method+.
|
@@ -107,8 +107,8 @@ module ActiveSpy
|
|
107
107
|
def add_hooks(hooks_to_add)
|
108
108
|
hooks_to_add.each do |hook|
|
109
109
|
hook = {
|
110
|
-
'hook'=> {
|
111
|
-
'class'=> hook['class'],
|
110
|
+
'hook' => {
|
111
|
+
'class' => hook['class'],
|
112
112
|
'post_path' => ActiveSpy::Engine.routes.url_helpers.notifications_path(hook['post_class'].downcase),
|
113
113
|
}
|
114
114
|
}
|
@@ -24,13 +24,13 @@ module ActiveSpy
|
|
24
24
|
# Set the external class of the model that we are listening to.
|
25
25
|
#
|
26
26
|
def self.external_class(klass)
|
27
|
-
|
27
|
+
@external_classes = [klass]
|
28
28
|
end
|
29
29
|
|
30
30
|
# Set the external classes which we are listening to.
|
31
31
|
#
|
32
32
|
def self.external_classes(*classes)
|
33
|
-
|
33
|
+
@external_classes = classes
|
34
34
|
end
|
35
35
|
|
36
36
|
# Convert the listener class into one or more hook hashes
|
@@ -38,7 +38,7 @@ module ActiveSpy
|
|
38
38
|
def self.to_hook
|
39
39
|
hooks = []
|
40
40
|
hook_classes.each do |hook_class|
|
41
|
-
hooks << { 'class' => hook_class, 'post_class'=> name.split('Listener')[0] }
|
41
|
+
hooks << { 'class' => hook_class, 'post_class' => name.split('Listener')[0] }
|
42
42
|
end
|
43
43
|
hooks
|
44
44
|
end
|
@@ -46,7 +46,7 @@ module ActiveSpy
|
|
46
46
|
# Get the classes that we are listegnig to.
|
47
47
|
#
|
48
48
|
def self.hook_classes
|
49
|
-
return
|
49
|
+
return @external_classes if defined? @external_classes
|
50
50
|
[name.split('Listener')[0]]
|
51
51
|
end
|
52
52
|
|
data/lib/active_spy/rails/spy.rb
CHANGED
@@ -34,17 +34,15 @@ module ActiveSpy
|
|
34
34
|
# instead, it will be returned.
|
35
35
|
#
|
36
36
|
def dynamically_define_method_or_call_block(abstract_name, method_name)
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
send(:instance_variable_set, "@#{abstract_name}", new_value)
|
47
|
-
end
|
37
|
+
return unless method_name
|
38
|
+
if abstract_name == method_name
|
39
|
+
attr_accessor method_name
|
40
|
+
else
|
41
|
+
define_method method_name do
|
42
|
+
send(:instance_variable_get, "@#{abstract_name}")
|
43
|
+
end
|
44
|
+
define_method "#{method_name}=" do |new_value|
|
45
|
+
send(:instance_variable_set, "@#{abstract_name}", new_value)
|
48
46
|
end
|
49
47
|
end
|
50
48
|
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module ActiveSpy
|
4
|
+
module Rails
|
5
|
+
# Module to hold classes that are intended to validate something.
|
6
|
+
#
|
7
|
+
module Validation
|
8
|
+
# Class responsible to validate event that are send to event-runner
|
9
|
+
# instances.
|
10
|
+
#
|
11
|
+
class Event
|
12
|
+
|
13
|
+
def initialize(event_json)
|
14
|
+
@event = JSON.load(event_json)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Validates the +event_json+ provided in the initializer
|
18
|
+
#
|
19
|
+
# (see #initialize)
|
20
|
+
def validate!
|
21
|
+
check_actor_key(@event['actor'])
|
22
|
+
check_realm_key(@event['realm'])
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
# Check if actor is valid.
|
28
|
+
#
|
29
|
+
def check_actor_key(actor)
|
30
|
+
fail ActorNotPresent if actor.nil?
|
31
|
+
errors = get_errors_from_hash(actor, :actor)
|
32
|
+
fail InvalidActor, errors unless errors.empty?
|
33
|
+
end
|
34
|
+
|
35
|
+
# Check if realm is valid.
|
36
|
+
#
|
37
|
+
def check_realm_key(realm)
|
38
|
+
fail RealmNotPresent if realm.nil?
|
39
|
+
errors = get_errors_from_hash(realm, :realm)
|
40
|
+
fail InvalidRealm, errors unless errors.empty?
|
41
|
+
end
|
42
|
+
|
43
|
+
# Get the error from +data+ regarding +hash_type+ checking if the
|
44
|
+
# required keys are being provided.
|
45
|
+
#
|
46
|
+
def get_errors_from_hash(data, hash_type)
|
47
|
+
keys = hash_type == :actor ? required_actor_keys : required_realm_keys
|
48
|
+
keys.map do |key|
|
49
|
+
"#{key} should not be empty." if data[key].nil?
|
50
|
+
end.compact.join(' ')
|
51
|
+
end
|
52
|
+
|
53
|
+
# Required keys for an actor to be valid.
|
54
|
+
#
|
55
|
+
def required_actor_keys
|
56
|
+
%w[id class login url avatar_url]
|
57
|
+
end
|
58
|
+
|
59
|
+
# Required keys for realm to be valid.
|
60
|
+
#
|
61
|
+
def required_realm_keys
|
62
|
+
%w[id class name url]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Error when actor is not present.
|
67
|
+
#
|
68
|
+
class ActorNotPresent < RuntimeError
|
69
|
+
end
|
70
|
+
|
71
|
+
# Error when the actor is invalid.
|
72
|
+
#
|
73
|
+
class InvalidActor < RuntimeError
|
74
|
+
end
|
75
|
+
|
76
|
+
# Error when realm is not present.
|
77
|
+
#
|
78
|
+
class RealmNotPresent < RuntimeError
|
79
|
+
end
|
80
|
+
|
81
|
+
# Error when the realm is invalid.
|
82
|
+
#
|
83
|
+
class InvalidRealm < RuntimeError
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/lib/active_spy.rb
CHANGED
@@ -2,13 +2,17 @@ require 'active_spy/configuration'
|
|
2
2
|
require 'active_spy/base'
|
3
3
|
require 'active_spy/spy/spy'
|
4
4
|
require 'active_spy/spy/spy_list'
|
5
|
-
|
6
|
-
|
7
|
-
require 'active_spy/rails/
|
8
|
-
require 'active_spy/rails/
|
9
|
-
require 'active_spy/rails/
|
10
|
-
require 'active_spy/rails/
|
11
|
-
require 'active_spy/rails/
|
5
|
+
|
6
|
+
if defined?(Rails)
|
7
|
+
require 'active_spy/rails/base'
|
8
|
+
require 'active_spy/rails/spy'
|
9
|
+
require 'active_spy/rails/railtie'
|
10
|
+
require 'active_spy/rails/engine'
|
11
|
+
require 'active_spy/rails/engine'
|
12
|
+
require 'active_spy/rails/hook_list'
|
13
|
+
require 'active_spy/rails/listener'
|
14
|
+
require 'active_spy/rails/validation'
|
15
|
+
end
|
12
16
|
|
13
17
|
# Base module for the gem
|
14
18
|
#
|
@@ -29,11 +33,11 @@ module ActiveSpy
|
|
29
33
|
def self.register_service
|
30
34
|
host = ActiveSpy::Configuration.event_host
|
31
35
|
port = ActiveSpy::Configuration.event_port
|
32
|
-
|
36
|
+
@base_url = "#{host}:#{port}/services"
|
33
37
|
|
34
38
|
return if self.service_registered?
|
35
39
|
service = { service: ActiveSpy::Configuration.settings }.to_json
|
36
|
-
RestClient.post(
|
40
|
+
RestClient.post(@base_url, service, content_type: :json)
|
37
41
|
end
|
38
42
|
|
39
43
|
# @!method self.service_registered?
|
@@ -43,7 +47,7 @@ module ActiveSpy
|
|
43
47
|
def self.service_registered?
|
44
48
|
name = ActiveSpy::Configuration.name
|
45
49
|
begin
|
46
|
-
|
50
|
+
RestClient.get "#{@base_url}/#{name.downcase.gsub(' ', '-').strip}"
|
47
51
|
rescue RestClient::ResourceNotFound
|
48
52
|
return false
|
49
53
|
else
|
@@ -12,7 +12,7 @@ module ActiveSpy
|
|
12
12
|
# The source for templates
|
13
13
|
#
|
14
14
|
def self.source_root
|
15
|
-
@@_active_spy_source_root ||= File.expand_path(
|
15
|
+
@@_active_spy_source_root ||= File.expand_path('../templates', __FILE__)
|
16
16
|
end
|
17
17
|
|
18
18
|
# Creates a config file based in the +active_spy.yml+ file.
|
@@ -26,7 +26,7 @@ module ActiveSpy
|
|
26
26
|
def inject_config_into_environment
|
27
27
|
content = File.read(File.join(@@_active_spy_source_root, 'initializer.rb'))
|
28
28
|
|
29
|
-
File.open(
|
29
|
+
File.open('config/environment.rb', 'a+') do |f|
|
30
30
|
f << content unless f.read.include?(content)
|
31
31
|
end
|
32
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_spy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Douglas Camata
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -225,6 +225,7 @@ files:
|
|
225
225
|
- lib/active_spy/rails/listener.rb
|
226
226
|
- lib/active_spy/rails/railtie.rb
|
227
227
|
- lib/active_spy/rails/spy.rb
|
228
|
+
- lib/active_spy/rails/validation.rb
|
228
229
|
- lib/active_spy/spy/spy.rb
|
229
230
|
- lib/active_spy/spy/spy_list.rb
|
230
231
|
- lib/rails/generators/active_spy/install/install_generator.rb
|