Class: Sqs2cb::MessageHandler
- Inherits:
-
Object
- Object
- Sqs2cb::MessageHandler
- Defined in:
- lib/sqs2cb.rb
Instance Method Summary collapse
- #handle_received_message(message) ⇒ Object
-
#initialize ⇒ MessageHandler
constructor
A new instance of MessageHandler.
- #send_to_caseblocks(msgHash) ⇒ Object
- #stop ⇒ Object
- #transfer_messages ⇒ Object
- #update_caseblocks(msgHash) ⇒ Object
Constructor Details
#initialize ⇒ MessageHandler
Returns a new instance of MessageHandler.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/sqs2cb.rb', line 9 def initialize @awsAccessKeyId = ENV['AWS_ACCESS_KEY'] @awsSecretAccessKey = ENV['AWS_SECRET_ACCESS_KEY'] @caseBlocksAPIEndpoint = ENV['CB_API_ENDPOINT'] @caseBlocksAPIToken = ENV['CB_API_TOKEN'] @caseBlocksQueue = ENV['CB_QUEUE'] @usePatchCommand = (@caseBlocksQueue =~ /patch/) logfile = ENV['SQS2CB_LOGFILE_PATH'].nil? ? STDOUT : File.open(ENV['SQS2CB_LOGFILE_PATH'], 'a') logfile.sync = true @logger = Logger.new logfile @stop = false @options = {:use_patch_command => @usePatchCommand, :case_blocks_api_endpoint => @caseBlocksAPIEndpoint, :caseblocks_api_token => @caseBlocksAPIToken} raise RuntimeError, "Please ensure AWS_ACCESS_KEY is set." if @awsAccessKeyId.nil? or @awsAccessKeyId.empty? raise RuntimeError, "Please ensure AWS_SECRET_ACCESS_KEY is set." if @awsSecretAccessKey.nil? or @awsSecretAccessKey.empty? raise RuntimeError, "Please ensure CB_API_ENDPOINT is set." if @caseBlocksAPIEndpoint.nil? or @caseBlocksAPIEndpoint.empty? raise RuntimeError, "Please ensure CB_API_TOKEN is set." if @caseBlocksAPIToken.nil? or @caseBlocksAPIToken.empty? @logger.info "MessageHandler ready to transfer messages." end |
Instance Method Details
#handle_received_message(message) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/sqs2cb.rb', line 53 def () @logger.info "Message received." @logger.debug .body if @usePatchCommand update_caseblocks(JSON.parse(.body)) else send_to_caseblocks(JSON.parse(.body)) end rescue JSON::ParserError => ex # TODO needs some form of remote notification that an error occured. @logger.error "Bad message format. Unable to deserialize message into JSON." @logger.error ex rescue Exception => ex @logger.error "Something caused a message handling failure." @logger.error ex end |
#send_to_caseblocks(msgHash) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/sqs2cb.rb', line 97 def send_to_caseblocks(msgHash) @logger.info "Sending to CaseBlocks" response = RestClient.post(add_auth(Uri.parse("#{@caseBlocksAPIEndpoint}/case_blocks/cases")), msgHash.to_json, :content_type => :json, :accept => :json, "AUTH_TOKEN" => @caseBlocksAPIToken) @logger.info "Received #{response.code} Location: #{response.headers[:location]}" rescue RestClient::ExceptionWithResponse => ex if ex.response.nil? @logger.error "Received a bad response (null) from CaseBlocks. Re-raising." raise ex else @logger.error "Received #{ex.response.code} bad response: #{ex.response.body}" end end |
#stop ⇒ Object
31 32 33 |
# File 'lib/sqs2cb.rb', line 31 def stop @stop = true end |
#transfer_messages ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sqs2cb.rb', line 35 def @logger.info "MessageHandler awaiting first message." begin sqs = AWS::SQS.new(region: 'us-east-1', access_key_id: @awsAccessKeyId, secret_access_key: @awsSecretAccessKey) sqs.queues[@caseBlocksQueue].poll do || if block_given? yield , @options else () end return if @stop end rescue Exception => ex @logger.error "Something cause an SQS failure." @logger.error ex end end |
#update_caseblocks(msgHash) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/sqs2cb.rb', line 70 def update_caseblocks(msgHash) caseId = msgHash.delete('id') caseTypeCode = msgHash.delete('case_type_code') initialResponse = RestClient.get("#{@caseBlocksAPIEndpoint}/case_blocks/#{caseTypeCode}/#{caseId}.json?auth_token=#{@caseBlocksAPIToken}", :content_type => :json, :accept => :json, "AUTH_TOKEN" => @caseBlocksAPIToken) currentCase = JSON.parse(initialResponse.body) data = currentCase[caseTypeCode] data.merge!(msgHash) %w(tasklists conversations).each {|k| data.delete k} currentCase[caseTypeCode] = data secondResponse = RestClient.put("#{@caseBlocksAPIEndpoint}/case_blocks/#{caseTypeCode}/#{caseId}.json?auth_token=#{@caseBlocksAPIToken}", currentCase.to_json, :content_type => :json, :accept => :json, "AUTH_TOKEN" => @caseBlocksAPIToken) @logger.info "Received #{secondResponse.code}" rescue RestClient::ExceptionWithResponse => ex if ex.response.nil? @logger.error "Received a bad response (null) from CaseBlocks. Re-raising." raise ex else @logger.error "Received #{ex.response.code} bad response: #{ex.response.body}" end end |