aws-broker 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -22
- data/lib/aws-broker.rb +5 -2
- data/lib/aws/broker/matchers.rb +27 -0
- data/lib/aws/broker/publishing.rb +26 -0
- data/lib/aws/broker/railtie.rb +15 -0
- data/lib/shoryuken/subscriber.rb +43 -0
- metadata +8 -5
- data/lib/aws/broker/constants.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15d27e2ba58c931cc347494c6420b762666dcc60
|
4
|
+
data.tar.gz: 873896f0dd399e8e41f232cbcf23c3d757223ddb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df5fb8fcf7e36951e8d3b8737c66f2fa1672f292c699eda096e88edc7aff1e57acae9fa7c89ab9057f9e129737e81b28e2924025bde0b6e55b3c0b046235b915
|
7
|
+
data.tar.gz: 7513b2f5159cf72adff7a05de25a502f7f0255ee1c17c35e143ea17b4ba45cf31061e7bb8d73cf0e25f5d8ef8cd513684b3d0c2921fc94fd50b8005f4de1dae6
|
data/README.md
CHANGED
@@ -16,7 +16,7 @@ Message processing is not part of this gem - we recommend using
|
|
16
16
|
|
17
17
|
gem 'aws-broker'
|
18
18
|
|
19
|
-
### Usage
|
19
|
+
### Basic Usage
|
20
20
|
|
21
21
|
Broker = Aws::Broker
|
22
22
|
topic = 'topic'
|
@@ -33,37 +33,52 @@ Message processing is not part of this gem - we recommend using
|
|
33
33
|
# publish message to topic
|
34
34
|
Broker.publish(topic, message)
|
35
35
|
|
36
|
-
###
|
36
|
+
### Rails Usage
|
37
37
|
|
38
|
-
|
38
|
+
Though AWS Broker does not require Rails, an ActiveRecord extension and
|
39
|
+
accompanying RSpec matcher is provided if the project is Rails. This simplifies
|
40
|
+
the process of publishing events for ActiveRecord create, update, and destroy
|
41
|
+
callbacks.
|
39
42
|
|
40
|
-
|
43
|
+
## Publishing
|
41
44
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
+
# app/models/user.rb
|
46
|
+
class User < ActiveRecord::Base
|
47
|
+
publish_event :create, :update, :destroy
|
45
48
|
end
|
46
49
|
|
47
|
-
|
50
|
+
## Subscribing
|
48
51
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
| `queue_prefix` | nil | prefix for default queue name (prefix-topic) |
|
52
|
+
# app/subscribers/user_subscriber.rb
|
53
|
+
class UserSubscriber < Shoryuken::Subscriber
|
54
|
+
self.resource_class = 'User'
|
53
55
|
|
54
|
-
|
56
|
+
def create; ... end
|
55
57
|
|
56
|
-
|
57
|
-
the AWS SDK. See
|
58
|
-
[Configuring the AWS SDK for Ruby](http://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html)
|
59
|
-
for details.
|
58
|
+
def update; ... end
|
60
59
|
|
61
|
-
|
60
|
+
def destroy; ... end
|
61
|
+
end
|
62
|
+
|
63
|
+
## Testing
|
64
|
+
|
65
|
+
# spec/rails_helper.rb
|
66
|
+
require 'aws/broker/matchers'
|
67
|
+
|
68
|
+
# spec/models/user_spec.rb
|
69
|
+
describe User do
|
70
|
+
context 'publish' do
|
71
|
+
subject { User }
|
72
|
+
it { should publish_event(:create) }
|
73
|
+
it { should publish_event(:update) }
|
74
|
+
it { should publish_event(:destroy) }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
### Usage Details
|
62
79
|
|
63
|
-
|
64
|
-
|
65
|
-
* IAM
|
66
|
-
* `Aws.config[:credentials]`
|
80
|
+
See the [wiki](https://github.com/Thanx/aws-broker/wiki) for an in-depth
|
81
|
+
overview of usage and configuration options.
|
67
82
|
|
68
83
|
### Inspiration
|
69
84
|
|
data/lib/aws-broker.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
RSpec::Matchers.define :publish_event do |event|
|
2
|
+
match do |klass|
|
3
|
+
filter = :"publish_event_#{event}"
|
4
|
+
# find callback
|
5
|
+
callback = klass._commit_callbacks.detect { |c| c.filter == filter }
|
6
|
+
# assert existence
|
7
|
+
expect(callback).to be
|
8
|
+
# assert after_commit
|
9
|
+
expect(callback.kind).to eq(:after)
|
10
|
+
# assert after commit event type
|
11
|
+
expect(callback.instance_variable_get(:@if)[0]).to include("[:#{event}]")
|
12
|
+
# assert broker call is triggered
|
13
|
+
object = klass.new
|
14
|
+
allow(object).to receive(:id) { 0 }
|
15
|
+
expect(Broker).to receive(:publish).with(
|
16
|
+
klass.to_s.underscore.tr('/','_'),
|
17
|
+
event: event,
|
18
|
+
id: 0
|
19
|
+
)
|
20
|
+
object.send(filter)
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
failure_message do |klass|
|
25
|
+
"expected #{klass.to_s} to publish_event #{event}"
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Aws
|
2
|
+
class Broker
|
3
|
+
module Publishing
|
4
|
+
|
5
|
+
# only :create, :update, :destroy
|
6
|
+
def publish_event(*events)
|
7
|
+
events.each do |event|
|
8
|
+
event = event.to_sym
|
9
|
+
unless [:create, :update, :destroy].include?(event)
|
10
|
+
raise "Invalid publish event #{event}"
|
11
|
+
end
|
12
|
+
method_name = :"publish_event_#{event}"
|
13
|
+
define_method method_name do
|
14
|
+
Broker.publish(
|
15
|
+
self.class.to_s.underscore.tr('/', '_'),
|
16
|
+
event: event,
|
17
|
+
id: id
|
18
|
+
)
|
19
|
+
end
|
20
|
+
after_commit method_name, on: event
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'aws/broker/publishing'
|
2
|
+
|
3
|
+
module Aws
|
4
|
+
class Broker
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
|
7
|
+
initializer 'aws-broker.extend_active_record' do
|
8
|
+
ActiveSupport.on_load(:active_record) do
|
9
|
+
ActiveRecord::Base.send(:extend, Aws::Broker::Publishing)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Shoryuken
|
2
|
+
class Subscriber
|
3
|
+
include Shoryuken::Worker
|
4
|
+
|
5
|
+
class << self
|
6
|
+
attr_reader :resource_class
|
7
|
+
|
8
|
+
def queue=(queue)
|
9
|
+
shoryuken_options(
|
10
|
+
queue: queue,
|
11
|
+
auto_delete: true,
|
12
|
+
body_parser: :json
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
def resource_class=(klass)
|
17
|
+
@resource_class = klass
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def perform(_, body)
|
22
|
+
@body = body
|
23
|
+
send(event)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def resource
|
29
|
+
@resource ||= self.class.resource_class.constantize.find(message[:id])
|
30
|
+
end
|
31
|
+
|
32
|
+
def event
|
33
|
+
message[:event]
|
34
|
+
end
|
35
|
+
|
36
|
+
def message
|
37
|
+
@message ||= JSON.parse(@body['Message']).symbolize_keys
|
38
|
+
rescue JSON::ParserError
|
39
|
+
{}
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aws-broker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eng @ Thanx
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-11-
|
11
|
+
date: 2017-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-sns
|
@@ -38,7 +38,7 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
description:
|
41
|
+
description: Ruby pub-sub on AWS
|
42
42
|
email: eng@thanx.com
|
43
43
|
executables: []
|
44
44
|
extensions: []
|
@@ -49,9 +49,12 @@ files:
|
|
49
49
|
- lib/aws/broker.rb
|
50
50
|
- lib/aws/broker/base.rb
|
51
51
|
- lib/aws/broker/config.rb
|
52
|
-
- lib/aws/broker/
|
52
|
+
- lib/aws/broker/matchers.rb
|
53
53
|
- lib/aws/broker/publisher.rb
|
54
|
+
- lib/aws/broker/publishing.rb
|
55
|
+
- lib/aws/broker/railtie.rb
|
54
56
|
- lib/aws/broker/subscriber.rb
|
57
|
+
- lib/shoryuken/subscriber.rb
|
55
58
|
homepage: https://github.com/thanx/aws-broker
|
56
59
|
licenses:
|
57
60
|
- MIT
|
@@ -72,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
75
|
version: '0'
|
73
76
|
requirements: []
|
74
77
|
rubyforge_project:
|
75
|
-
rubygems_version: 2.
|
78
|
+
rubygems_version: 2.5.1
|
76
79
|
signing_key:
|
77
80
|
specification_version: 4
|
78
81
|
summary: AWS Broker
|