active_spy 1.4.4 → 2.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,77 @@
1
+ module ActiveSpy
2
+ module Agent
3
+ extend ActiveSupport::Concern
4
+ include Station
5
+
6
+ included do
7
+ after_create :broadcast_create
8
+ after_update :broadcast_update
9
+ after_destroy :broadcast_destroy
10
+ end
11
+
12
+ def broadcast_create
13
+ broadcast_event('create')
14
+ end
15
+
16
+ def broadcast_update
17
+ broadcast_event('update')
18
+ end
19
+
20
+ def broadcast_destroy
21
+ broadcast_event('destroy')
22
+ end
23
+
24
+ def broadcast_event(action)
25
+ # Rails.logger.debug("[SPY] - Broadcasting event #{self.class.name.underscore}##{action}")
26
+ params = event_params(action)
27
+ # Rails.logger.debug("[SPY] - Event #{self.class.name.underscore}##{action} params: #{params.inspect}")
28
+ self.class.report!(params)
29
+ # Rails.logger.debug("[SPY] - Event #{self.class.name.underscore}##{action} sent!")
30
+ end
31
+
32
+ def event_params(action)
33
+ {
34
+ type: self.class.name.underscore,
35
+ actor: payload_for_actor,
36
+ payload: payload_for(action),
37
+ action: action
38
+ }
39
+ end
40
+
41
+ def payload_for(action)
42
+ { self.class.name.underscore => attributes }
43
+ end
44
+
45
+ def payload_for_actor
46
+ actor = ActiveSpy::Agent.current_actor
47
+
48
+ if actor
49
+ actor.respond_to?(:to_actor) ? actor.to_actor : actor.attributes
50
+ end
51
+ end
52
+
53
+ def self.current_actor
54
+ RequestStore.store[:current_actor]
55
+ end
56
+
57
+ def self.current_actor=(actor)
58
+ RequestStore.store[:current_actor] = actor
59
+ end
60
+
61
+ module ClassMethods
62
+ def report!(event)
63
+ # Rails.logger.debug("[SPY] - Publishing event to SNS Topic #{sns_topic.topic_arn}")
64
+ sns_client.publish({
65
+ topic_arn: sns_topic.topic_arn,
66
+ message: event.to_json
67
+ })
68
+ end
69
+
70
+ private
71
+
72
+ def delete_sns_topic
73
+ sns_client.delete_topic(topic_arn: sns_topic.topic_arn)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,124 @@
1
+ module ActiveSpy
2
+ module Handler
3
+ extend ActiveSupport::Concern
4
+ include Station
5
+
6
+ included do
7
+ include Shoryuken::Worker
8
+
9
+ ensure_sqs_queue_subscription!
10
+
11
+ shoryuken_options queue: sqs_queue_name, auto_delete: true,
12
+ body_parser: :json
13
+ end
14
+
15
+ def perform(sqs_message, body)
16
+ type = body.delete('type')
17
+ action = body.delete('action')
18
+ payload = body.delete('payload')[type]
19
+ actor = body.delete('actor')
20
+
21
+ class_name = self.class.name
22
+ # Rails.logger.debug("[Interceptor][#{class_name}] - Receiving Message##{sqs_message.message_id}")
23
+ # Rails.logger.debug("[Interceptor][#{class_name}] - Message##{sqs_message.message_id} type: #{type}")
24
+ # Rails.logger.debug("[Interceptor][#{class_name}] - Message##{sqs_message.message_id} action: #{action}")
25
+ # Rails.logger.debug("[Interceptor][#{class_name}] - Message##{sqs_message.message_id} actor: #{actor.inspect}")
26
+ # Rails.logger.debug("[Interceptor][#{class_name}] - Message##{sqs_message.message_id} payload: #{payload.inspect}")
27
+ send(action, type, payload, actor)
28
+ # Rails.logger.debug("[Interceptor][#{class_name}] - Message##{sqs_message.message_id} Received!")
29
+ end
30
+
31
+ def create(type, payload, actor); end
32
+
33
+ def update(type, payload, actor); end
34
+
35
+ def destroy(type, payload, actor); end
36
+
37
+ module ClassMethods
38
+ private
39
+
40
+ def sqs_queue_name
41
+ @sqs_queue_name ||= "#{dasherized_name}-#{ActiveSpy.options[:app_env]}"
42
+ end
43
+
44
+ def sqs_queue
45
+ @sqs_queue ||= sqs_client.create_queue({
46
+ queue_name: sqs_queue_name,
47
+ attributes: {
48
+ 'DelaySeconds' => '0',
49
+ 'MaximumMessageSize' => '262144',
50
+ 'MessageRetentionPeriod' => '345600',
51
+ 'ReceiveMessageWaitTimeSeconds' => '0',
52
+ 'VisibilityTimeout' => '60'
53
+ }
54
+ })
55
+ end
56
+
57
+ def sqs_queue_arn
58
+ @sqs_queue_arn ||= sqs_client.get_queue_attributes({
59
+ queue_url: sqs_queue.queue_url,
60
+ attribute_names: ['QueueArn']
61
+ }).attributes['QueueArn']
62
+ end
63
+
64
+ def sns_topic_name
65
+ @sns_topic_name ||=
66
+ "#{dasherized_name.gsub('-handler', '')}-#{ActiveSpy.options[:app_env]}"
67
+ end
68
+
69
+ def ensure_sqs_queue_subscription!
70
+ # Rails.logger.debug("[Interceptor] - Configuring Queue's Policy to accept SNS messages")
71
+ sqs_client.set_queue_attributes({
72
+ queue_url: sqs_queue.queue_url,
73
+ attributes: {
74
+ 'Policy' => sqs_queue_policy.gsub("\n", '').gsub(/ +/, '')
75
+ }
76
+ })
77
+
78
+ # Rails.logger.debug("[Interceptor] - Subscribing SNS Queue #{sqs_queue_name} to SNS Topic #{sns_topic.topic_arn}")
79
+ subscription = sns_client.subscribe({
80
+ topic_arn: sns_topic.topic_arn,
81
+ protocol: 'sqs',
82
+ endpoint: sqs_queue_arn
83
+ })
84
+
85
+ # Rails.logger.debug("[Interceptor] - Updating Subscription with arn #{subscription.subscription_arn} for SNS Queue #{sqs_queue_name}")
86
+ sns_client.set_subscription_attributes({
87
+ subscription_arn: subscription.subscription_arn,
88
+ attribute_name: 'RawMessageDelivery',
89
+ attribute_value: 'true',
90
+ })
91
+
92
+ # Rails.logger.debug("[Interceptor] - Subscription for SNS Queue #{sqs_queue_name} Ready!")
93
+ end
94
+
95
+ def sqs_queue_policy
96
+ %(
97
+ {
98
+ "Version": "2012-10-17",
99
+ "Id": "#{sqs_queue_arn}/SQSDefaultPolicy",
100
+ "Statement": [
101
+ {
102
+ "Effect": "Allow",
103
+ "Principal": {
104
+ "AWS": "*"
105
+ },
106
+ "Action": "SQS:SendMessage",
107
+ "Resource": "#{sqs_queue_arn}",
108
+ "Condition": {
109
+ "ArnEquals": {
110
+ "aws:SourceArn": "#{sns_topic.topic_arn}"
111
+ }
112
+ }
113
+ }
114
+ ]
115
+ }
116
+ ).gsub("\n", '').gsub(/ +/, '')
117
+ end
118
+
119
+ def delete_sqs_queue
120
+ sqs_client.delete_queue(queue_url: sqs_queue.queue_url)
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,56 @@
1
+ require 'nullobject'
2
+
3
+ module ActiveSpy
4
+ module Station
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ private
9
+
10
+ def sns_client
11
+ if ActiveSpy.options[:fake_clients]
12
+ Null::Object.instance
13
+ else
14
+ @sns_client ||= Aws::SNS::Client.new
15
+ end
16
+ end
17
+
18
+ def sqs_client
19
+ if ActiveSpy.options[:fake_clients]
20
+ Null::Object.instance
21
+ else
22
+ @sqs_client ||= Aws::SQS::Client.new
23
+ end
24
+ end
25
+
26
+ def find_sns_topic(name)
27
+ sns_client.list_topics.topics.each do |topic|
28
+ return topic if name == topic.topic_arn.split(':').last
29
+ end
30
+
31
+ return nil
32
+ end
33
+
34
+ def create_sns_topic(name)
35
+ sns_client.create_topic(name: name)
36
+ end
37
+
38
+ def app_name
39
+ @app_name ||= ActiveSpy.options[:app_name].underscore.dasherize
40
+ end
41
+
42
+ def sns_topic
43
+ @sns_topic ||= find_sns_topic(sns_topic_name) || create_sns_topic(sns_topic_name)
44
+ end
45
+
46
+ def sns_topic_name
47
+ @sns_topic_name ||= "#{app_name}-#{dasherized_name}-#{ActiveSpy.options[:app_env]}"
48
+ end
49
+
50
+ def dasherized_name
51
+ @dasherized_name ||= name.underscore.dasherize
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,10 @@
1
+ module ActiveSpy
2
+ module Version
3
+ MAJOR = 2
4
+ MINOR = 0
5
+ PATCH = 0
6
+ BUILD = 'pre1'
7
+
8
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
9
+ end
10
+ 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.4
4
+ version: 2.0.0.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Douglas Camata
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-02 00:00:00.000000000 Z
11
+ date: 2015-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -25,49 +25,49 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.0.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: rest-client
28
+ name: shoryuken
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 1.8.0
33
+ version: 1.0.0
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
- version: 1.8.0
40
+ version: 1.0.0
41
41
  - !ruby/object:Gem::Dependency
