utter_events 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a7f9364791c5599be45a3537b0ec6fdcb4070628
4
- data.tar.gz: 8acd1e350b764ca069915b659fa4f23d3b371f7a
3
+ metadata.gz: fb46bae6c354ecba25cfa0706148df5dd12070a6
4
+ data.tar.gz: 2c62baca84bb188665a375c76817642760ea5117
5
5
  SHA512:
6
- metadata.gz: dc4044d8813310cf6653e1d39702c8260f37b1c502f50624990f8c15a3070c5a8593cd9796aa33a17382e0e8a0f00b2b5319b6b13760c11648de333295a6c4b0
7
- data.tar.gz: 0617c8f3578bf306b4c0e9929edea1371095f99fc2d3b20e6e89b6bf920381f9a10b1d246f6a94d4f9831f31b5b3215aabd77eb33aa4c7fea2f8600480cdc8c4
6
+ metadata.gz: 917cd2594cfbfd4b2c9b154232e72fedb3595ee345d03054fe9a9bbaef428220e7766283328610da177d4b008388c2b584b285f57a9c8a91bfafdfcecb0988d4
7
+ data.tar.gz: 497277157be74cd61f7acbfeb4f4e47355c2901d81c9c6775c0454fb58ae67e6d7126f7cc25d7adf2ceefb095a79cc871f268497639452d40838eb0f5d822a3c
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .byebug*
data/README.md CHANGED
@@ -98,6 +98,26 @@ end
98
98
 
99
99
  Note that this doesn't work on events that are called from class methods; you will need to observe the Global Events Table object in those cases.
100
100
 
