announce 0.1.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
+ SHA1:
3
+ metadata.gz: 9e8d5885a6f2032d348745eb8a772fa7954a4b6f
4
+ data.tar.gz: 6e61f5138c7007c219fab7e21b31a822f8652431
5
+ SHA512:
6
+ metadata.gz: 61db19b838c4315a5805912204adb8dc4f1ec4f6ea5f568ea299876a978bd69c84b8432922f72e21d3ce0918a09f9744f0c5cd2c2d0e623a025e5404c63b7c2c
7
+ data.tar.gz: 6bbac8d7678e2d58f75e90ab6921e87b60db46be445c409a0e189c2d2e25b3ddefa46b3141dde9573d127e5a760e5b5304b6caafa7b01ef7e4cf3b12dbb165a4
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .env
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in announce.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,50 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features)
6
+
7
+ ## Uncomment to clear the screen before every task
8
+ # clearing :on
9
+
10
+ ## Guard internally checks for changes in the Guardfile and exits.
11
+ ## If you want Guard to automatically start up again, run guard in a
12
+ ## shell loop, e.g.:
13
+ ##
14
+ ## $ while bundle exec guard; do echo "Restarting Guard..."; done
15
+ ##
16
+ ## Note: if you are using the `directories` clause above and you are not
17
+ ## watching the project directory ('.'), then you will want to move
18
+ ## the Guardfile to a watched dir and symlink it back, e.g.
19
+ #
20
+ # $ mkdir config
21
+ # $ mv Guardfile config/
22
+ # $ ln -s config/Guardfile .
23
+ #
24
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
25
+
26
+ guard :minitest do
27
+ # with Minitest::Unit
28
+ watch(%r{^test/(.*)\/?test_(.*)\.rb$})
29
+ watch(%r{^lib/announce/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
30
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
31
+
32
+ # with Minitest::Spec
33
+ # watch(%r{^spec/(.*)_spec\.rb$})
34
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
35
+ # watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
36
+
37
+ # Rails 4
38
+ # watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
39
+ # watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' }
40
+ # watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" }
41
+ # watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
42
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" }
43
+ # watch(%r{^test/.+_test\.rb$})
44
+ # watch(%r{^test/test_helper\.rb$}) { 'test' }
45
+
46
+ # Rails < 4
47
+ # watch(%r{^app/controllers/(.*)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
48
+ # watch(%r{^app/helpers/(.*)\.rb$}) { |m| "test/helpers/#{m[1]}_test.rb" }
49
+ # watch(%r{^app/models/(.*)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
50
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Andrew Kuklewicz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Announce
2
+
3
+ ## Announce lets your services know about events, and helps you process them.
4
+
5
+ Announce is a gem to support using pub sub messages for events, built on top of other job processors like shoryuken and using ActiveJob.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'announce'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install announce
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/announce/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ end
7
+
8
+ task :default => :test
9
+
data/announce.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'announce/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'announce'
8
+ spec.version = Announce::VERSION
9
+ spec.authors = ['Andrew Kuklewicz']
10
+ spec.email = ['andrew@prx.org']
11
+
12
+ spec.summary = %q{Announce is for pubsub of messages}
13
+ spec.description = %q{Announce is for pubsub of messages for events, built on other gems like shoryuken.}
14
+ spec.homepage = 'https://github.com/PRX/announce'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'shoryuken', '~> 1.0'
23
+ spec.add_development_dependency 'bundler', '~> 1.8'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'growl'
26
+ spec.add_development_dependency 'minitest'
27
+ spec.add_development_dependency 'guard'
28
+ spec.add_development_dependency 'guard-minitest'
29
+ spec.add_development_dependency 'dotenv'
30
+ end
data/bin/console ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "announce"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require 'dotenv'
10
+ Dotenv.load
11
+
12
+ require 'shoryuken'
13
+ Shoryuken::Client.account_id = ENV['AWS_ACCOUNT_ID'] unless Shoryuken::Client.account_id
14
+
15
+ # (If you use this, don't forget to add pry to your Gemfile!)
16
+ # require "pry"
17
+ # Pry.start
18
+
19
+ require "irb"
20
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/env-sample ADDED
@@ -0,0 +1,4 @@
1
+ AWS_ACCESS_KEY_ID=
2
+ AWS_SECRET_ACCESS_KEY=
3
+ AWS_REGION=
4
+ AWS_ACCOUNT_ID=
@@ -0,0 +1,102 @@
1
+ require 'announce'
2
+ require 'announce/message'
3
+
4
+ # publish, subscribe, and configure_broker are the 3 required methods for an adapter
5
+ # this base adapter also has some helpful base classes, but they are not necessary
6
+ # you could write an adapter from scratch so long as the class has these 3 class methods.
7
+ module Announce
8
+ module Adapters
9
+ class BaseAdapter
10
+
11
+ class << self
12
+
13
+ def publish(subject, action, body, options = {})
14
+ topic = adapter_constantize(:topic).new(subject, action, options)
15
+ msg = Announce::Message.new(subject: subject, action: action, body: body)
16
+ topic.publish(msg.to_message, options)
17
+ end
18
+
19
+ def subscribe(worker_class, subject, actions = [], options = {})
20
+ subscriber = adapter_constantize(:subscriber).new
21
+ subscriber.subscribe(worker_class, subject, actions, options)
22
+ end
23
+
24
+ def configure_broker(options)
25
+ broker_manager = adapter_constantize(:broker_manager).new(options)
26
+ broker_manager.configure
27
+ end
28
+
29
+ def adapter_constantize(name)
30
+ "::Announce::Adapters::#{Announce.options[:adapter].to_s.camelize}Adapter::#{name.to_s.camelize}".constantize
31
+ end
32
+ end
33
+
34
+ class Subscriber
35
+ def subscribe(worker_class, subject, actions, options)
36
+ raise NotImplementedError.new("You must implement subscribe.")
37
+ end
38
+ end
39
+
40
+ class BrokerManager
41
+ attr_accessor :options
42
+
43
+ # uses the configuration
44
+ def initialize(options = Announce.options)
45
+ @options = options
46
+ end
47
+
48
+ # actually configure the broker queues, topics, and subscriptions
49
+ def configure
50
+ raise NotImplementedError.new("You must implement configure.")
51
+ end
52
+ end
53
+
54
+ class Destination
55
+ attr_accessor :subject, :action, :options
56
+
57
+ def publish(message, options = {})
58
+ raise NotImplementedError.new("You must implement publish.")
59
+ end
60
+
61
+ def create
62
+ raise NotImplementedError.new("You must implement create.")
63
+ end
64
+
65
+ def self.name_for(subject, action)
66
+ [prefix, subject, action].join(delimiter)
67
+ end
68
+
69
+ def initialize(subject, action, options = {})
70
+ @subject = subject
71
+ @action = action
72
+ @options = options || {}
73
+ end
74
+
75
+ def name(subject = @subject, action = @action)
76
+ self.class.name_for(subject, action)
77
+ end
78
+
79
+ def self.prefix
80
+ ::Announce.options[:name_prefix]
81
+ end
82
+
83
+ def self.delimiter
84
+ ::Announce.options[:name_delimiter]
85
+ end
86
+
87
+ def self.app
88
+ ::Announce.options[:app_name]
89
+ end
90
+ end
91
+
92
+ class Topic < Destination
93
+ end
94
+
95
+ class Queue < Destination
96
+ def self.name_for(subject, action)
97
+ [prefix, app, subject, action].join(delimiter)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,41 @@
1
+ require 'announce/adapters/base_adapter'
2
+
3
+ module Announce
4
+ module Adapters
5
+ class InlineAdapter < BaseAdapter
6
+
7
+ def self.subscriptions
8
+ @@subscriptions ||= {}
9
+ end
10
+
11
+ class BrokerManager < BaseAdapter::BrokerManager
12
+ def configure; end
13
+ end
14
+
15
+ class Subscriber < BaseAdapter::Subscriber
16
+ def subscribe(worker_class, subject, actions, options)
17
+ Array(actions).each do |action|
18
+ queue_name = Queue.name_for(subject, action)
19
+ InlineAdapter.subscriptions[queue_name] = worker_class
20
+ end
21
+ end
22
+ end
23
+
24
+ class Topic < BaseAdapter::Topic
25
+ def publish(message, options = {})
26
+ queue_name = Queue.name_for(subject, action)
27
+ worker_class = InlineAdapter.subscriptions[queue_name]
28
+ if defined?(::ActiveJob)
29
+ job = worker_class.new(message)
30
+ ::ActiveJob::Base.execute(job.serialize)
31
+ else
32
+ worker_class.new.perform(message)
33
+ end
34
+ end
35
+ end
36
+
37
+ class Queue < BaseAdapter::Queue
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,214 @@
1
+ require 'shoryuken'
2
+ require 'announce/adapters/base_adapter'
3
+
4
+ module Announce
5
+ module Adapters
6
+ class ShoryukenAdapter < BaseAdapter
7
+
8
+ class Subscriber < BaseAdapter::Subscriber
9
+ def subscribe(worker_class, subject, actions, options)
10
+ Array(actions).each do |action|
11
+ queue_name = Queue.name_for(subject, action)
12
+ Shoryuken.register_worker(queue_name, worker_class, true)
13
+ end
14
+ end
15
+ end
16
+
17
+ class BrokerManager < BaseAdapter::BrokerManager
18
+
19
+ # actually configure the broker queues, topics, and subscriptions
20
+ def configure
21
+ configure_publishing
22
+ configure_subscribing
23
+ end
24
+
25
+ def configure_publishing
26
+ (options[:publish] || {}).each do |subject, actions|
27
+ Array(actions).each do |action|
28
+ Announce::Adapter::ShoryukenAdapter::Topic(subject, action, options).create
29
+ end
30
+ end
31
+ end
32
+
33
+ def configure_subscribing
34
+ (options[:subscribe] || {}).each do |subject, actions|
35
+ Array(actions).each do |action|
36
+ topic = Announce::Adapter::ShoryukenAdapter::Topic(subject, action, options)
37
+ queue = Announce::Adapter::ShoryukenAdapter::Queue(subject, action, options)
38
+ topic.create
39
+ queue.create
40
+ topic.subscribe(queue)
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ class Topic < BaseAdapter::Topic
47
+
48
+ def publish(message, options = {})
49
+ Shoryuken::Client.topics(name).send_message(message, options)
50
+ end
51
+
52
+ def create
53
+ sns.create_topic(name: name)[:topic_arn]
54
+ end
55
+
56
+ def subscribe(queue)
57
+ subscription_arn = sns.subscribe(
58
+ topic_arn: arn,
59
+ protocol: 'sqs',
60
+ endpoint: queue.arn
61
+ )[:subscription_arn]
62
+
63
+ sns.set_subscription_attributes(
64
+ subscription_arn: subscription_arn,
65
+ attribute_name: 'RawMessageDelivery',
66
+ attribute_value: 'true'
67
+ )
68
+ subscription_arn
69
+ end
70
+
71
+ def arn
72
+ account_id = Shoryuken::Client.account_id
73
+ region = sns.config[:region]
74
+ "arn:aws:sns:#{region}:#{account_id}:#{name}"
75
+ end
76
+
77
+ def sns
78
+ Shoryuken::Client.sns
79
+ end
80
+ end
81
+
82
+ class Queue < BaseAdapter::Queue
83
+
84
+ def create
85
+ create_attributes = default_options.merge((options[:queues] || {}).stringify_keys)
86
+
87
+ dlq_arn = create_dlq
88
+ create_attributes['RedrivePolicy'] = %Q{{"maxReceiveCount":"10", "deadLetterTargetArn":"#{dlq_arn}"}"}
89
+
90
+ sqs.create_queue(queue_name: name, attributes: create_attributes)[:queue_url]
91
+ end
92
+
93
+ def arn
94
+ account_id = Shoryuken::Client.account_id
95
+ region = sqs.config[:region]
96
+ "arn:aws:sqs:#{region}:#{account_id}:#{name}"
97
+ end
98
+
99
+ def create_dlq
100
+ dlq_name = "#{name}_failures"
101
+
102
+ dlq_options = {
103
+ 'MaximumMessageSize' => "#{(256 * 1024)}",
104
+ 'MessageRetentionPeriod' => "#{2 * 7 * 24 * 60 * 60}" # 2 weeks in seconds
105
+ }
106
+
107
+ dlq = sqs.create_queue(
108
+ queue_name: dlq_name,
109
+ attributes: dlq_options
110
+ )
111
+
112
+ attrs = sqs.get_queue_attributes(
113
+ queue_url: dlq.queue_url,
114
+ attribute_names: ['QueueArn']
115
+ )
116
+
117
+ attrs.attributes['QueueArn']
118
+ end
119
+
120
+ def default_options
121
+ {
122
+ 'DelaySeconds' => '0',
123
+ 'MaximumMessageSize' => "#{256 * 1024}",
124
+ 'VisibilityTimeout' => "#{60 * 60}", # 1 hour in seconds
125
+ 'ReceiveMessageWaitTimeSeconds' => '0',
126
+ 'MessageRetentionPeriod' => "#{7 * 24 * 60 * 60}", # 1 week in seconds
127
+ 'Policy' => policy
128
+ }
129
+ end
130
+
131
+ def policy
132
+ account_id = Shoryuken::Client.account_id
133
+ region = sqs.config[:region]
134
+ {
135
+ "Version" => "2012-10-17",
136
+ "Id" => "AnnounceSNStoSQS",
137
+ "Statement" => [
138
+ {
139
+ "Sid" => "1",
140
+ "Effect" => "Allow",
141
+ "Principal" => {
142
+ "AWS" => "*"
143
+ },
144
+ "Action" => "sqs:*",
145
+ "Resource" => arn,
146
+ "Condition" => {
147
+ "ArnLike" => {
148
+ "aws:SourceArn" => "arn:aws:sns:#{region}:#{account_id}:*"
149
+ }
150
+ }
151
+ }
152
+ ]
153
+ }.to_json
154
+ end
155
+
156
+ def sqs
157
+ Shoryuken::Client.sqs
158
+ end
159
+ end
160
+
161
+ class AnnounceWorker #:nodoc:
162
+ include Shoryuken::Worker
163
+
164
+ attr_accessor :job_class
165
+
166
+ shoryuken_options body_parser: :json, auto_delete: true
167
+
168
+ def perform(sqs_msg, hash)
169
+ job = job_class.new(hash)
170
+ Base.execute(job.serialize)
171
+ end
172
+ end
173
+
174
+ class AnnounceWorkerRegistry < Shoryuken::DefaultWorkerRegistry
175
+
176
+ attr_accessor :subscriptions
177
+
178
+ def initialize
179
+ super
180
+ @subscribers = {}
181
+ end
182
+
183
+ def clear
184
+ super
185
+ @subscribers.clear
186
+ end
187
+
188
+ def fetch_worker(queue, message)
189
+ super.tap do |worker|
190
+ if @subscribers[queue] && worker.respond_to?('job_class=')
191
+ worker.job_class = @subscribers[queue]
192
+ end
193
+ end
194
+ end
195
+
196
+ def register_worker(queue, clazz, subscription = false)
197
+ if subscription && active_job?
198
+ @subscribers[queue] = clazz
199
+ clazz = AnnounceWorker
200
+ end
201
+ super(queue, clazz)
202
+ end
203
+
204
+ def active_job?
205
+ defined?(::ActiveJob) && ::ActiveJob::Base.queue_adapter.to_s == 'shoryuken'
206
+ end
207
+ end
208
+
209
+ end
210
+ end
211
+ end
212
+
213
+ Shoryuken.worker_registry = Announce::Adapters::ShoryukenAdapter::AnnounceWorkerRegistry.new
214
+ Shoryuken::Client.account_id = ENV['AWS_ACCOUNT_ID'] unless Shoryuken::Client.account_id
@@ -0,0 +1,62 @@
1
+ require 'announce/adapters/base_adapter'
2
+
3
+ module Announce
4
+ module Adapters
5
+ class TestAdapter < BaseAdapter
6
+
7
+ class Subscriber < BaseAdapter::Subscriber
8
+
9
+ @@subscriptions = []
10
+
11
+ def self.subscriptions
12
+ @@subscriptions
13
+ end
14
+
15
+ def subscribe(worker_class, subject, actions, options)
16
+ @@subscriptions << [worker_class, subject, actions, options]
17
+ true
18
+ end
19
+ end
20
+
21
+ class BrokerManager < BaseAdapter::BrokerManager
22
+ @@configured = false
23
+
24
+ def self.reset
25
+ @@configured = false
26
+ end
27
+
28
+ def self.configured?
29
+ @@configured
30
+ end
31
+
32
+ def configure
33
+ @@configured = true
34
+ end
35
+ end
36
+
37
+ class Topic < BaseAdapter::Topic
38
+
39
+ @@published_messages = []
40
+
41
+ def self.published_messages
42
+ @@published_messages
43
+ end
44
+
45
+ def publish(message, options = {})
46
+ @@published_messages << message
47
+ true
48
+ end
49
+
50
+ def create
51
+ true
52
+ end
53
+ end
54
+
55
+ class Queue < BaseAdapter::Queue
56
+ def create
57
+ true
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,32 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+
4
+ module Announce
5
+ class Configuration
6
+ attr_reader :options
7
+
8
+ def self.configure(options={})
9
+ opts = new(options).configure
10
+ Announce.options.merge!(opts)
11
+ end
12
+
13
+ def initialize(options)
14
+ @options = options
15
+ base = defined?(Rails) ? Rails.root : Dir.pwd
16
+ options[:pub_sub_file] ||= File.join(base, 'config', 'pub_sub.yml')
17
+ end
18
+
19
+ def pub_sub_file
20
+ options[:pub_sub_file]
21
+ end
22
+
23
+ def configure
24
+ if File.exist?(pub_sub_file)
25
+ YAML.load(ERB.new(IO.read(pub_sub_file)).result).deep_symbolize_keys
26
+ else
27
+ Announce.logger.warn "PubSub file #{pub_sub_file} does not exist"
28
+ {}
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,63 @@
1
+ begin
2
+ require 'active_support/core_ext/hash/keys'
3
+ require 'active_support/core_ext/hash/deep_merge'
4
+ require 'active_support/core_ext/hash/slice'
5
+ rescue LoadError
6
+ class Hash
7
+ def stringify_keys
8
+ keys.each do |key|
9
+ self[key.to_s] = delete(key)
10
+ end
11
+ self
12
+ end if !{}.respond_to?(:stringify_keys)
13
+
14
+ def symbolize_keys
15
+ keys.each do |key|
16
+ self[(key.to_sym rescue key) || key] = delete(key)
17
+ end
18
+ self
19
+ end if !{}.respond_to?(:symbolize_keys)
20
+
21
+ def deep_symbolize_keys
22
+ keys.each do |key|
23
+ value = delete(key)
24
+ self[(key.to_sym rescue key) || key] = value
25
+
26
+ value.deep_symbolize_keys if value.is_a? Hash
27
+ end
28
+ self
29
+ end if !{}.respond_to?(:deep_symbolize_keys)
30
+
31
+ def slice(*keys)
32
+ keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
33
+ keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
34
+ end
35
+ end
36
+ end
37
+
38
+ begin
39
+ require 'active_support/core_ext/string/inflections'
40
+ rescue LoadError
41
+ class String
42
+ def constantize
43
+ names = self.split('::')
44
+ names.shift if names.empty? || names.first.empty?
45
+
46
+ constant = Object
47
+ names.each do |name|
48
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
49
+ end
50
+ constant
51
+ end
52
+ end if !"".respond_to?(:constantize)
53
+
54
+ class String
55
+ def camelize
56
+ string = self
57
+ string = string.sub(/^[a-z\d]*/) { $&.capitalize }
58
+ string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
59
+ string.gsub!(/\//, '::')
60
+ string
61
+ end
62
+ end if !"".respond_to?(:camelize)
63
+ end
@@ -0,0 +1,28 @@
1
+ require 'json'
2
+
3
+ module Announce
4
+ class Message
5
+
6
+ attr_accessor :options
7
+
8
+ def initialize(options={})
9
+ @options = {
10
+ 'message_id' => SecureRandom.uuid,
11
+ 'app' => app,
12
+ 'sent_at' => Time.now.utc
13
+ }.merge(options).stringify_keys
14
+ end
15
+
16
+ def app
17
+ Announce.options[:app_name]
18
+ end
19
+
20
+ def to_message
21
+ options.stringify_keys
22
+ end
23
+
24
+ def to_json
25
+ options.to_json
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ require 'shoryuken'
2
+ require 'announce'
3
+ require 'announce/message'
4
+
5
+ module Announce
6
+ module Publisher
7
+ def publish(subject, action, body, options = {})
8
+ Announce.publish(subject, action, body, options)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,45 @@
1
+ # let's see if we can use active_job for this...
2
+
3
+ module Announce
4
+ module Subscriber
5
+
6
+ def self.included(base)
7
+ base.class_eval do
8
+ attr_accessor :subject, :action, :message
9
+ end
10
+
11
+ base.extend(ClassMethods)
12
+ end
13
+
14
+ module ClassMethods
15
+ def subscribe_to(subject, actions=[], options = {})
16
+ Announce.subscribe(self, subject, actions, options)
17
+ end
18
+ end
19
+
20
+ def perform(*args)
21
+ delegate_event(*args)
22
+ end
23
+
24
+ # For use in adapters to delegate to method named receive_subject_action
25
+ def delegate_event(event)
26
+ @message = event.deep_symbolize_keys
27
+ @subject = message[:subject]
28
+ @action = message[:action]
29
+
30
+ if [message, subject, action].any? { |a| a.nil? }
31
+ raise "Message, subject, and action are not all specified for '#{event.inspect}'"
32
+ end
33
+
34
+ if respond_to?(delegate_method)
35
+ public_send(delegate_method, message[:body])
36
+ else
37
+ raise "`#{self.class.name}` is subscribed, but doesn't implement `#{delegate_method}` for '#{event.inspect}'"
38
+ end
39
+ end
40
+
41
+ def delegate_method(message = @message)
42
+ ['receive', message[:subject], message[:action]].join('_')
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Announce
2
+ VERSION = "0.1.0"
3
+ end
data/lib/announce.rb ADDED
@@ -0,0 +1,66 @@
1
+ require 'logger'
2
+
3
+ require 'announce/configuration'
4
+ require 'announce/core_ext'
5
+ require 'announce/message'
6
+ require 'announce/publisher'
7
+ require 'announce/subscriber'
8
+ require 'announce/version'
9
+
10
+ module Announce
11
+ class << self
12
+
13
+ def publish(subject, action, message, options={})
14
+ adapter_class.publish(subject, action, message, options)
15
+ end
16
+
17
+ def subscribe(worker_class, subject, actions = [], options = {})
18
+ adapter_class.subscribe(worker_class, subject, actions, options)
19
+ end
20
+
21
+ def configure_broker
22
+ adapter_class.configure_broker(options)
23
+ end
24
+
25
+ def options
26
+ @options ||= default_options
27
+ end
28
+
29
+ def configure(opts = {})
30
+ Announce::Configuration.configure(opts)
31
+ yield @options if block_given?
32
+ end
33
+
34
+ def default_options
35
+ {}.tap do |defaults|
36
+ if defined?(ActiveJob)
37
+ defaults[:name_prefix] = ::ActiveJob::Base.queue_name_prefix
38
+ defaults[:name_delimiter] = ::ActiveJob::Base.queue_name_delimiter
39
+ defaults[:adapter] = ::ActiveJob::Base.queue_adapter
40
+ else
41
+ defaults[:name_prefix] = ENV['RAILS_ENV'] || ENV['APP_ENV'] || 'development'
42
+ defaults[:name_delimiter] = '_'
43
+ defaults[:adapter] = :inline
44
+ end
45
+ end
46
+ end
47
+
48
+ def adapter_class
49
+ adapter = Announce.options[:adapter]
50
+ require "announce/adapters/#{adapter}_adapter"
51
+ "::Announce::Adapters::#{adapter.to_s.camelize}Adapter".constantize
52
+ end
53
+
54
+ def logger
55
+ @logger ||= if defined?(Rails)
56
+ Rails.logger
57
+ else
58
+ Logger.new(STDOUT)
59
+ end
60
+ end
61
+
62
+ def logger=(l)
63
+ @logger = l
64
+ end
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: announce
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kuklewicz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shoryuken
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: growl
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: dotenv
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Announce is for pubsub of messages for events, built on other gems like
126
+ shoryuken.
127
+ email:
128
+ - andrew@prx.org
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".travis.yml"
135
+ - CODE_OF_CONDUCT.md
136
+ - Gemfile
137
+ - Guardfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - announce.gemspec
142
+ - bin/console
143
+ - bin/setup
144
+ - env-sample
145
+ - lib/announce.rb
146
+ - lib/announce/adapters/base_adapter.rb
147
+ - lib/announce/adapters/inline_adapter.rb
148
+ - lib/announce/adapters/shoryuken_adapter.rb
149
+ - lib/announce/adapters/test_adapter.rb
150
+ - lib/announce/configuration.rb
151
+ - lib/announce/core_ext.rb
152
+ - lib/announce/message.rb
153
+ - lib/announce/publisher.rb
154
+ - lib/announce/subscriber.rb
155
+ - lib/announce/version.rb
156
+ homepage: https://github.com/PRX/announce
157
+ licenses:
158
+ - MIT
159
+ metadata: {}
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 2.4.5
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: Announce is for pubsub of messages
180
+ test_files: []