propono 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDE0YzRiMzE2ZmNkZGU3NGIwODc4YzkxMjljNWFiMGM0MDEwM2U3OA==
4
+ OGI3ZmNiOTNiM2RhMmMzOWQwM2NlNTM4OWRhMjY0NjJiYmUyZGMwMQ==
5
5
  data.tar.gz: !binary |-
6
- N2Y4YTI2YmY3NWM3ZTA0MjJlZWJkMjkxNGQxMGYwZDlkNDI4NzE0Zg==
6
+ ZTgyOTIyOTRlNWZhYzNiNGRjNzc0NGZiZTk4ZWVhZjA1ODNmY2UzZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MGM3Mzg3NmZmZjIzYjZjMTUyODMzMzM1ZGZlNDRhNjYwNzkzMzE4ZTI0ODA4
10
- YzJlNzc5OGE2MzMyNDY0MWViYTVlZjNmODIyMGIyMGY0OWE1ZDgxOThiYWI5
11
- NGFlMjIzYTAxNzcxZTBhYzQ0YmE5ZTQ2NzYyNDViNDYzNGNhMzc=
9
+ MjZjMTVlN2IwMmM5Njc5YzMyN2YzZTNmZmUzMDFkYWNkNWFiY2JkYmY1MWMx
10
+ MDA2OWJlOTNhODc2MWQyNzUzYjgxODI0ZjZiMzBhZDJiNDEwZjM1ZmNiMzhl
11
+ Y2ZkNTc1MTQ3NDcyZDJkZjdiZjNlYTQyZmZmMmI1MjY1M2RkYzk=
12
12
  data.tar.gz: !binary |-
13
- MGJkYTU1YTBlZTI4ZWJlZWMwZDcyZjg3NWNmOTBlOGU5YjMyYTcxOTI1OGZm
14
- MTczNGM2NDE2MDJlZDQ4ZjY5ZDc4NWE2MWU4YWRmODUzZWI2MzQ3OGZlYzIx
15
- OGE1ZmJkY2U1OGI5ODFmMWRlYTJkYjliYjRkZGJmOWUxYTkxZTc=
13
+ NmM4ZGNmNThjOTFmZTc5MGNmOGFmOWMxNjJlODE0NDRjMWMyZGFkNWU1OThm
14
+ NjNhNDNhMDQ2NjUxOTc0MTU4Njk1NmIwOTE0NGM5ZjkyN2EyOTNjNzkzZjFm
15
+ OWE4MTIzZmMyMGMyN2YzNGM2NDQxNTJjMDA0ZGUyNTg0ZTlmZDA=
data/lib/propono.rb CHANGED
@@ -4,12 +4,12 @@ require 'propono/components/sns'
4
4
  require 'propono/components/sqs'
5
5
  require "propono/components/queue"
6
6
  require "propono/components/topic"
7
+ require "propono/components/post_subscription"
8
+ require "propono/components/queue_subscription"
7
9
 
8
- require "propono/services/post_subscriber"
9
10
  require "propono/services/publisher"
10
11
  require "propono/services/queue_creator"
11
12
  require "propono/services/queue_listener"
12
- require "propono/services/queue_subscriber"
13
13
  require "propono/services/subscriber"
14
14
  require "propono/services/topic_creator"
15
15
 
@@ -1,9 +1,9 @@
1
1
  module Propono
2
- class PostSubscriber
2
+ class PostSubscription
3
3
  include Sns
4
4
 
5
- def self.subscribe(topic, endpoint)
6
- new(topic, endpoint).subscribe
5
+ def self.create(topic, endpoint)
6
+ new(topic, endpoint).create
7
7
  end
8
8
 
9
9
  def initialize(topic_id, endpoint)
@@ -11,7 +11,7 @@ module Propono
11
11
  @endpoint = endpoint
12
12
  end
13
13
 
14
- def subscribe
14
+ def create
15
15
  topic_arn = TopicCreator.find_or_create(@topic_id)
16
16
  sns.subscribe(topic_arn, @endpoint, 'http')
17
17
  end
@@ -1,31 +1,33 @@
1
1
  module Propono
2
- class QueueSubscriber
2
+ class QueueSubscription
3
3
 
4
4
  include Sns
5
5
  include Sqs
6
6
 
7
7
  attr_reader :topic_arn, :queue
8
8
 
