active_spy 1.0.0.rc → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -4
- data/Gemfile.lock +39 -38
- data/README.md +17 -32
- data/VERSION +1 -1
- data/active_spy.gemspec +21 -21
- data/app/controllers/active_spy/notifications_controller.rb +21 -0
- data/config/initializers/{active_event_configuration_loader.rb → active_spy_configuration_loader.rb} +7 -2
- data/lib/active_spy/rails/base.rb +6 -11
- data/lib/active_spy/rails/hook_list.rb +10 -6
- data/lib/active_spy/rails/listener.rb +45 -15
- data/lib/active_spy/rails/railtie.rb +0 -4
- data/lib/active_spy/rails/spy.rb +23 -11
- data/lib/active_spy/spy/spy_list.rb +2 -2
- data/lib/active_spy.rb +8 -3
- data/lib/rails/generators/active_spy/install/install_generator.rb +41 -0
- data/lib/rails/generators/{active_event → active_spy}/install/templates/initializer.rb +1 -0
- metadata +23 -23
- data/app/controllers/active_event/notifications_controller.rb +0 -6
- data/lib/rails/generators/active_event/install/install_generator.rb +0 -21
- /data/lib/rails/generators/{active_event/install/templates/active_event.yml → active_spy/install/templates/active_spy.yml} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63646f55b335e82ea778e8c3a4993a476be52765
|
4
|
+
data.tar.gz: 2b8997b23b8593f80261ce6bfb85bffedfce549f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84773196a680aff01ad330c72d87f088a47f7b715f0a007582ccf103adc650442fdb6c50ed1685e9b51aa13667d521c86f9279de82241824de5f062b38e00e3a
|
7
|
+
data.tar.gz: 9539e10b3494b00222b2b4e11c3ca45d313beb53303f17018a2540b363be12e83a5349354ae949b648dedbee9cc94cba3270594df74b7739844f21266b10b634
|
data/Gemfile
CHANGED
@@ -2,15 +2,15 @@ source 'http://rubygems.org'
|
|
2
2
|
# Add dependencies required to use your gem here.
|
3
3
|
# Example:
|
4
4
|
# gem "activesupport", ">= 2.3.5"
|
5
|
-
gem 'activesupport', '
|
6
|
-
gem 'rest-client', '1.6.7'
|
5
|
+
gem 'activesupport', '>= 4.0.0'
|
6
|
+
gem 'rest-client', '~> 1.6.7'
|
7
7
|
gem 'hashie'
|
8
8
|
|
9
9
|
# Add dependencies to develop your gem here.
|
10
10
|
# Include everything needed to run rake, tests, features, etc.
|
11
11
|
group :development do
|
12
|
-
gem 'activerecord', '
|
13
|
-
gem 'rails', '
|
12
|
+
gem 'activerecord', '>= 4.0.0'
|
13
|
+
gem 'rails', '>= 4.0.0'
|
14
14
|
gem 'rubocop', '~> 0.24.0'
|
15
15
|
gem 'pry', '~> 0.10.0'
|
16
16
|
gem 'rspec', '~> 3.0.0'
|
data/Gemfile.lock
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
actionmailer (4.1.
|
5
|
-
actionpack (= 4.1.
|
6
|
-
actionview (= 4.1.
|
4
|
+
actionmailer (4.1.4)
|
5
|
+
actionpack (= 4.1.4)
|
6
|
+
actionview (= 4.1.4)
|
7
7
|
mail (~> 2.5.4)
|
8
|
-
actionpack (4.1.
|
9
|
-
actionview (= 4.1.
|
10
|
-
activesupport (= 4.1.
|
8
|
+
actionpack (4.1.4)
|
9
|
+
actionview (= 4.1.4)
|
10
|
+
activesupport (= 4.1.4)
|
11
11
|
rack (~> 1.5.2)
|
12
12
|
rack-test (~> 0.6.2)
|
13
|
-
actionview (4.1.
|
14
|
-
activesupport (= 4.1.
|
13
|
+
actionview (4.1.4)
|
14
|
+
activesupport (= 4.1.4)
|
15
15
|
builder (~> 3.1)
|
16
16
|
erubis (~> 2.7.0)
|
17
|
-
activemodel (4.1.
|
18
|
-
activesupport (= 4.1.
|
17
|
+
activemodel (4.1.4)
|
18
|
+
activesupport (= 4.1.4)
|
19
19
|
builder (~> 3.1)
|
20
|
-
activerecord (4.1.
|
21
|
-
activemodel (= 4.1.
|
22
|
-
activesupport (= 4.1.
|
20
|
+
activerecord (4.1.4)
|
21
|
+
activemodel (= 4.1.4)
|
22
|
+
activesupport (= 4.1.4)
|
23
23
|
arel (~> 5.0.0)
|
24
|
-
activesupport (4.1.
|
24
|
+
activesupport (4.1.4)
|
25
25
|
i18n (~> 0.6, >= 0.6.9)
|
26
26
|
json (~> 1.7, >= 1.7.7)
|
27
27
|
minitest (~> 5.1)
|
@@ -48,10 +48,10 @@ GEM
|
|
48
48
|
multi_json (>= 1.7.5, < 2.0)
|
49
49
|
nokogiri (~> 1.6.0)
|
50
50
|
oauth2
|
51
|
-
hashie (3.
|
51
|
+
hashie (3.2.0)
|
52
52
|
highline (1.6.21)
|
53
53
|
hike (1.2.3)
|
54
|
-
i18n (0.6.
|
54
|
+
i18n (0.6.11)
|
55
55
|
jeweler (2.0.1)
|
56
56
|
builder
|
57
57
|
bundler (>= 1.0)
|
@@ -69,19 +69,19 @@ GEM
|
|
69
69
|
method_source (0.8.2)
|
70
70
|
mime-types (1.25.1)
|
71
71
|
mini_portile (0.6.0)
|
72
|
-
minitest (5.
|
72
|
+
minitest (5.4.0)
|
73
73
|
multi_json (1.10.1)
|
74
74
|
multi_xml (0.5.5)
|
75
75
|
multipart-post (2.0.0)
|
76
76
|
nokogiri (1.6.2.1)
|
77
77
|
mini_portile (= 0.6.0)
|
78
|
-
oauth2 (0.
|
78
|
+
oauth2 (1.0.0)
|
79
79
|
faraday (>= 0.8, < 0.10)
|
80
80
|
jwt (~> 1.0)
|
81
81
|
multi_json (~> 1.3)
|
82
82
|
multi_xml (~> 0.5)
|
83
83
|
rack (~> 1.2)
|
84
|
-
parser (2.2.0.pre.
|
84
|
+
parser (2.2.0.pre.3)
|
85
85
|
ast (>= 1.1, < 3.0)
|
86
86
|
slop (~> 3.4, >= 3.4.5)
|
87
87
|
polyglot (0.3.5)
|
@@ -93,27 +93,28 @@ GEM
|
|
93
93
|
rack (1.5.2)
|
94
94
|
rack-test (0.6.2)
|
95
95
|
rack (>= 1.0)
|
96
|
-
rails (4.1.
|
97
|
-
actionmailer (= 4.1.
|
98
|
-
actionpack (= 4.1.
|
99
|
-
actionview (= 4.1.
|
100
|
-
activemodel (= 4.1.
|
101
|
-
activerecord (= 4.1.
|
102
|
-
activesupport (= 4.1.
|
96
|
+
rails (4.1.4)
|
97
|
+
actionmailer (= 4.1.4)
|
98
|
+
actionpack (= 4.1.4)
|
99
|
+
actionview (= 4.1.4)
|
100
|
+
activemodel (= 4.1.4)
|
101
|
+
activerecord (= 4.1.4)
|
102
|
+
activesupport (= 4.1.4)
|
103
103
|
bundler (>= 1.3.0, < 2.0)
|
104
|
-
railties (= 4.1.
|
104
|
+
railties (= 4.1.4)
|
105
105
|
sprockets-rails (~> 2.0)
|
106
|
-
railties (4.1.
|
107
|
-
actionpack (= 4.1.
|
108
|
-
activesupport (= 4.1.
|
106
|
+
railties (4.1.4)
|
107
|
+
actionpack (= 4.1.4)
|
108
|
+
activesupport (= 4.1.4)
|
109
109
|
rake (>= 0.8.7)
|
110
110
|
thor (>= 0.18.1, < 2.0)
|
111
111
|
rainbow (2.0.0)
|
112
112
|
rake (10.3.2)
|
113
113
|
rdoc (4.1.1)
|
114
114
|
json (~> 1.4)
|
115
|
-
rest-client (1.6.
|
116
|
-
mime-types (
|
115
|
+
rest-client (1.6.8)
|
116
|
+
mime-types (~> 1.16)
|
117
|
+
rdoc (>= 2.4.2)
|
117
118
|
rspec (3.0.0)
|
118
119
|
rspec-core (~> 3.0.0)
|
119
120
|
rspec-expectations (~> 3.0.0)
|
@@ -126,9 +127,9 @@ GEM
|
|
126
127
|
rspec-mocks (3.0.2)
|
127
128
|
rspec-support (~> 3.0.0)
|
128
129
|
rspec-support (3.0.2)
|
129
|
-
rubocop (0.24.
|
130
|
+
rubocop (0.24.1)
|
130
131
|
json (>= 1.7.7, < 2)
|
131
|
-
parser (>= 2.2.0.pre.
|
132
|
+
parser (>= 2.2.0.pre.3, < 3.0)
|
132
133
|
powerpack (~> 0.0.6)
|
133
134
|
rainbow (>= 1.99.1, < 3.0)
|
134
135
|
ruby-progressbar (~> 1.4)
|
@@ -162,15 +163,15 @@ PLATFORMS
|
|
162
163
|
ruby
|
163
164
|
|
164
165
|
DEPENDENCIES
|
165
|
-
activerecord (
|
166
|
-
activesupport (
|
166
|
+
activerecord (>= 4.0.0)
|
167
|
+
activesupport (>= 4.0.0)
|
167
168
|
bundler (~> 1.6.3)
|
168
169
|
hashie
|
169
170
|
jeweler (~> 2.0.1)
|
170
171
|
pry (~> 0.10.0)
|
171
|
-
rails (
|
172
|
+
rails (>= 4.0.0)
|
172
173
|
rdoc (~> 4.1.1)
|
173
|
-
rest-client (
|
174
|
+
rest-client (~> 1.6.7)
|
174
175
|
rspec (~> 3.0.0)
|
175
176
|
rubocop (~> 0.24.0)
|
176
177
|
simplecov
|
data/README.md
CHANGED
@@ -46,9 +46,10 @@ Chair.new.break!
|
|
46
46
|
### Rails app
|
47
47
|
|
48
48
|
First of all, run the install generator: `rails g active_spy:install`.
|
49
|
-
This command will
|
50
|
-
`config/active_spy.yml
|
51
|
-
|
49
|
+
This command will inject gem's configurations at `config/environment.rb`,
|
50
|
+
generate `config/active_spy.yml` file and mount active_spy's engine at
|
51
|
+
`config/routes.rb`. You must edit the YAML file to use your own app
|
52
|
+
parameters and optionally change the engine route.
|
52
53
|
|
53
54
|
Then, create a `ProductEvent` class at `RAILS_ROOT/app/events`
|
54
55
|
|
@@ -61,36 +62,29 @@ Declare ActiveSpy's `model_realm`, `model_actor`, and `watch_model_changes`
|
|
61
62
|
methods in the model that is being watched:
|
62
63
|
|
63
64
|
```ruby
|
64
|
-
|
65
65
|
class User < ActiveRecord::Base
|
66
66
|
belong_to :project
|
67
67
|
belongs_to :project_group
|
68
68
|
|
69
|
-
model_realm
|
70
|
-
model_actor :
|
69
|
+
model_realm :realm
|
70
|
+
model_actor :my_actor
|
71
71
|
watch_model_changes
|
72
72
|
|
73
|
-
def get_actor
|
74
|
-
self
|
75
|
-
end
|
76
|
-
|
77
73
|
# ActiveSpy's payload_for method override
|
78
74
|
#
|
79
75
|
# def payload_for(method)
|
80
76
|
# { user: attributes }
|
81
77
|
# end
|
82
|
-
|
83
|
-
# ActiveSpy's realm method override
|
84
|
-
#
|
85
|
-
# def realm
|
86
|
-
# return project_group if project_group.admin == self
|
87
|
-
# project
|
88
|
-
# end
|
89
78
|
end
|
90
79
|
```
|
91
80
|
|
92
|
-
You may override `payload_for(method)
|
93
|
-
use
|
81
|
+
You may override `payload_for(method)` for more complex use cases. The
|
82
|
+
`model_realm :realm` will create a realm attribute accessor for you to use
|
83
|
+
wherever you want, usually inside a controller, to set the realm of the user.
|
84
|
+
The `model_actor :my_actor` will create an attribute acessdor for the actor,
|
85
|
+
but will define it using the `my_actor` and `my_actor=` methods. This allows
|
86
|
+
you to avoid your attributes, methods and relations to be overwritten. Both
|
87
|
+
realm and actor and necesary to be bent to the event runner.
|
94
88
|
|
95
89
|
Now, when you can create, update or delete instances of User, a request will be
|
96
90
|
sent to the `event_host` and `event_port` defined in the configuration YAML file.
|
@@ -99,7 +93,7 @@ The body will be filled with a hash like this, as json:
|
|
99
93
|
```
|
100
94
|
{
|
101
95
|
type: 'User', # object's class name
|
102
|
-
actor: user.
|
96
|
+
actor: user.my_actor, # object's actor (who made that action)
|
103
97
|
realm: user.realm, # object's realm
|
104
98
|
action: action # the action executed in the object
|
105
99
|
payload: {
|
@@ -114,18 +108,9 @@ to suit your own needs.
|
|
114
108
|
|
115
109
|
#### Handling the request received by the event runner
|
116
110
|
|
117
|
-
To handle the request received by the event-runner, you
|
118
|
-
|
119
|
-
|
120
|
-
```ruby
|
121
|
-
Rails.application.routes.draw do
|
122
|
-
mount ActiveSpy::Engine => 'active_spy', as: :active_spy
|
123
|
-
end
|
124
|
-
```
|
125
|
-
|
126
|
-
Then, you need to create a listener class, inheriting from
|
127
|
-
`ActiveSpy::Rails::Listener` and named using the watched model's name
|
128
|
-
plus the `Listener` postfix, like this:
|
111
|
+
To handle the request received by the event-runner, you need to create a
|
112
|
+
listener class, inheriting from `ActiveSpy::Rails::Listener` and named using the
|
113
|
+
watched model's name plus the `Listener` postfix, like this:
|
129
114
|
|
130
115
|
```ruby
|
131
116
|
class UserListener < ActiveSpy::Rails::Listener
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.0
|
1
|
+
1.0.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.0.0
|
5
|
+
# stub: active_spy 1.0.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "active_spy"
|
9
|
-
s.version = "1.0.0
|
9
|
+
s.version = "1.0.0"
|
10
10
|
|
11
|
-
s.required_rubygems_version = Gem::Requirement.new("
|
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-16"
|
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 = [
|
@@ -27,8 +27,8 @@ Gem::Specification.new do |s|
|
|
27
27
|
"Rakefile",
|
28
28
|
"VERSION",
|
29
29
|
"active_spy.gemspec",
|
30
|
-
"app/controllers/
|
31
|
-
"config/initializers/
|
30
|
+
"app/controllers/active_spy/notifications_controller.rb",
|
31
|
+
"config/initializers/active_spy_configuration_loader.rb",
|
32
32
|
"config/routes.rb",
|
33
33
|
"lib/active_spy.rb",
|
34
34
|
"lib/active_spy/base.rb",
|
@@ -41,9 +41,9 @@ Gem::Specification.new do |s|
|
|
41
41
|
"lib/active_spy/rails/spy.rb",
|
42
42
|
"lib/active_spy/spy/spy.rb",
|
43
43
|
"lib/active_spy/spy/spy_list.rb",
|
44
|
-
"lib/rails/generators/
|
45
|
-
"lib/rails/generators/
|
46
|
-
"lib/rails/generators/
|
44
|
+
"lib/rails/generators/active_spy/install/install_generator.rb",
|
45
|
+
"lib/rails/generators/active_spy/install/templates/active_spy.yml",
|
46
|
+
"lib/rails/generators/active_spy/install/templates/initializer.rb"
|
47
47
|
]
|
48
48
|
s.homepage = "http://github.com/edools/active_spy"
|
49
49
|
s.licenses = ["MIT"]
|
@@ -54,11 +54,11 @@ Gem::Specification.new do |s|
|
|
54
54
|
s.specification_version = 4
|
55
55
|
|
56
56
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
57
|
-
s.add_runtime_dependency(%q<activesupport>, ["
|
58
|
-
s.add_runtime_dependency(%q<rest-client>, ["
|
57
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 4.0.0"])
|
58
|
+
s.add_runtime_dependency(%q<rest-client>, ["~> 1.6.7"])
|
59
59
|
s.add_runtime_dependency(%q<hashie>, [">= 0"])
|
60
|
-
s.add_development_dependency(%q<activerecord>, ["
|
61
|
-
s.add_development_dependency(%q<rails>, ["
|
60
|
+
s.add_development_dependency(%q<activerecord>, [">= 4.0.0"])
|
61
|
+
s.add_development_dependency(%q<rails>, [">= 4.0.0"])
|
62
62
|
s.add_development_dependency(%q<rubocop>, ["~> 0.24.0"])
|
63
63
|
s.add_development_dependency(%q<pry>, ["~> 0.10.0"])
|
64
64
|
s.add_development_dependency(%q<rspec>, ["~> 3.0.0"])
|
@@ -68,11 +68,11 @@ Gem::Specification.new do |s|
|
|
68
68
|
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
69
69
|
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
70
70
|
else
|
71
|
-
s.add_dependency(%q<activesupport>, ["
|
72
|
-
s.add_dependency(%q<rest-client>, ["
|
71
|
+
s.add_dependency(%q<activesupport>, [">= 4.0.0"])
|
72
|
+
s.add_dependency(%q<rest-client>, ["~> 1.6.7"])
|
73
73
|
s.add_dependency(%q<hashie>, [">= 0"])
|
74
|
-
s.add_dependency(%q<activerecord>, ["
|
75
|
-
s.add_dependency(%q<rails>, ["
|
74
|
+
s.add_dependency(%q<activerecord>, [">= 4.0.0"])
|
75
|
+
s.add_dependency(%q<rails>, [">= 4.0.0"])
|
76
76
|
s.add_dependency(%q<rubocop>, ["~> 0.24.0"])
|
77
77
|
s.add_dependency(%q<pry>, ["~> 0.10.0"])
|
78
78
|
s.add_dependency(%q<rspec>, ["~> 3.0.0"])
|
@@ -83,11 +83,11 @@ Gem::Specification.new do |s|
|
|
83
83
|
s.add_dependency(%q<simplecov>, [">= 0"])
|
84
84
|
end
|
85
85
|
else
|
86
|
-
s.add_dependency(%q<activesupport>, ["
|
87
|
-
s.add_dependency(%q<rest-client>, ["
|
86
|
+
s.add_dependency(%q<activesupport>, [">= 4.0.0"])
|
87
|
+
s.add_dependency(%q<rest-client>, ["~> 1.6.7"])
|
88
88
|
s.add_dependency(%q<hashie>, [">= 0"])
|
89
|
-
s.add_dependency(%q<activerecord>, ["
|
90
|
-
s.add_dependency(%q<rails>, ["
|
89
|
+
s.add_dependency(%q<activerecord>, [">= 4.0.0"])
|
90
|
+
s.add_dependency(%q<rails>, [">= 4.0.0"])
|
91
91
|
s.add_dependency(%q<rubocop>, ["~> 0.24.0"])
|
92
92
|
s.add_dependency(%q<pry>, ["~> 0.10.0"])
|
93
93
|
s.add_dependency(%q<rspec>, ["~> 3.0.0"])
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ActiveSpy
|
2
|
+
class NotificationsController < ActionController::Base
|
3
|
+
def handle
|
4
|
+
request.format = 'application/json'
|
5
|
+
hooks = ActiveSpy::Rails::HookList.hooks
|
6
|
+
result = nil
|
7
|
+
hooks.each do |hook|
|
8
|
+
if hook['post_class'].downcase == params['class']
|
9
|
+
listener = "#{hook['post_class']}Listener".constantize
|
10
|
+
result = listener.new.handle(params['event'])
|
11
|
+
if result.errors.present?
|
12
|
+
render json: result.errors
|
13
|
+
else
|
14
|
+
render json: result
|
15
|
+
end and return
|
16
|
+
end
|
17
|
+
end
|
18
|
+
render nothing: true, status: :not_found
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/config/initializers/{active_event_configuration_loader.rb → active_spy_configuration_loader.rb}
RENAMED
@@ -1,8 +1,13 @@
|
|
1
1
|
require 'hashie'
|
2
2
|
|
3
3
|
yml_path = Rails.root.join('config', 'active_spy.yml')
|
4
|
-
|
5
|
-
|
4
|
+
if File.exists?(yml_path)
|
5
|
+
all_settings = YAML.load_file(yml_path)
|
6
|
+
env_settings = Hashie::Mash.new(all_settings[Rails.env])
|
7
|
+
else
|
8
|
+
# TODO: Add a warning here
|
9
|
+
env_settings = {}
|
10
|
+
end
|
6
11
|
|
7
12
|
ActiveSpy.configure do |config|
|
8
13
|
config.name env_settings['name']
|
@@ -10,20 +10,15 @@ module ActiveSpy
|
|
10
10
|
class Base
|
11
11
|
def initialize(object)
|
12
12
|
@object = object
|
13
|
+
inject_is_new_method(@object)
|
14
|
+
@object.is_new = true if @object.new_record?
|
13
15
|
end
|
14
16
|
|
15
17
|
# Overriding to avoid sending the object to server 2 times (in both
|
16
18
|
# before and after callabcks).
|
17
19
|
#
|
18
20
|
def respond_to?(method)
|
19
|
-
method.include?('after_')
|
20
|
-
end
|
21
|
-
|
22
|
-
# Set a flag in the object to tell us wether it's a new record or not.
|
23
|
-
#
|
24
|
-
def before_save
|
25
|
-
inject_is_new_method(@object)
|
26
|
-
@object.is_new = true if @object.new_record?
|
21
|
+
method.include?('after_')
|
27
22
|
end
|
28
23
|
|
29
24
|
# Inject an attribute in the +object+, called +is_new?+ and a setter
|
@@ -48,7 +43,7 @@ module ActiveSpy
|
|
48
43
|
host = ActiveSpy::Configuration.event_host
|
49
44
|
port = ActiveSpy::Configuration.event_port
|
50
45
|
|
51
|
-
RestClient.post "#{host}:#{port}/",
|
46
|
+
RestClient.post "#{host}:#{port}/events",
|
52
47
|
event: get_request_params(method)
|
53
48
|
remove_is_new_method(@object)
|
54
49
|
end
|
@@ -60,8 +55,8 @@ module ActiveSpy
|
|
60
55
|
action = get_action(real_method)
|
61
56
|
{
|
62
57
|
type: @object.class.name,
|
63
|
-
actor: @object.actor,
|
64
|
-
realm: @object.realm,
|
58
|
+
actor: @object.instance_variable_get('@actor'),
|
59
|
+
realm: @object.instance_variable_get('@realm'),
|
65
60
|
payload: @object.payload_for(action),
|
66
61
|
action: action
|
67
62
|
}
|
@@ -6,6 +6,10 @@ module ActiveSpy
|
|
6
6
|
class HookList
|
7
7
|
include Singleton
|
8
8
|
|
9
|
+
# Simple attribute reader for hooks
|
10
|
+
#
|
11
|
+
attr_reader :hooks
|
12
|
+
|
9
13
|
# Initialize an empty hook list
|
10
14
|
#
|
11
15
|
def initialize
|
@@ -39,6 +43,7 @@ module ActiveSpy
|
|
39
43
|
# them already exists, they will be excluded and readded.
|
40
44
|
#
|
41
45
|
def register
|
46
|
+
@hooks = @hooks.map(&:to_hook).flatten
|
42
47
|
old_hooks = get_old_hooks
|
43
48
|
hooks_to_delete = get_hooks_to_delete(old_hooks)
|
44
49
|
hooks_to_add = get_hooks_to_add(old_hooks)
|
@@ -49,7 +54,6 @@ module ActiveSpy
|
|
49
54
|
# Get the old hooks list for this service from the event-runner
|
50
55
|
#
|
51
56
|
def get_old_hooks
|
52
|
-
|
53
57
|
JSON.load(RestClient.get(@base_service_url))['hooks']
|
54
58
|
end
|
55
59
|
|
@@ -102,11 +106,11 @@ module ActiveSpy
|
|
102
106
|
#
|
103
107
|
def add_hooks(hooks_to_add)
|
104
108
|
hooks_to_add.each do |hook|
|
105
|
-
RestClient.post "#{@base_service_url}/hooks",
|
106
|
-
'
|
107
|
-
|
108
|
-
|
109
|
-
|
109
|
+
RestClient.post "#{@base_service_url}/hooks",
|
110
|
+
'hook'=> {
|
111
|
+
'class'=> hook['class'],
|
112
|
+
'post_path' => ActiveSpy::Engine.routes.url_helpers.notifications_path(hook['post_class'].downcase),
|
113
|
+
}
|
110
114
|
end
|
111
115
|
end
|
112
116
|
end
|
@@ -18,23 +18,47 @@ module ActiveSpy
|
|
18
18
|
# later registration of them within the event runner.
|
19
19
|
#
|
20
20
|
def self.inherited(child)
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
ActiveSpy::Rails::HookList << child
|
22
|
+
end
|
23
|
+
|
24
|
+
# Set the external class of the model that we are listening to.
|
25
|
+
#
|
26
|
+
def self.external_class(klass)
|
27
|
+
@@external_classes = [klass]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Set the external classes which we are listening to.
|
31
|
+
#
|
32
|
+
def self.external_classes(*classes)
|
33
|
+
@@external_classes = classes
|
34
|
+
end
|
35
|
+
|
36
|
+
# Convert the listener class into one or more hook hashes
|
37
|
+
#
|
38
|
+
def self.to_hook
|
39
|
+
hooks = []
|
40
|
+
hook_classes.each do |hook_class|
|
41
|
+
hooks << { 'class' => hook_class, 'post_class'=> name.split('Listener')[0] }
|
25
42
|
end
|
43
|
+
hooks
|
44
|
+
end
|
45
|
+
|
46
|
+
# Get the classes that we are listegnig to.
|
47
|
+
#
|
48
|
+
def self.hook_classes
|
49
|
+
return @@external_classes if defined? @@external_classes
|
50
|
+
[name.split('Listener')[0]]
|
26
51
|
end
|
27
52
|
|
28
53
|
# Handle a request with +params+ and sync the database according to
|
29
54
|
# them.
|
30
55
|
#
|
31
56
|
def handle(params)
|
32
|
-
object_type = params.delete(
|
33
|
-
callback = params
|
34
|
-
payload_content = params.delete(
|
35
|
-
actor = params.delete(
|
36
|
-
realm = params.delete(
|
37
|
-
|
57
|
+
object_type = params.delete('type')
|
58
|
+
callback = params.delete('action')
|
59
|
+
payload_content = params.delete('payload')[object_type.downcase]
|
60
|
+
actor = params.delete('actor')
|
61
|
+
realm = params.delete('realm')
|
38
62
|
sync_database(callback, object_type, payload_content, actor, realm)
|
39
63
|
end
|
40
64
|
|
@@ -51,7 +75,9 @@ module ActiveSpy
|
|
51
75
|
#
|
52
76
|
def create(object_type, payload, _actor, _realm)
|
53
77
|
klass = get_object_class(object_type)
|
54
|
-
klass.new
|
78
|
+
object = klass.new
|
79
|
+
object.update_attributes(payload)
|
80
|
+
object
|
55
81
|
end
|
56
82
|
|
57
83
|
# Logic to handle object's update. You can override this, as you wish,
|
@@ -59,8 +85,10 @@ module ActiveSpy
|
|
59
85
|
#
|
60
86
|
def update(object_type, payload, _actor, _realm)
|
61
87
|
klass = get_object_class(object_type)
|
62
|
-
guid = payload
|
63
|
-
klass.find_by(guid: guid)
|
88
|
+
guid = payload['guid']
|
89
|
+
object = klass.find_by(guid: guid)
|
90
|
+
object.update_attributes(payload)
|
91
|
+
object
|
64
92
|
end
|
65
93
|
|
66
94
|
# Destroy a record from our database. You can override this, as you wish,
|
@@ -68,8 +96,10 @@ module ActiveSpy
|
|
68
96
|
#
|
69
97
|
def destroy(klass, payload, _actor, _realm)
|
70
98
|
klass = get_object_class(klass)
|
71
|
-
guid = payload.delete(
|
72
|
-
klass.find_by(guid: guid)
|
99
|
+
guid = payload.delete('guid')
|
100
|
+
object = klass.find_by(guid: guid)
|
101
|
+
object.destroy!
|
102
|
+
object
|
73
103
|
end
|
74
104
|
|
75
105
|
# Gets the object class. First, it'll look the {MODEL_HANDLER} hash and
|
data/lib/active_spy/rails/spy.rb
CHANGED
@@ -19,21 +19,33 @@ module ActiveSpy
|
|
19
19
|
module ClassMethods
|
20
20
|
# Class method to define the realm of the model.
|
21
21
|
#
|
22
|
-
def model_realm(realm_name = nil
|
23
|
-
realm
|
24
|
-
realm = block if block_given?
|
25
|
-
define_method :realm do
|
26
|
-
realm.call
|
27
|
-
end
|
22
|
+
def model_realm(realm_name = nil)
|
23
|
+
dynamically_define_method_or_call_block(:realm, realm_name)
|
28
24
|
end
|
29
25
|
|
30
26
|
# Class method to define the actor of the model.
|
31
27
|
#
|
32
|
-
def model_actor(actor_name = nil
|
33
|
-
actor
|
34
|
-
|
35
|
-
|
36
|
-
|
28
|
+
def model_actor(actor_name = nil)
|
29
|
+
dynamically_define_method_or_call_block(:actor, actor_name)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Defines a method called +method_name+ that will call a method called
|
33
|
+
# +method_value+ if a symbol is provided. If a block is provided
|
34
|
+
# instead, it will be returned.
|
35
|
+
#
|
36
|
+
def dynamically_define_method_or_call_block(abstract_name, method_name)
|
37
|
+
if 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
|
+
|
45
|
+
define_method "#{method_name}=" do |new_value|
|
46
|
+
send(:instance_variable_set, "@#{abstract_name}", new_value)
|
47
|
+
end
|
48
|
+
end
|
37
49
|
end
|
38
50
|
end
|
39
51
|
|
@@ -52,9 +52,9 @@ module ActiveSpy
|
|
52
52
|
ActiveSupport::Inflector.constantize(klass).class_eval do
|
53
53
|
|
54
54
|
old_method = instance_method(method)
|
55
|
-
define_method method do
|
55
|
+
define_method method do |*args, &block|
|
56
56
|
send(:invoke_callback, method, :before)
|
57
|
-
result = old_method.bind(self).call
|
57
|
+
result = old_method.bind(self).call(*args, &block)
|
58
58
|
send(:invoke_callback, method, :after)
|
59
59
|
result
|
60
60
|
end
|
data/lib/active_spy.rb
CHANGED
@@ -32,7 +32,7 @@ module ActiveSpy
|
|
32
32
|
@@base_url = "#{host}:#{port}/services"
|
33
33
|
|
34
34
|
return if self.service_registered?
|
35
|
-
RestClient.post
|
35
|
+
RestClient.post(@@base_url, service: ActiveSpy::Configuration.settings)
|
36
36
|
end
|
37
37
|
|
38
38
|
# @!method self.service_registered?
|
@@ -41,8 +41,13 @@ module ActiveSpy
|
|
41
41
|
#
|
42
42
|
def self.service_registered?
|
43
43
|
name = ActiveSpy::Configuration.name
|
44
|
-
|
45
|
-
|
44
|
+
begin
|
45
|
+
r = RestClient.get "#{@@base_url}/#{name.downcase.gsub(' ', '-').strip}"
|
46
|
+
rescue RestClient::ResourceNotFound
|
47
|
+
return false
|
48
|
+
else
|
49
|
+
return true
|
50
|
+
end
|
46
51
|
end
|
47
52
|
end
|
48
53
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module ActiveSpy
|
4
|
+
# Module that holds the rails generators
|
5
|
+
#
|
6
|
+
module Generators
|
7
|
+
# The generator that installs the gem
|
8
|
+
#
|
9
|
+
class InstallGenerator < ::Rails::Generators::Base
|
10
|
+
desc 'Creates an active_spy gem configuration file at config/active_spy.yml, and inject configurations at config/environment.rb'
|
11
|
+
|
12
|
+
# The source for templates
|
13
|
+
#
|
14
|
+
def self.source_root
|
15
|
+
@@_active_spy_source_root ||= File.expand_path("../templates", __FILE__)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Creates a config file based in the +active_spy.yml+ file.
|
19
|
+
#
|
20
|
+
def create_config_file
|
21
|
+
template 'active_spy.yml', File.join('config', 'active_spy.yml')
|
22
|
+
end
|
23
|
+
|
24
|
+
# Injects the {ActiveSpy} initialization in the environment.
|
25
|
+
#
|
26
|
+
def inject_config_into_environment
|
27
|
+
content = File.read(File.join(@@_active_spy_source_root, 'initializer.rb'))
|
28
|
+
|
29
|
+
File.open("config/environment.rb", "a+") do |f|
|
30
|
+
f << content unless f.read.include?(content)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Mount {ActiveSpy::Engine} in the route file.
|
35
|
+
#
|
36
|
+
def mount_engine
|
37
|
+
route "mount ActiveSpy::Engine => 'active_spy', as: :active_spy"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_spy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0
|
4
|
+
version: 1.0.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-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 4.
|
19
|
+
version: 4.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 4.
|
26
|
+
version: 4.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rest-client
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 1.6.7
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 1.6.7
|
41
41
|
- !ruby/object:Gem::Dependency
|
@@ -56,30 +56,30 @@ dependencies:
|
|
56
56
|
name: activerecord
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 4.
|
61
|
+
version: 4.0.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 4.
|
68
|
+
version: 4.0.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rails
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 4.
|
75
|
+
version: 4.0.0
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 4.
|
82
|
+
version: 4.0.0
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rubocop
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -213,8 +213,8 @@ files:
|
|
213
213
|
- Rakefile
|
214
214
|
- VERSION
|
215
215
|
- active_spy.gemspec
|
216
|
-
- app/controllers/
|
217
|
-
- config/initializers/
|
216
|
+
- app/controllers/active_spy/notifications_controller.rb
|
217
|
+
- config/initializers/active_spy_configuration_loader.rb
|
218
218
|
- config/routes.rb
|
219
219
|
- lib/active_spy.rb
|
220
220
|
- lib/active_spy/base.rb
|
@@ -227,9 +227,9 @@ files:
|
|
227
227
|
- lib/active_spy/rails/spy.rb
|
228
228
|
- lib/active_spy/spy/spy.rb
|
229
229
|
- lib/active_spy/spy/spy_list.rb
|
230
|
-
- lib/rails/generators/
|
231
|
-
- lib/rails/generators/
|
232
|
-
- lib/rails/generators/
|
230
|
+
- lib/rails/generators/active_spy/install/install_generator.rb
|
231
|
+
- lib/rails/generators/active_spy/install/templates/active_spy.yml
|
232
|
+
- lib/rails/generators/active_spy/install/templates/initializer.rb
|
233
233
|
homepage: http://github.com/edools/active_spy
|
234
234
|
licenses:
|
235
235
|
- MIT
|
@@ -245,9 +245,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
245
245
|
version: '0'
|
246
246
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
247
247
|
requirements:
|
248
|
-
- - "
|
248
|
+
- - ">="
|
249
249
|
- !ruby/object:Gem::Version
|
250
|
-
version:
|
250
|
+
version: '0'
|
251
251
|
requirements: []
|
252
252
|
rubyforge_project:
|
253
253
|
rubygems_version: 2.2.2
|
@@ -1,21 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module ActiveSpy
|
4
|
-
module Generators
|
5
|
-
class InstallGenerator < ::Rails::Generators::Base
|
6
|
-
desc 'Creates an active_spy gem configuration file at config/active_spy.yml, and an initializer at config/initializers/active_spy.rb'
|
7
|
-
|
8
|
-
def self.source_root
|
9
|
-
@_active_spy_source_root ||= File.expand_path("../templates", __FILE__)
|
10
|
-
end
|
11
|
-
|
12
|
-
def create_config_file
|
13
|
-
template 'active_spy.yml', File.join('config', 'active_spy.yml')
|
14
|
-
end
|
15
|
-
|
16
|
-
def create_initializer_file
|
17
|
-
template 'initializer.rb', File.join('config', 'initializers', 'active_spy.rb')
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|