42
- name: hashie
42
+ name: nullobject
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 0.0.5
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 0.0.5
55
55
  - !ruby/object:Gem::Dependency
56
- name: activerecord
56
+ name: request_store
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: 4.0.0
62
- type: :development
61
+ version: 1.2.0
62
+ type: :runtime
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.0.0
68
+ version: 1.2.0
69
69
  - !ruby/object:Gem::Dependency
70
- name: rails
70
+ name: activerecord
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -95,91 +95,91 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: 0.24.0
97
97
  - !ruby/object:Gem::Dependency
98
- name: pry
98
+ name: bundler
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: 0.10.0
103
+ version: 1.9.7
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: 0.10.0
110
+ version: 1.9.7
111
111
  - !ruby/object:Gem::Dependency
112
- name: rspec
112
+ name: jeweler
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: 3.0.0
117
+ version: 2.0.1
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: 3.0.0
124
+ version: 2.0.1
125
125
  - !ruby/object:Gem::Dependency
126
- name: yard
126
+ name: simplecov
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - '='
129
+ - - ">="
130
130
  - !ruby/object:Gem::Version
131
- version: 0.8.7.4
131
+ version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - '='
136
+ - - ">="
137
137
  - !ruby/object:Gem::Version
138
- version: 0.8.7.4
138
+ version: '0'
139
139
  - !ruby/object:Gem::Dependency
