Skip to content
Browse files

Rationalise listeners.

  • Loading branch information...
1 parent c8cb77b commit 8ab5ad913bc150807e5c844f9f66333c0a4dfcc7 @threedaymonk threedaymonk committed Apr 26, 2012
View
38 lib/abstract_listener.rb
@@ -0,0 +1,38 @@
+class AbstractListener
+
+ def initialize(messenger = Messenger.instance.client, logger = Rails.logger)
+ @messenger = messenger
+ @logger = logger
+ end
+ attr_reader :messenger, :logger
+
+ def listen
+ Signal.trap('TERM') do
+ client.close
+ exit
+ end
+
+ self.class.listeners.each do |messages, handler|
+ messenger.when *messages do |message|
+ logger.info "Received message #{message}"
+ begin
+ handler.call(message, logger)
+ rescue => e
+ logger.error "Unable to process message #{message}"
+ logger.error [e.message, e.backtrace].flatten.join("\n")
+ end
+ logger.info "Finished processing message #{message}"
+ end
+ end
+
+ messenger.join
+ end
+
+ def self.listen(*messages, &handler)
+ listeners << [messages, handler]
+ end
+
+ def self.listeners
+ @listeners ||= []
+ end
+end
View
49 lib/contact_listener.rb
@@ -1,45 +1,16 @@
-class ContactListener
- def process(message)
- logger.info "Received message #{message}"
+require "abstract_listener"
- begin
- logger.info "Processing contact #{message['id']}"
- yield Contact.find_or_initialize_by_contactotron_id(message['id'])
- rescue => e
- logger.error "Unable to process message #{message}"
- logger.error [e.message, e.backtrace].flatten.join("\n")
- end
+class ContactListener < AbstractListener
- logger.info "Finished processing message #{message}"
+ listen 'contactotron', '*', 'created' do |message, logger|
+ logger.info "Creating contact #{message['id']}"
+ contact = Contact.find_or_initialize_by_contactotron_id(message['id'])
+ contact.update_from_contactotron
end
- def listen
- Signal.trap 'TERM' do
- client.close
- exit
- end
-
- marples = Messenger.instance.client
-
- marples.when 'contactotron', '*', 'created' do |message|
- process message do |contact|
- logger.info "Creating contact #{message['id']}"
- contact.update_from_contactotron
- end
- end
-
- marples.when 'contactotron', '*', 'updated' do |message|
- process message do |contact|
- logger.info "Updating contact #{message['id']}"
- contact.update_from_contactotron
- end
- end
-
- logger.info 'Listening for created and updated contacts in Contact-o-tron'
- marples.join
- end
-
- def logger
- @logger ||= Logger.new(STDOUT).tap { |logger| logger.level = Logger::DEBUG }
+ listen 'contactotron', '*', 'updated' do |message, logger|
+ logger.info "Updating contact #{message['id']}"
+ contact = Contact.find_or_initialize_by_contactotron_id(message['id'])
+ contact.update_from_contactotron
end
end
View
41 lib/deleted_publication_listener.rb
@@ -1,36 +1,13 @@
-class DeletedPublicationListener
- def listen
- Signal.trap('TERM') do
- client.close
- exit
- end
+require "abstract_listener"
- marples = Messenger.instance.client
+class DeletedPublicationListener < AbstractListener
- marples.when 'publisher', '*', 'destroyed' do |publication|
- logger.info "Found publication #{publication}"
-
- begin
- logger.info "Processing artefact #{publication['panopticon_id']}"
-
- artefact = Artefact.find(publication['panopticon_id'])
- logger.info 'Getting need ID from Panopticon'
-
- artefact.destroy
- logger.info 'Artefact destroyed'
- rescue => e
- logger.error "Unable to process message #{publication}"
- logger.error [e.message, e.backtrace].flatten.join("\n")
- end
-
- logger.info "Finished processing message #{publication}"
- end
-
- logger.info 'Listening for deleted objects in Publisher'
- marples.join
- end
-
- def logger
- @logger ||= Logger.new(STDOUT)#.tap { |logger| logger.level = Logger::DEBUG }
+ listen 'publisher', '*', 'destroyed' do |publication, logger|
+ logger.info "Found publication #{publication}"
+ logger.info "Processing artefact #{publication['panopticon_id']}"
+ artefact = Artefact.find(publication['panopticon_id'])
+ logger.info 'Getting need ID from Panopticon'
+ artefact.destroy
+ logger.info 'Artefact destroyed'
end
end
View
58 lib/need_listener.rb
@@ -1,50 +1,22 @@
-class NeedListener
+require "abstract_listener"
- def initialize
- @marples = Messenger.instance.client
- end
-
- def listen
- Signal.trap('TERM') do
- client.close
- exit
- end
-
- listen_on_updated
- @marples.join
- end
-
- def listen_on_updated
- @marples.when 'need-o-tron', 'needs', 'updated' do |need|
- logger.info "Found need #{need}"
+class NeedListener < AbstractListener
- begin
- logger.info "Processing need `#{need['title']}`"
+ listen 'need-o-tron', 'needs', 'updated' do |need, logger|
+ logger.info "Found need #{need}"
+ logger.info "Processing need `#{need['title']}`"
- artefact = Artefact.find_by_need_id(need['id'])
- logger.info "Found artefact `#{artefact.name}` in Panopticon"
+ artefact = Artefact.find_by_need_id(need['id'])
+ logger.info "Found artefact `#{artefact.name}` in Panopticon"
- require 'gds_api/needotron'
- api = GdsApi::Needotron.new(Plek.current.environment)
- need_data = api.need_by_id(need['id'])
- logger.info "Getting need information from `#{need['id']}`"
-
- artefact.department = need_data.writing_team.name rescue nil
- artefact.fact_checkers = need_data.fact_checkers.collect{ |e| e.fact_checker.email }.join(', ')
- artefact.save!
- logger.info "----> Saved `#{artefact.name}` with department `#{artefact.department}` and fact checkers `#{artefact.fact_checkers}`"
- rescue => e
- logger.error "Unable to process message #{need}"
- logger.error [e.message, e.backtrace].flatten.join("\n")
- end
-
- logger.info "Finished processing `#{need}`"
- end
-
- logger.info 'Listening for updated objects in Needotron'
- end
+ require 'gds_api/needotron'
+ api = GdsApi::Needotron.new(Plek.current.environment)
+ need_data = api.need_by_id(need['id'])
+ logger.info "Getting need information from `#{need['id']}`"
- def logger
- @logger ||= Rails.logger
+ artefact.department = need_data.writing_team.name rescue nil
+ artefact.fact_checkers = need_data.fact_checkers.collect{ |e| e.fact_checker.email }.join(', ')
+ artefact.save!
+ logger.info "----> Saved `#{artefact.name}` with department `#{artefact.department}` and fact checkers `#{artefact.fact_checkers}`"
end
end
View
6 test/unit/contact_listener_test.rb
@@ -14,26 +14,24 @@ def mock_messenger
messenger = mock_messenger
messenger.expects(:when).with("contactotron", "*", "created").yields(message)
- Messenger.instance.stubs(:client).returns(messenger)
contact = mock("contact")
contact.expects(:update_from_contactotron)
Contact.expects(:find_or_initialize_by_contactotron_id).with(1234).returns(contact)
- ContactListener.new.listen
+ ContactListener.new(messenger, stub_everything).listen
end
test "should update a contact" do
message = {"id" => 1234}
messenger = mock_messenger
messenger.expects(:when).with("contactotron", "*", "updated").yields(message)
- Messenger.instance.stubs(:client).returns(messenger)
contact = mock("contact")
contact.expects(:update_from_contactotron)
Contact.expects(:find_or_initialize_by_contactotron_id).with(1234).returns(contact)
- ContactListener.new.listen
+ ContactListener.new(messenger, stub_everything).listen
end
end
View
4 test/unit/deleted_publication_listener_test.rb
@@ -14,12 +14,12 @@ def mock_messenger
messenger = mock_messenger
messenger.expects(:when).with("publisher", "*", "destroyed").yields(message)
- Messenger.instance.stubs(:client).returns(messenger)
artefact = Artefact.new
artefact.expects(:destroy)
Artefact.expects(:find).with(1234).returns(artefact)
- DeletedPublicationListener.new.listen
+ DeletedPublicationListener.new(messenger, stub_everything).listen
end
end
+
View
3 test/unit/need_listener_test.rb
@@ -15,7 +15,6 @@ def mock_messenger
messenger = mock_messenger
messenger.expects(:when).with("need-o-tron", "needs", "updated").yields(message)
- Messenger.instance.stubs(:client).returns(messenger)
artefact = Artefact.new
artefact.expects(:save!)
@@ -32,7 +31,7 @@ def mock_messenger
api.expects(:need_by_id).with(1234).returns(need_data)
GdsApi::Needotron.expects(:new).returns(api)
- NeedListener.new.listen
+ NeedListener.new(messenger, stub_everything).listen
assert_equal "WRITING TEAM NAME", artefact.department
assert_equal "A, B", artefact.fact_checkers

0 comments on commit 8ab5ad9

Please sign in to comment.
Something went wrong with that request. Please try again.