101
+ ## Examples
102
+
103
+ There is an example [Rails App](https://github.com/parasquid/utter/tree/master/sample_rails_app) that uses `Utter`
104
+
105
+ $ # after checking out this repository
106
+ $ cd sample_rails_app
107
+ $ bundle install
108
+ $ rails s
109
+
110
+ Then go to localhost:3000/articles and take a look at the console. You should see something like this:
111
+
112
+ ```ruby
113
+ object_id: 70100155242700
114
+ event: index_viewed
115
+ payload: {:params=>{"controller"=>"articles", "action"=>"index"}}
116
+ ```
117
+ You will need to [setup your AWS credentials](https://blogs.aws.amazon.com/security/post/Tx3D6U6WSFGOK2H/A-New-and-Standardized-Way-to-Manage-Credentials-in-the-AWS-SDKs) if you wish to be able to use the AWS related examples.
118
+
119
+ The `UtterDynamoLogger` watcher utilizes [Celluloid](https://github.com/celluloid/celluloid/wiki/Basic-usage) in order to not block the original method call.
120
+
101
121
  ## Development
102
122
 
103
123
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -17,10 +17,16 @@ module Utter
17
17
  end
18
18
 
19
19
  def process_event(object_id, event, payload)
20
+ return unless @backing_hash.has_key? object_id
21
+ return unless @backing_hash[object_id].has_key? event
22
+
23
+ # call registered event handlers
20
24
  @backing_hash[object_id][event].compact!
21
25
  @backing_hash[object_id][event].each do |block|
22
26
  block.call(payload)
23
27
  end
28
+
29
+ # notify watchers
24
30
  changed
25
31
  notify_observers(object_id, event, payload)
26
32
  end
@@ -0,0 +1,45 @@
1
+ module Utter
2
+ module Utils
3
+ class Wrapper
4
+ def wrap(wrapped_class, before: nil, after: nil)
5
+ wrapped_class.class_eval do
6
+ self.define_singleton_method :utter_before_action do
7
+ before || Proc.new {}
8
+ end
9
+
10
+ self.define_singleton_method :utter_after_action do
11
+ after || Proc.new {}
12
+ end
13
+
14
+ def self.inherited(klass)
15
+ def klass.method_added(name)
16
+ # prevent a SystemStackError
17
+ return if @_not_new
18
+ @_not_new = true
19
+
20
+ # preserve the original method call
21
+ original = "original #{name}"
22
+ alias_method original, name
23
+
24
+ # wrap the method call
25
+ define_method(name) do |*args, &block|
26
+ self.class.superclass.utter_before_action.call(self, args, name)
27
+
28
+ # call the original method
29
+ result = send original, *args, &block
30
+
31
+ self.class.superclass.utter_after_action.call(self, args, name)
32
+
33
+ # return the original return value
34
+ result
35
+ end
36
+
37
+ # reset the guard for the next method definition
38
+ @_not_new = false
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  module Utter
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -7,6 +7,7 @@ gem 'uglifier', '>= 1.3.0'
7
7
  gem 'jquery-rails'
8
8
  gem 'jbuilder', '~> 2.0'
9
9
  gem 'sdoc', '~> 0.4.0', group: :doc
10
+ gem "celluloid", require: "celluloid/current"
10
11
 
11
12
  group :development, :test do
12
13
  gem 'byebug'
@@ -14,4 +15,6 @@ group :development, :test do
14
15
  gem "quiet_assets"
15
16
  end
16
17
 
17
- gem "utter", path: "../"
18
+ gem "utter_events", path: "../", require: "utter"
19
+
20
+ gem "aws-sdk", "~> 2"
@@ -1,8 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- utter (0.1.0)
5
- gem_config (= 0.3.1)
4
+ utter_events (0.2.0)
6
5
 
7
6
  GEM
8
7
  remote: https://rubygems.org/
@@ -43,21 +42,45 @@ GEM
43
42
  thread_safe (~> 0.3, >= 0.3.4)
44
43
  tzinfo (~> 1.1)
45
44
  arel (6.0.3)
45
+ aws-sdk (2.2.22)
46
+ aws-sdk-resources (= 2.2.22)
47
+ aws-sdk-core (2.2.22)
48
+ jmespath (~> 1.0)
49
+ aws-sdk-resources (2.2.22)
50
+ aws-sdk-core (= 2.2.22)
46
51
  binding_of_caller (0.7.2)
47
52
  debug_inspector (>= 0.0.1)
48
53
  builder (3.2.2)
49
54
  byebug (8.2.2)
55
+ celluloid (0.17.3)
56
+ celluloid-essentials
57
+ celluloid-extras
58
+ celluloid-fsm
59
+ celluloid-pool
60
+ celluloid-supervision
61
+ timers (>= 4.1.1)
62
+ celluloid-essentials (0.20.5)
63
+ timers (>= 4.1.1)
64
+ celluloid-extras (0.20.5)
65
+ timers (>= 4.1.1)
66
+ celluloid-fsm (0.20.5)
67
+ timers (>= 4.1.1)
68
+ celluloid-pool (0.20.5)
69
+ timers (>= 4.1.1)
70
+ celluloid-supervision (0.20.5)
71
+ timers (>= 4.1.1)
50
72
  concurrent-ruby (1.0.0)
51
73
  debug_inspector (0.0.2)
52
74
  erubis (2.7.0)
53
75
  execjs (2.6.0)
54
- gem_config (0.3.1)
55
76
  globalid (0.3.6)
56
77
  activesupport (>= 4.1.0)
78
+ hitimes (1.2.3)
57
79
  i18n (0.7.0)
58
80
  jbuilder (2.4.1)
59
81
  activesupport (>= 3.0.0, < 5.1)
60
82
  multi_json (~> 1.2)
83
+ jmespath (1.1.3)
61
84
  jquery-rails (4.1.0)
62
85
  rails-dom-testing (~> 1.0)
63
86
  railties (>= 4.2.0)
@@ -126,6 +149,8 @@ GEM
126
149
  thor (0.19.1)
127
150
  thread_safe (0.3.5)
128
151
  tilt (2.0.2)
152
+ timers (4.1.1)
153
+ hitimes
129
154
  tzinfo (1.2.2)
130
155
  thread_safe (~> 0.1)
131
156
  uglifier (2.7.2)
@@ -141,7 +166,9 @@ PLATFORMS
141
166
  ruby
142
167
 
143
168
  DEPENDENCIES
169
+ aws-sdk (~> 2)
144
170
  byebug
171
+ celluloid
145
172
  jbuilder (~> 2.0)
146
173
  jquery-rails
147
174
  quiet_assets
@@ -150,7 +177,7 @@ DEPENDENCIES
150
177
  sdoc (~> 0.4.0)
151
178
  sqlite3
152
179
  uglifier (>= 1.3.0)
153
- utter!
180
+ utter_events!
154
181
  web-console (~> 2.0)
155
182
 
156
183
  BUNDLED WITH
@@ -1,8 +1,79 @@
1
+
2
+ require "pp"
3
+
1
4
  class UtterConsoleLogger
2
5
  def update(object_id, event, payload)
3
6
  puts "object_id: #{object_id}\nevent: #{event}\npayload: #{payload}"
4
7
  end
5
8
  end
6
9
 
7
- watcher = UtterConsoleLogger.new
8
- Utter::GLOBAL_EVENTS_TABLE.add_observer(watcher)
10
+ class UtterKinesisLogger
11
+ def initialize
12
+ credentials = Aws::SharedCredentials.new(profile_name: "kinesis")
13
+ @client = Aws::Firehose::Client.new(credentials: credentials, region: "us-east-1")
14
+ end
15
+
16
+ def update(object_id, event, payload)
17
+ self.async._update(object_id, event, payload)
18
+ end
19
+
20
+ def _update(object_id, event, payload)
21
+ @client.put_record(
22
+ delivery_stream_name: "checkout",
23
+ record: {
24
+ data: payload.to_s
25
+ }
26
+ )
27
+ end
28
+ end
29
+
30
+ class UtterDynamoLogger
31
+ include Celluloid
32
+
33
+ def initialize
34
+ credentials = Aws::SharedCredentials.new(profile_name: "dynamodb")
35
+ @client = Aws::DynamoDB::Client.new(
36
+ credentials: credentials,
37
+ region: "us-east-1"
38
+ )
39
+ end
40
+
41
+ def update(object_id, event, payload)
42
+ self.async._update(object_id, event, payload)
43
+ end
44
+
45
+ def _update(object_id, event, payload)
46
+ @client.put_item({
47
+ table_name: "mv_utter_events",
48
+ item: {
49
+ event_id: SecureRandom.uuid,
50
+ object_id: object_id,
51
+ event: event,
52
+ payload: payload
53
+ }
54
+ })
55
+ end
56
+ end
57
+
58
+ require "utter/utils/wrapper"
59
+
60
+ [ApplicationController].each do |klass|
61
+ after_action = Proc.new do |context, args, name|
62
+ payload = {
63
+ "object_instance" => context.to_s,
64
+ "action_dispatch.request.path_parameters" => context.env["action_dispatch.request.path_parameters"]
65
+ }
66
+ context.utter("#{context.class.to_s}##{name}", payload) if context.respond_to?(:utter)
67
+ end
68
+ Utter::Utils::Wrapper.new.wrap(klass, after: after_action )
69
+ end
70
+
71
+
72
+
73
+
74
+ console_watcher = UtterConsoleLogger.new
75
+ dynamo_watcher = UtterDynamoLogger.new
76
+ # kinesis_watcher = UtterKinesisLogger.new
77
+ Utter::GLOBAL_EVENTS_TABLE.add_observer(console_watcher)
78
+ Utter::GLOBAL_EVENTS_TABLE.add_observer(dynamo_watcher)
79
+ # Utter::GLOBAL_EVENTS_TABLE.add_observer(kinesis_watcher)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: utter_events
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - parasquid
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-23 00:00:00.000000000 Z
11
+ date: 2016-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -116,6 +116,7 @@ files:
116
116
  - bin/console
117
117
  - bin/setup
118
118
  - lib/utter.rb
119
+ - lib/utter/utils/wrapper.rb
119
120
  - lib/utter/version.rb
120
121
  - sample_rails_app/.gitignore
121
122
  - sample_rails_app/Gemfile