9
- def self.subscribe(topic_id)
10
- new(topic_id).subscribe
9
+ def self.create(topic_id)
10
+ new(topic_id).tap do |subscription|
11
+ subscription.create
12
+ end
11
13
  end
12
14
 
13
15
  def initialize(topic_id)
14
16
  @topic_id = topic_id
15
17
  end
16
18
 
17
- def subscribe
19
+ def create
18
20
  @topic = TopicCreator.find_or_create(@topic_id)
19
21
  @queue = QueueCreator.find_or_create(queue_name)
20
22
  sns.subscribe(@topic.arn, @queue.arn, 'sqs')
21
23
  end
22
24
 
23
- private
24
-
25
25
  def queue_name
26
- "#{config.application_name.gsub(" ", "_")}::#{@topic_id}"
26
+ @queue_name ||= "#{config.application_name.gsub(" ", "_")}::#{@topic_id}"
27
27
  end
28
28
 
29
+ private
30
+
29
31
  def config
30
32
  Configuration.instance
31
33
  end
@@ -3,43 +3,51 @@ module Propono
3
3
 
4
4
  include Sqs
5
5
 
6
- def self.listen(queue_url, &block)
7
- new(queue_url, &block).listen
6
+ def self.listen(topic_id, &message_processor)
7
+ new(topic_id, &message_processor).listen
8
8
  end
9
9
 
10
- def initialize(queue_url, &block)
11
- @queue_url = queue_url
12
- @block = block
10
+ def initialize(topic_id, &message_processor)
11
+ @topic_id = topic_id
12
+ @message_processor = message_processor
13
13
  end
14
14
 
15
15
  def listen
16
- loop {
17
- sleep 10 unless read_messages
18
- }
16
+ loop do
17
+ unless read_messages
18
+ sleep 10
19
+ end
20
+ end
19
21
  end
20
22
 
21
23
  private
22
24
 
23
25
  def read_messages
24
- begin
25
- response = sqs.receive_message( @queue_url, options = { 'MaxNumberOfMessages' => 10 } )
26
- messages = response.body['Message']
27
- if messages.empty?
28
- false
29
- else
30
- process_messages(messages)
31
- end
32
- rescue
33
- config.logger.puts "Unexpected error reading from queue #{@queue_url}"
26
+ response = sqs.receive_message( queue_url, options = { 'MaxNumberOfMessages' => 10 } )
27
+ messages = response.body['Message']
28
+ if messages.empty?
29
+ false
30
+ else
31
+ process_messages(messages)
34
32
  end
33
+ rescue
34
+ config.logger.puts "Unexpected error reading from queue #{queue_url}"
35
35
  end
36
36
 
37
37
  def process_messages(messages)
38
38
  messages.each do |message|
39
- @block.call(message)
39
+ @message_processor.call(message)
40
40
  sqs.delete_message(message['ReceiptHandle'])
41
41
  end
42
42
  true
43
43
  end
44
+
45
+ def queue_url
46
+ @queue_url ||= subscription.queue.url
47
+ end
48
+
49
+ def subscription
50
+ @subscription ||= QueueSubscription.create(@topic_id)
51
+ end
44
52
  end
45
53
  end
@@ -2,11 +2,11 @@ module Propono
2
2
 
3
3
  module Subscriber
4
4
  def self.subscribe_by_queue(topic)
5
- QueueSubscriber.subscribe(topic)
5
+ QueueSubscription.create(topic)
6
6
  end
7
7
 
8
8
  def self.subscribe_by_post(topic, endpoint)
9
- PostSubscriber.subscribe(topic, endpoint)
9
+ PostSubscription.create(topic, endpoint)
10
10
  end
11
11
  end
12
12
  end
@@ -1,3 +1,3 @@
1
1
  module Propono
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -1,14 +1,14 @@
1
1
  require File.expand_path('../test_helper', __FILE__)
2
2
 
3
3
  module Propono
4
- class PostSubscriberTest < Minitest::Test
4
+ class PostSubscriptionTest < Minitest::Test
5
5
  def test_create_topic
6
6
  topic = 'foobar'
7
7
  TopicCreator.expects(:find_or_create).with(topic)
8
- PostSubscriber.subscribe(topic, "foobar")
8
+ PostSubscription.create(topic, "foobar")
9
9
  end
10
10
 
11
- def test_subscribe_calls_subscribe
11
+ def test_create_calls_create
12
12
  arn = "arn123"
13
13
  endpoint = "http://meducation.net/some_queue_name"