140
- name: rdoc
140
+ name: sqlite3
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
- - - "~>"
143
+ - - ">="
144
144
  - !ruby/object:Gem::Version
145
- version: 4.1.1
145
+ version: '0'
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
- - - "~>"
150
+ - - ">="
151
151
  - !ruby/object:Gem::Version
152
- version: 4.1.1
152
+ version: '0'
153
153
  - !ruby/object:Gem::Dependency
154
- name: bundler
154
+ name: rake
155
155
  requirement: !ruby/object:Gem::Requirement
156
156
  requirements:
157
- - - "~>"
157
+ - - ">="
158
158
  - !ruby/object:Gem::Version
159
- version: 1.9.7
159
+ version: '0'
160
160
  type: :development
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
- - - "~>"
164
+ - - ">="
165
165
  - !ruby/object:Gem::Version
166
- version: 1.9.7
166
+ version: '0'
167
167
  - !ruby/object:Gem::Dependency
168
- name: jeweler
168
+ name: pry-byebug
169
169
  requirement: !ruby/object:Gem::Requirement
170
170
  requirements:
171
- - - "~>"
171
+ - - ">="
172
172
  - !ruby/object:Gem::Version
173
- version: 2.0.1
173
+ version: '0'
174
174
  type: :development
175
175
  prerelease: false
176
176
  version_requirements: !ruby/object:Gem::Requirement
177
177
  requirements:
178
- - - "~>"
178
+ - - ">="
179
179
  - !ruby/object:Gem::Version
180
- version: 2.0.1
180
+ version: '0'
181
181
  - !ruby/object:Gem::Dependency
182
- name: simplecov
182
+ name: dotenv
183
183
  requirement: !ruby/object:Gem::Requirement
184
184
  requirements:
185
185
  - - ">="
@@ -211,23 +211,14 @@ files:
211
211
  - LICENSE.txt
212
212
  - README.md
213
213
  - Rakefile
214
- - VERSION
215
214
  - active_spy.gemspec
216
- - app/controllers/active_spy/notifications_controller.rb
217
215
  - config/initializers/active_spy_configuration_loader.rb
218
216
  - config/routes.rb
219
217
  - lib/active_spy.rb
220
- - lib/active_spy/base.rb
221
- - lib/active_spy/configuration.rb
222
- - lib/active_spy/rails/base.rb
223
- - lib/active_spy/rails/engine.rb
224
- - lib/active_spy/rails/hook_list.rb
225
- - lib/active_spy/rails/listener.rb
226
- - lib/active_spy/rails/railtie.rb
227
- - lib/active_spy/rails/spy.rb
228
- - lib/active_spy/rails/validation.rb
229
- - lib/active_spy/spy/spy.rb
230
- - lib/active_spy/spy/spy_list.rb
218
+ - lib/active_spy/agent.rb
219
+ - lib/active_spy/handler.rb
220
+ - lib/active_spy/station.rb
221
+ - lib/active_spy/version.rb
231
222
  - lib/rails/generators/active_spy/install/install_generator.rb
232
223
  - lib/rails/generators/active_spy/install/templates/active_spy.yml
233
224
  - lib/rails/generators/active_spy/install/templates/initializer.rb
@@ -246,9 +237,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
246
237
  version: '0'
247
238
  required_rubygems_version: !ruby/object:Gem::Requirement
248
239
  requirements:
249
- - - ">="
240
+ - - ">"
250
241
  - !ruby/object:Gem::Version
251
- version: '0'
242
+ version: 1.3.1
252
243
  requirements: []
253
244
  rubyforge_project:
254
245
  rubygems_version: 2.2.2