active_record_streams 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +35 -0
- data/.gitignore +12 -0
- data/.rspec +5 -0
- data/.rubocop.yml +10 -0
- data/.travis.yml +12 -0
- data/CHANGELOG.md +13 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +96 -0
- data/MIT-LICENSE +20 -0
- data/README.md +168 -0
- data/Rakefile +8 -0
- data/active_record_streams.gemspec +55 -0
- data/lib/active_record_streams.rb +26 -0
- data/lib/active_record_streams/config.rb +15 -0
- data/lib/active_record_streams/config_spec.rb +32 -0
- data/lib/active_record_streams/credentials.rb +31 -0
- data/lib/active_record_streams/credentials_spec.rb +79 -0
- data/lib/active_record_streams/extensions/active_record/base.rb +41 -0
- data/lib/active_record_streams/extensions/active_record/base_spec.rb +45 -0
- data/lib/active_record_streams/extensions/active_record/persistence.rb +17 -0
- data/lib/active_record_streams/extensions/active_record/persistence_spec.rb +36 -0
- data/lib/active_record_streams/extensions/active_record/relation.rb +60 -0
- data/lib/active_record_streams/extensions/active_record/relation_spec.rb +41 -0
- data/lib/active_record_streams/message.rb +21 -0
- data/lib/active_record_streams/message_spec.rb +29 -0
- data/lib/active_record_streams/publishers/http_stream.rb +58 -0
- data/lib/active_record_streams/publishers/http_stream_spec.rb +73 -0
- data/lib/active_record_streams/publishers/kinesis_client.rb +26 -0
- data/lib/active_record_streams/publishers/kinesis_client_spec.rb +49 -0
- data/lib/active_record_streams/publishers/kinesis_stream.rb +64 -0
- data/lib/active_record_streams/publishers/kinesis_stream_spec.rb +93 -0
- data/lib/active_record_streams/publishers/sns_client.rb +25 -0
- data/lib/active_record_streams/publishers/sns_client_spec.rb +46 -0
- data/lib/active_record_streams/publishers/sns_stream.rb +52 -0
- data/lib/active_record_streams/publishers/sns_stream_spec.rb +89 -0
- data/lib/active_record_streams/version.rb +5 -0
- data/lib/active_record_streams/version_spec.rb +9 -0
- data/lib/active_record_streams_spec.rb +21 -0
- metadata +199 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 04ab246f568d123036bade4dac470b4043c56853
|
4
|
+
data.tar.gz: 5dec2a061686683d131d0d1363a63bc6b5e85e47
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 939dcae88607b746000741c4ada06c4e60c6e522a932b0b6f45353ae9183df7371fa2bdfafe5999db4b6b90ee097d42e5ca919fbfb253eb84e3d8cee4eae0c8f
|
7
|
+
data.tar.gz: d027a7cf85323ced248355420ee9a0bf3fb090e066ee6bf972fa65933361128831b215e14bd274c51be81b1a2673075cd9b14c3e63ed90a81dc4aa8ad739cbad
|
@@ -0,0 +1,35 @@
|
|
1
|
+
## Overview
|
2
|
+
|
3
|
+
Please include a brief summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. Please also add links to relevant tickets (if there are any).
|
4
|
+
|
5
|
+
Connects #XXX
|
6
|
+
|
7
|
+
### Type of change
|
8
|
+
|
9
|
+
Please delete options that are not relevant.
|
10
|
+
|
11
|
+
- Bug fix (non-breaking change which fixes an issue)
|
12
|
+
- New feature (non-breaking change which adds functionality)
|
13
|
+
- Breaking change (fix or feature that would cause existing functionality to not work as expected)
|
14
|
+
- Configuration changes
|
15
|
+
- Refactoring
|
16
|
+
- This change requires a documentation update
|
17
|
+
|
18
|
+
### Migrations
|
19
|
+
|
20
|
+
*Optional.* Add brief description of migration.
|
21
|
+
|
22
|
+
### Demo
|
23
|
+
|
24
|
+
*Optional.* Screenshots, `curl` examples, etc.
|
25
|
+
|
26
|
+
### Notes
|
27
|
+
|
28
|
+
*Optional.* Ancillary topics, caveats, alternative strategies that didn't work out, anything else.
|
29
|
+
|
30
|
+
## Testing instructions
|
31
|
+
|
32
|
+
- Describe how to test this PR
|
33
|
+
- Prefer bulleted description
|
34
|
+
- Include any setup required, such as bundling scripts, restarting services, etc.
|
35
|
+
- Include test case, and expected output
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
## [Unreleased]
|
8
|
+
|
9
|
+
## [0.1.0] - 2017-06-20
|
10
|
+
### Added
|
11
|
+
- Global hooks for ActiveRecord 4.2.10.
|
12
|
+
- Overrides to ActiveRecord::Base, ActiveRecord::Relation, ActiveRecord::Persistence to serve callbacks.
|
13
|
+
- SNS, Kinesis and HTTP stream types.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
active_record_streams (0.1.0)
|
5
|
+
activerecord (~> 4.2.10)
|
6
|
+
aws-sdk (~> 2.11.9)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (4.2.11.1)
|
12
|
+
activesupport (= 4.2.11.1)
|
13
|
+
builder (~> 3.1)
|
14
|
+
activerecord (4.2.11.1)
|
15
|
+
activemodel (= 4.2.11.1)
|
16
|
+
activesupport (= 4.2.11.1)
|
17
|
+
arel (~> 6.0)
|
18
|
+
activesupport (4.2.11.1)
|
19
|
+
i18n (~> 0.7)
|
20
|
+
minitest (~> 5.1)
|
21
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
22
|
+
tzinfo (~> 1.1)
|
23
|
+
arel (6.0.4)
|
24
|
+
ast (2.4.0)
|
25
|
+
aws-eventstream (1.0.2)
|
26
|
+
aws-sdk (2.11.258)
|
27
|
+
aws-sdk-resources (= 2.11.258)
|
28
|
+
aws-sdk-core (2.11.258)
|
29
|
+
aws-sigv4 (~> 1.0)
|
30
|
+
jmespath (~> 1.0)
|
31
|
+
aws-sdk-resources (2.11.258)
|
32
|
+
aws-sdk-core (= 2.11.258)
|
33
|
+
aws-sigv4 (1.1.0)
|
34
|
+
aws-eventstream (~> 1.0, >= 1.0.2)
|
35
|
+
builder (3.2.3)
|
36
|
+
coderay (1.1.2)
|
37
|
+
concurrent-ruby (1.1.5)
|
38
|
+
diff-lcs (1.3)
|
39
|
+
i18n (0.9.5)
|
40
|
+
concurrent-ruby (~> 1.0)
|
41
|
+
jaro_winkler (1.5.2)
|
42
|
+
jmespath (1.4.0)
|
43
|
+
method_source (0.9.2)
|
44
|
+
minitest (5.11.3)
|
45
|
+
parallel (1.17.0)
|
46
|
+
parser (2.6.2.1)
|
47
|
+
ast (~> 2.4.0)
|
48
|
+
pry (0.12.2)
|
49
|
+
coderay (~> 1.1.0)
|
50
|
+
method_source (~> 0.9.0)
|
51
|
+
psych (3.1.0)
|
52
|
+
rainbow (3.0.0)
|
53
|
+
rake (10.5.0)
|
54
|
+
rspec (3.8.0)
|
55
|
+
rspec-core (~> 3.8.0)
|
56
|
+
rspec-expectations (~> 3.8.0)
|
57
|
+
rspec-mocks (~> 3.8.0)
|
58
|
+
rspec-core (3.8.0)
|
59
|
+
rspec-support (~> 3.8.0)
|
60
|
+
rspec-expectations (3.8.2)
|
61
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
62
|
+
rspec-support (~> 3.8.0)
|
63
|
+
rspec-mocks (3.8.0)
|
64
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
65
|
+
rspec-support (~> 3.8.0)
|
66
|
+
rspec-support (3.8.0)
|
67
|
+
rubocop (0.67.2)
|
68
|
+
jaro_winkler (~> 1.5.1)
|
69
|
+
parallel (~> 1.10)
|
70
|
+
parser (>= 2.5, != 2.5.1.1)
|
71
|
+
psych (>= 3.1.0)
|
72
|
+
rainbow (>= 2.2.2, < 4.0)
|
73
|
+
ruby-progressbar (~> 1.7)
|
74
|
+
unicode-display_width (>= 1.4.0, < 1.6)
|
75
|
+
rubocop-performance (1.1.0)
|
76
|
+
rubocop (>= 0.67.0)
|
77
|
+
ruby-progressbar (1.10.0)
|
78
|
+
thread_safe (0.3.6)
|
79
|
+
tzinfo (1.2.5)
|
80
|
+
thread_safe (~> 0.1)
|
81
|
+
unicode-display_width (1.5.0)
|
82
|
+
|
83
|
+
PLATFORMS
|
84
|
+
ruby
|
85
|
+
|
86
|
+
DEPENDENCIES
|
87
|
+
active_record_streams!
|
88
|
+
bundler (~> 1.17)
|
89
|
+
pry (~> 0.12.2)
|
90
|
+
rake (~> 10.0)
|
91
|
+
rspec (~> 3.0)
|
92
|
+
rubocop (~> 0.67.2)
|
93
|
+
rubocop-performance (~> 1.1.0)
|
94
|
+
|
95
|
+
BUNDLED WITH
|
96
|
+
1.17.1
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019 Advanon AG (https://advanon.com)
|
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.md
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/Advanon/active_record_streams.svg?branch=master)](https://travis-ci.org/Advanon/active_record_streams)
|
2
|
+
|
3
|
+
# Active Record Streams
|
4
|
+
|
5
|
+
A small library to stream ActiveRecord's create/update/delete
|
6
|
+
events to AWS SNS topics, Kinesis streams or HTTP listeners.
|
7
|
+
|
8
|
+
## Version mappings
|
9
|
+
|
10
|
+
```
|
11
|
+
1.0.X - ActiveRecord 4.2.10
|
12
|
+
```
|
13
|
+
|
14
|
+
## Warning
|
15
|
+
|
16
|
+
Please, be aware that this library changes behaviour of such methods as
|
17
|
+
`update_all`, `delete_all`, `update_columns`, `update_column`
|
18
|
+
to start serving callbacks.
|
19
|
+
|
20
|
+
## How does it work
|
21
|
+
|
22
|
+
The library adds a global hook to the ActiveRecord's `after_commit`
|
23
|
+
events and streams these events to the specified targets (topics/streams/http).
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'active_record_streams'
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
|
35
|
+
$ bundle
|
36
|
+
|
37
|
+
Or install it yourself as:
|
38
|
+
|
39
|
+
$ gem install active_record_streams
|
40
|
+
|
41
|
+
## Usage
|
42
|
+
|
43
|
+
### Setting up for AWS
|
44
|
+
|
45
|
+
Skip this section if you are not going to use any of AWS targets.
|
46
|
+
|
47
|
+
If you already configured AWS sdk or you are running the code under
|
48
|
+
AWS-controlled environment with proper policies, ActiveRecordStreams
|
49
|
+
should start working without specifying any credentials.
|
50
|
+
|
51
|
+
If you didn't yet configure AWS globally or you want to specify separate
|
52
|
+
authentication details for streaming, use the following snippet:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
# config/initializers/active_record_streams.rb
|
56
|
+
|
57
|
+
require 'active_record_streams'
|
58
|
+
|
59
|
+
ActiveRecordStreams.configure do |config|
|
60
|
+
config.aws_region = 'eu-central-1'
|
61
|
+
config.aws_access_key_id = 'YOUR_ACCESS_KEY_ID'
|
62
|
+
config.aws_secret_access_key = 'YOUR_SECRET_ACCESS_KEY'
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
### Enabling streams
|
67
|
+
|
68
|
+
To start streaming events you just need to add them to configuration.
|
69
|
+
You may add as many streams as you need. By default, each stream
|
70
|
+
publishes events for all the tables, but you may change that by specifying
|
71
|
+
the `table_name` or `ignored_tables` option.
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
# config/initializers/active_record_streams.rb
|
75
|
+
|
76
|
+
require 'active_record_streams'
|
77
|
+
|
78
|
+
ActiveRecordStreams.configure do |config|
|
79
|
+
# Listen to all tables and publish events to the specified SNS topic
|
80
|
+
config.streams << ActiveRecordStreams::Publishers::SnsStream.new(
|
81
|
+
topic_arn: 'arn:aws:sns:...'
|
82
|
+
)
|
83
|
+
|
84
|
+
# Publish only events coming from `users` table to the specified SNS topic
|
85
|
+
config.streams << ActiveRecordStreams::Publishers::SnsStream.new(
|
86
|
+
table_name: 'users',
|
87
|
+
topic_arn: 'arn:aws:sns:...'
|
88
|
+
)
|
89
|
+
|
90
|
+
# Publish events for all the tables but `updates`
|
91
|
+
config.streams << ActiveRecordStreams::Publishers::SnsStream.new(
|
92
|
+
ignored_tables: %w[updates],
|
93
|
+
topic_arn: 'arn:aws:sns:...'
|
94
|
+
)
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
## Supported targets:
|
99
|
+
|
100
|
+
### ActiveRecordStreams::Publishers::SnsStream
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
config.streams << ActiveRecordStreams::Publishers::SnsStream.new(
|
104
|
+
topic_arn: String, # Required
|
105
|
+
table_name: String, # Optional
|
106
|
+
ignored_tables: Enumerable<String>, # Optional
|
107
|
+
|
108
|
+
# See: https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/SNS/Client.html#publish-instance_method
|
109
|
+
overrides: { ## Optional
|
110
|
+
target_arn: String, #
|
111
|
+
phone_number: String, #
|
112
|
+
subject: String, #
|
113
|
+
message_structure: String, #
|
114
|
+
message_attributes: { #
|
115
|
+
'String' => { #
|
116
|
+
data_type: String, #
|
117
|
+
string_value: String, #
|
118
|
+
binary_value: String ##
|
119
|
+
}
|
120
|
+
}
|
121
|
+
}
|
122
|
+
)
|
123
|
+
```
|
124
|
+
|
125
|
+
### ActiveRecordStreams::Publishers::KinesisStream
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
config.streams << ActiveRecordStreams::Publishers::KinesisStream.new(
|
129
|
+
stream_name: String, # Required
|
130
|
+
table_name: String, # Optional
|
131
|
+
ignored_tables: Enumerable<String>, # Optional
|
132
|
+
|
133
|
+
# See: https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/Kinesis/Client.html#put_record-instance_method
|
134
|
+
overrides: { ## Optional
|
135
|
+
explicit_hash_key: String, #
|
136
|
+
sequence_number_for_ordering: String, ##
|
137
|
+
}
|
138
|
+
)
|
139
|
+
```
|
140
|
+
|
141
|
+
### ActiveRecordStreams::Publishers::HttpStream
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
config.streams << ActiveRecordStreams::Publishers::HttpStream.new(
|
145
|
+
url: String, # Required
|
146
|
+
headers: Hash, # Optional
|
147
|
+
table_name: String, # Optional
|
148
|
+
ignored_tables: Enumerable<String> # Optional
|
149
|
+
)
|
150
|
+
```
|
151
|
+
|
152
|
+
## License
|
153
|
+
|
154
|
+
This software is licensed under the MIT license. See `MIT-LICENSE` for details.
|
155
|
+
|
156
|
+
## Development
|
157
|
+
|
158
|
+
1) Run `bundle install` to install dependencies
|
159
|
+
2) Use `rubocop` or `bundle exec rubocop` to run the Rubocop
|
160
|
+
3) Run `rspec` or `bundle exec rspec` to run the tests
|
161
|
+
4) Run `bundle exec rake install` to install the gem on a local machine
|
162
|
+
5) Do not forget to add your changes to the `CHANGELOG.md`
|
163
|
+
5) To release a new version update `version.rb`, `version_spec.rb` and run `gem build active_record_streams.gemspec` to release a new version
|
164
|
+
6) To push a new version to the Rubygems run `gem push <generated_gem_file.gem>`
|
165
|
+
|
166
|
+
## Contributing
|
167
|
+
|
168
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Advanon/active_record_streams.
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'active_record_streams/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'active_record_streams'
|
9
|
+
spec.version = ActiveRecordStreams::VERSION
|
10
|
+
spec.authors = ['Advanon Team']
|
11
|
+
spec.email = ['team@advanon.com']
|
12
|
+
|
13
|
+
spec.summary = <<-HEREDOC
|
14
|
+
Stream ActiveRecord events via HTTP, AWS SNS or Kinesis streams
|
15
|
+
HEREDOC
|
16
|
+
|
17
|
+
spec.description = <<-HEREDOC
|
18
|
+
Publish events about changes to your ActiveRecord models to different
|
19
|
+
targets, like HTTP endpoints, AWS SNS topics or Kinesis streams
|
20
|
+
HEREDOC
|
21
|
+
|
22
|
+
spec.homepage = 'https://advanon.com'
|
23
|
+
spec.required_ruby_version = '>= 2.3.3'
|
24
|
+
|
25
|
+
if spec.respond_to?(:metadata)
|
26
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
27
|
+
|
28
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
29
|
+
|
30
|
+
spec.metadata['source_code_uri'] =
|
31
|
+
'https://github.com/Advanon/active_record_streams'
|
32
|
+
|
33
|
+
spec.metadata['changelog_uri'] =
|
34
|
+
'https://github.com/Advanon/active_record_streams/blob/master/CHANGELOG.md'
|
35
|
+
else
|
36
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
37
|
+
'public gem pushes.'
|
38
|
+
end
|
39
|
+
|
40
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
41
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
|
42
|
+
end
|
43
|
+
|
44
|
+
spec.require_paths = ['lib']
|
45
|
+
|
46
|
+
spec.add_dependency 'activerecord', '~> 4.2.10'
|
47
|
+
spec.add_dependency 'aws-sdk', '~> 2.11.9'
|
48
|
+
|
49
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
50
|
+
spec.add_development_dependency 'pry', '~> 0.12.2'
|
51
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
52
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
53
|
+
spec.add_development_dependency 'rubocop', '~> 0.67.2'
|
54
|
+
spec.add_development_dependency 'rubocop-performance', '~> 1.1.0'
|
55
|
+
end
|