event_notifier 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c4bdaed6c6e9be6fed55844588f65789c25e9931758373311a7ef0121445246c
4
+ data.tar.gz: 2e4c112decef0357bd5f7f55db089912f5f1386d81167004a81aa85540ea59dc
5
+ SHA512:
6
+ metadata.gz: fcdd0bfb8ef55d5264517dcd1c7665a458582f8adcb579f77fc33e2f7a8d7fcb63535dca38f9d79827a9cdcb3d2ea24ce6d6cae36e9f787764b2f1fc8356669f
7
+ data.tar.gz: 9cb08e2f7330b5a97d27ec1265b41ac0011dc6c4b4c65e891918275471c2950e0d83ef80fe6dde206ab1dcbb7d8589f23ea3b1df7bf8ebb413757f9ceeaacd87
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Shahzad Tariq
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,54 @@
1
+ = EventNotifier
2
+
3
+ Listen for database events like create new record, updated existing or destroy & notify. These events can be used with ActionController::Live or Server Sent Events (SSE)
4
+
5
+ == Installation
6
+
7
+ Simply add event_notifier gem to your Gemfile.
8
+
9
+ gem 'event_notifier'
10
+
11
+ & from terminal, run:
12
+
13
+ bundle install
14
+
15
+
16
+ == Usage
17
+
18
+ class Account < ActiveRecord::Base
19
+ include EventNotifier
20
+
21
+ .....
22
+
23
+ end
24
+
25
+
26
+ and inside your controller / SSE or wherever you want to use database events,
27
+
28
+ class AccountsController < ApplicationController
29
+
30
+ def sse_live
31
+ Account.on_event do |_event|
32
+ sse.write(_event) # or whatever you want to do when a new account is created or existing account object is updated or destroyed
33
+ end
34
+ end
35
+ end
36
+
37
+
38
+ And now you are ready to track database event notifications.
39
+
40
+ p.s. Currently only create/update & destory events are handled
41
+
42
+
43
+ == TODO
44
+
45
+ * Configuration options for events to handle
46
+ * MongoDB support
47
+
48
+ == Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
@@ -0,0 +1,3 @@
1
+ module EventNotifier
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EventNotifier
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ after_save :notify_event
8
+ after_destroy :notify_event
9
+
10
+ def notify_event
11
+ self.class.connection.execute "NOTIFY #{self.class.table_name}, 'SSE'"
12
+ end
13
+
14
+ def self.on_event
15
+ connection.execute "LISTEN #{table_name}"
16
+ loop do
17
+ break if Rails.env.test?
18
+
19
+ connection.raw_connection.wait_for_notify do |_event, _pid, data|
20
+ yield data
21
+ end
22
+ sleep 5
23
+ end
24
+ ensure
25
+ connection.execute "UNLISTEN #{table_name}"
26
+ end
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: event_notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Shahzad Tariq
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ description: Listen for database events like create new record, updated existing or
28
+ destroy & notify. These events can be used with ActionController::Live or Server
29
+ Sent Events (SSE)
30
+ email:
31
+ - m.shahzad.tariq@hotmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - MIT-LICENSE
37
+ - README.rdoc
38
+ - lib/event_notifier.rb
39
+ - lib/event_notifier/VERSION.rb
40
+ homepage: http://github.com/mshahzadtariq/event_notifier
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.3.3
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Listen for database events like create new record, updated existing or destroy
63
+ & notify.
64
+ test_files: []