14
14
 
@@ -16,9 +16,9 @@ module Propono
16
16
 
17
17
  sns = mock()
18
18
  sns.expects(:subscribe).with(arn, endpoint, 'http')
19
- subscriber = PostSubscriber.new("Some topic", endpoint)
20
- subscriber.stubs(sns: sns)
21
- subscriber.subscribe
19
+ subscription = PostSubscription.new("Some topic", endpoint)
20
+ subscription.stubs(sns: sns)
21
+ subscription.create
22
22
  end
23
23
 
24
24
  def test_it_correctly_uses_http_and_https
@@ -4,10 +4,11 @@ module Propono
4
4
  class QueueListenerTest < Minitest::Test
5
5
 
6
6
  def setup
7
- @queue_url = "http://example.com"
7
+ super
8
+ @topic_id = "some-topic"
8
9
 
9
- @receipt_hanlde1 = "test-receipt-handle1"
10
- @receipt_hanlde2 = "test-receipt-handle2"
10
+ @receipt_handle1 = "test-receipt-handle1"
11
+ @receipt_handle2 = "test-receipt-handle2"
11
12
  @message1 = { "ReceiptHandle" => @receipt_handle1}
12
13
  @message2 = { "ReceiptHandle" => @receipt_handle2}
13
14
  @messages = { "Message" => [ @message1, @message2 ] }
@@ -22,10 +23,10 @@ module Propono
22
23
 
23
24
  sqs.expects(:receive_message).returns(message_response)
24
25
 
25
- queueListener = QueueListener.new(@queue_url) {}
26
- queueListener.stubs(sqs: sqs)
26
+ queue_listener = QueueListener.new(@topic_id) {}
27
+ queue_listener.stubs(sqs: sqs)
27
28
 
28
- queueListener.send(:read_messages)
29
+ queue_listener.send(:read_messages)
29
30
  end
30
31
 
31
32
  def test_each_message_yielded
@@ -37,10 +38,10 @@ module Propono
37
38
  sqs.expects(:delete_message).with(@receipt_handle1)
38
39
  sqs.expects(:delete_message).with(@receipt_handle2)
39
40
 
40
- queueListener = QueueListener.new(@queue_url) { }
41
- queueListener.stubs(sqs: sqs)
41
+ queue_listener = QueueListener.new(@topic_id) { }
42
+ queue_listener.stubs(sqs: sqs)
42
43
 
43
- queueListener.send(:read_messages)
44
+ queue_listener.send(:read_messages)
44
45
  end
45
46
 
46
47
  def test_each_message_deleted_from_sqs
@@ -51,10 +52,10 @@ module Propono
51
52
  sqs.stubs(receive_message: message_response)
52
53
 
53
54
  messages_yielded = [ ]
54
- queueListener = QueueListener.new(@queue_url) { |m| messages_yielded.push(m) }
55
- queueListener.stubs(sqs: sqs)
55
+ queue_listener = QueueListener.new(@topic_id) { |m| messages_yielded.push(m) }
56
+ queue_listener.stubs(sqs: sqs)
56
57
 
57
- queueListener.send(:read_messages)
58
+ queue_listener.send(:read_messages)
58
59
 
59
60
  assert_equal messages_yielded.size, 2
60
61
  assert messages_yielded.include?(@message1)
@@ -70,29 +71,51 @@ module Propono
70
71
 
71
72
  sqs.expects(:receive_message).returns(message_response)
72
73
 
73
- queueListener = QueueListener.new(@queue_url) {}
74
- queueListener.stubs(sqs: sqs)
74
+ queue_listener = QueueListener.new(@topic_id) {}
75
+ queue_listener.stubs(sqs: sqs)
75
76
 
76
- refute queueListener.send(:read_messages)
77
+ refute queue_listener.send(:read_messages)
77
78
  end
78
79
 
79
80
  def test_exception_from_sqs_is_logged
80
81
  sqs = mock()
81
82
  sqs.stubs(:receive_message).raises(StandardError)
82
83
 
83
- queueListener = QueueListener.new(@queue_url) {}
84
- queueListener.stubs(sqs: sqs)
84
+ queue_listener = QueueListener.new(@topic_id) {}
85
+ queue_listener.stubs(sqs: sqs)
86
+ queue_listener.stubs(queue_url: "http://example.com")
85
87
 
86
88
  # capture_io reasigns stderr. Assign the config.logger
