aws-broker 0.0.2

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
+ SHA1:
3
+ metadata.gz: c78e1a8326e34ac698ddf33e647d8d263c101256
4
+ data.tar.gz: 8d4a5395d3a052901010f33758b112c54f46d478
5
+ SHA512:
6
+ metadata.gz: 8dd6ecdeeeed53fa28f319acd16a3cbdbfcd471807869868db89e6a8156a7a09c03208f2fa094101e7f081f08359700591e0a72186aa5296d6d9d5b7afb736aa
7
+ data.tar.gz: 21f4532b4ec60c021ae997e197d0e343a719cc6e8188538fee0f0e1d8a7f2beef07c9b4aee2214787bfc84b6ceb13f88da5a47ca8663386361fb5a95584c5bda
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # AWS Broker
2
+
3
+ :incoming_envelope: Ruby pub-sub on AWS
4
+
5
+ * * *
6
+
7
+ ### Background
8
+
9
+ This lightweight abstraction simplifies pub-sub on AWS's SNS and SQS services.
10
+ Message processing is not part of this gem - we recommend using
11
+ [Shoryuken](https://github.com/phstc/shoryuken) in addition to this gem.
12
+
13
+ ### Usage
14
+
15
+ Usage:
16
+
17
+ Broker = Aws::Broker
18
+ topic = 'topic'
19
+ queue = 'queue'
20
+ message = { id: 0 }
21
+ Broker.subscribe(topic, queue)
22
+ Broker.publish(topic, message)
data/lib/aws-broker.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'aws-sdk-sqs'
2
+ require 'aws-sdk-sns'
3
+ require 'aws/broker'
4
+
5
+ module Aws
6
+
7
+ end
data/lib/aws/broker.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'aws/broker/config'
2
+ require 'aws/broker/publisher'
3
+ require 'aws/broker/subscriber'
4
+
5
+ module Aws
6
+ class Broker
7
+ class << self
8
+
9
+ def publish(*params)
10
+ Publisher.new(*params).publish
11
+ end
12
+
13
+ def subscribe(*params)
14
+ Subscriber.new(*params).subscribe
15
+ end
16
+
17
+ def config
18
+ @config ||= Broker::Config.new
19
+ end
20
+
21
+ def configure
22
+ yield(config)
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ module Aws
2
+ class Broker
3
+ class Base
4
+
5
+ private
6
+
7
+ def enabled?
8
+ Broker.config.enabled?
9
+ end
10
+
11
+ def create_topic
12
+ @topic_arn = sns.create_topic(name: @topic).topic_arn
13
+ end
14
+
15
+ def sns
16
+ @sns ||= Aws::SNS::Client.new(credentials)
17
+ end
18
+
19
+ def credentials
20
+ {
21
+ access_key_id: ENV['AWS_ACCESS_KEY'],
22
+ secret_access_key: ENV['AWS_SECRET_KEY'],
23
+ region: ENV['AWS_REGION']
24
+ }
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ module Aws
2
+ class Broker
3
+ class Config
4
+
5
+ attr_accessor :enabled
6
+ alias_method :enabled?, :enabled
7
+
8
+ attr_accessor :queue_prefix
9
+
10
+ def initialize
11
+ self.enabled = true
12
+ self.queue_prefix = nil
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ module Aws
2
+ class Broker
3
+
4
+ VERSION = '0.0.2'.freeze
5
+
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ require 'aws/broker/base'
2
+
3
+ module Aws
4
+ class Broker
5
+ class Publisher < Base
6
+
7
+ def initialize(topic, message)
8
+ @topic = topic
9
+ @message = message
10
+ end
11
+
12
+ def publish
13
+ return unless enabled?
14
+ create_topic
15
+ sns.publish(
16
+ topic_arn: @topic_arn,
17
+ message: @message.to_json
18
+ )
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,82 @@
1
+ require 'aws/broker/base'
2
+
3
+ module Aws
4
+ class Broker
5
+ class Subscriber < Base
6
+
7
+ def initialize(topic, queue=nil)
8
+ @topic = topic
9
+ @queue = queue || queue_name
10
+ end
11
+
12
+ def subscribe
13
+ return unless enabled?
14
+ create_queue
15
+ create_topic
16
+ sns.subscribe(
17
+ topic_arn: @topic_arn,
18
+ protocol: 'sqs',
19
+ endpoint: @queue_arn
20
+ )
21
+ set_queue_policy
22
+ end
23
+
24
+ private
25
+
26
+ def queue_name
27
+ if Broker.config.queue_prefix
28
+ "#{Broker.config.queue_prefix}-#{@topic}"
29
+ else
30
+ @topic
31
+ end
32
+ end
33
+
34
+ def create_queue
35
+ @queue_url = sqs.create_queue(
36
+ queue_name: @queue
37
+ ).queue_url
38
+ @queue_arn = sqs.get_queue_attributes(
39
+ queue_url: @queue_url,
40
+ attribute_names: ['QueueArn']
41
+ ).attributes['QueueArn']
42
+ end
43
+
44
+ def set_queue_policy
45
+ sqs.set_queue_attributes(
46
+ queue_url: @queue_url,
47
+ attributes: { 'Policy': policy }
48
+ )
49
+ end
50
+
51
+ def policy
52
+ <<-POLICY
53
+ {
54
+ "Version": "2008-10-17",
55
+ "Id": "#{@queue_arn}/SQSDefaultPolicy",
56
+ "Statement": [
57
+ {
58
+ "Sid": "#{@queue_arn}-Sid",
59
+ "Effect": "Allow",
60
+ "Principal": {
61
+ "AWS": "*"
62
+ },
63
+ "Action": "SQS:*",
64
+ "Resource": "#{@queue_arn}",
65
+ "Condition": {
66
+ "StringEquals": {
67
+ "aws:SourceArn": "#{@topic_arn}"
68
+ }
69
+ }
70
+ }
71
+ ]
72
+ }
73
+ POLICY
74
+ end
75
+
76
+ def sqs
77
+ @sqs ||= Aws::SQS::Client.new(credentials)
78
+ end
79
+
80
+ end
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws-broker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Eng @ Thanx
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-sns
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-sqs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Lightweight Ruby pub-sub abstraction on AWS
42
+ email: eng@thanx.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - README.md
48
+ - lib/aws-broker.rb
49
+ - lib/aws/broker.rb
50
+ - lib/aws/broker/base.rb
51
+ - lib/aws/broker/config.rb
52
+ - lib/aws/broker/constants.rb
53
+ - lib/aws/broker/publisher.rb
54
+ - lib/aws/broker/subscriber.rb
55
+ homepage: https://github.com/thanx/aws-broker
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.6.11
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: AWS Broker
79
+ test_files: []