87
89
  # to where capture_io has redirected it to for this test.
88
90
  out, err = capture_io do
89
91
  config.logger = $stderr
90
- queueListener.send(:read_messages)
92
+ queue_listener.send(:read_messages)
91
93
  end
92
94
  # Reassign config.logger to the correct stderr
93
95
  config.logger = $stderr
94
96
  assert_equal "Unexpected error reading from queue http://example.com\n", err
95
97
  end
96
98
 
99
+ def test_exception_from_sqs_returns_false
100
+ sqs = mock()
101
+ sqs.stubs(:receive_message).raises(StandardError)
102
+
103
+ queue_listener = QueueListener.new(@topic_id) {}
104
+ queue_listener.stubs(sqs: sqs)
105
+
106
+ refute queue_listener.send(:read_messages)
107
+ end
108
+
109
+ def test_listen_should_loop
110
+ listener = QueueListener.new(@topic_id)
111
+ listener.expects(:loop)
112
+ listener.listen
113
+ end
114
+
115
+ def test_read_messages_should_subscribe
116
+ listener = QueueListener.new(@topic_id)
117
+ QueueSubscription.expects(create: mock(queue: mock(url: {})))
118
+ listener.send(:read_messages)
119
+ end
97
120
  end
98
121
  end
@@ -1,46 +1,46 @@
1
1
  require File.expand_path('../test_helper', __FILE__)
2
2
 
3
3
  module Propono
4
- class QueueSubscriberTest < Minitest::Test
4
+ class QueueSubscriptionTest < Minitest::Test
5
5
  def test_create_topic
6
6
  topic_id = 'foobar'
7
7
  topic = Topic.new(topic_id)
8
8
  TopicCreator.expects(:find_or_create).with(topic_id).returns(topic)
9
- QueueSubscriber.subscribe(topic_id)
9
+ QueueSubscription.create(topic_id)
10
10
  end
11
11
 
12
12
  def test_sqs_create_is_called
13
13
  topic_id = "Foobar"
14
- subscriber = QueueSubscriber.new(topic_id)
14
+ subscription = QueueSubscription.new(topic_id)
15
15
 
16
16
  TopicCreator.stubs(find_or_create: Topic.new("1123"))
17
17
 
18
18
  sqs = mock()
19
- sqs.expects(:create_queue).with(subscriber.send(:queue_name)).returns(mock(body: {'QueueUrl' => Fog::AWS::SQS::Mock::QueueUrl}))
19
+ sqs.expects(:create_queue).with(subscription.send(:queue_name)).returns(mock(body: {'QueueUrl' => Fog::AWS::SQS::Mock::QueueUrl}))
20
20
  QueueCreator.any_instance.stubs(sqs: sqs)
21
21
 
22
- subscriber.subscribe
22
+ subscription.create
23
23
  end
24
24
 
25
- def test_subscriber_queue_name
25
+ def test_subscription_queue_name
26
26
  config.application_name = "MyApp"
27
27
 
28
28
  topic_id = "Foobar"
29
- subscriber = QueueSubscriber.new(topic_id)
29
+ subscription = QueueSubscription.new(topic_id)
30
30
 
31
- assert_equal subscriber.send(:queue_name), "MyApp::Foobar"
31
+ assert_equal subscription.send(:queue_name), "MyApp::Foobar"
32
32
  end
33
33
 
34
- def test_subscriber_queue_name_with_spaces
34
+ def test_subscription_queue_name_with_spaces
35
35
  config.application_name = "My App"
36
36
 
37
37
  topic_id = "Foobar"
38
- subscriber = QueueSubscriber.new(topic_id)
38
+ subscription = QueueSubscription.new(topic_id)
39
39
 
40
- assert_equal subscriber.send(:queue_name), "My_App::Foobar"
40
+ assert_equal subscription.send(:queue_name), "My_App::Foobar"
41
41
  end
42
42
 
43
- def test_subscribe_calls_subscribe
43
+ def test_create_calls_create
44
44
  arn = "arn123"
45
45
 
46
46
  TopicCreator.stubs(find_or_create: Topic.new(arn))
@@ -48,18 +48,18 @@ module Propono
48
48
 
49
49
  sns = mock()
50
50
  sns.expects(:subscribe).with(arn, Fog::AWS::SQS::Mock::QueueArn, 'sqs')
51
- subscriber = QueueSubscriber.new("Some topic")
52
- subscriber.stubs(sns: sns)
53
- subscriber.subscribe
51
+ subscription = QueueSubscription.new("Some topic")
52
+ subscription.stubs(sns: sns)
53
+ subscription.create
54
54
  end
55
55
 
56
- def test_subscribe_saves_queue
56
+ def test_create_saves_queue
57
57
  queue = Queue.new(Fog::AWS::SQS::Mock::QueueUrl)
58
58
 
59
59
  QueueCreator.expects(:find_or_create).returns(queue)
60
- subscriber = QueueSubscriber.new("Some Topic")
61
- subscriber.subscribe
62
- assert_equal queue, subscriber.queue
60
+ subscription = QueueSubscription.new("Some Topic")
61
+ subscription.create
62
+ assert_equal queue, subscription.queue
63
63
  end
64
64
  end
65
65
  end
@@ -4,16 +4,16 @@ module Propono
4
4
  class SubscriberTest < Minitest::Test
5
5
 
6
6
  def test_subscribe_by_queue_calls_queue_subscriber
7
- subscriber = QueueSubscriber.new("topic")
8
- QueueSubscriber.expects(:new).with("topic").returns(subscriber)
9
- QueueSubscriber.any_instance.expects(:subscribe)
7
+ subscriber = QueueSubscription.new("topic")
8
+ QueueSubscription.expects(:new).with("topic").returns(subscriber)
9
+ QueueSubscription.any_instance.expects(:create)
10
10
  Subscriber.subscribe_by_queue("topic")
11
11
  end
12
12
 
13
13
  def test_subscribe_by_post_calls_post_subscribe
14
- subscriber = PostSubscriber.new("topic", 'endpoint')
15
- PostSubscriber.expects(:new).with("topic", 'endpoint').returns(subscriber)
16
- PostSubscriber.any_instance.expects(:subscribe)
14
+ subscriber = PostSubscription.new("topic", 'endpoint')
15
+ PostSubscription.expects(:new).with("topic", 'endpoint').returns(subscriber)
16
+ PostSubscription.any_instance.expects(:create)
17
17
  Subscriber.subscribe_by_post("topic", "endpoint")
18
18
  end
19
19
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: propono
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - MalcyL
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-17 00:00:00.000000000 Z
12
+ date: 2013-10-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fog
@@ -97,26 +97,26 @@ files:
97
97
  - README.md
98
98
  - Rakefile
99
99
  - lib/propono.rb
100
+ - lib/propono/components/post_subscription.rb
100
101
  - lib/propono/components/queue.rb
102
+ - lib/propono/components/queue_subscription.rb
101
103
  - lib/propono/components/sns.rb
102
104
  - lib/propono/components/sqs.rb
103
105
  - lib/propono/components/topic.rb
104
106
  - lib/propono/configuration.rb
105
- - lib/propono/services/post_subscriber.rb
106
107
  - lib/propono/services/publisher.rb
107
108
  - lib/propono/services/queue_creator.rb
108
109
  - lib/propono/services/queue_listener.rb
109
- - lib/propono/services/queue_subscriber.rb
110
110
  - lib/propono/services/subscriber.rb
111
111
  - lib/propono/services/topic_creator.rb
112
112
  - lib/propono/version.rb
113
113
  - propono.gemspec
114
114
  - test/configuration_test.rb
115
- - test/post_subscriber_test.rb
115
+ - test/post_subscription_test.rb
116
116
  - test/publisher_test.rb
117
117
  - test/queue_creator_test.rb
118
118
  - test/queue_listener_test.rb
119
- - test/queue_subscriber_test.rb
119
+ - test/queue_subscription_test.rb
120
120
  - test/queue_test.rb
121
121
  - test/sns_test.rb
122
122
  - test/sqs_test.rb
@@ -150,11 +150,11 @@ specification_version: 4
150
150
  summary: General purpose pub/sub library built on top of AWS SNS and SQS
151
151
  test_files:
152
152
  - test/configuration_test.rb
153
- - test/post_subscriber_test.rb
153
+ - test/post_subscription_test.rb
154
154
  - test/publisher_test.rb
155
155
  - test/queue_creator_test.rb
156
156
  - test/queue_listener_test.rb
157
- - test/queue_subscriber_test.rb
157
+ - test/queue_subscription_test.rb
158
158
  - test/queue_test.rb
159
159
  - test/sns_test.rb
160
160
  - test/sqs_test.rb