sqs2cb 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/sqs2cb/version.rb +1 -1
  3. data/lib/sqs2cb.rb +34 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 42eb91030f2a727904890a8027e0eaea92aa8af7
4
- data.tar.gz: d8d6a352eb99ce722956a45820e7042d07564cea
3
+ metadata.gz: 2d5ec1ea2850d3094fa1412c8ea4b975e76303ec
4
+ data.tar.gz: 267051c30e0c48c88d410ff5641f1a533f60864b
5
5
  SHA512:
6
- metadata.gz: 8cdcb2eb5ee09de0f7095edccb88b12e92ee181b5934cce930c939de8a458b0c9084c14374e850f746e91aacf368b3308d3fb59d8f4fcd6f1131657e634b5e06
7
- data.tar.gz: 77ce423ce584c46d093391596a69d679c9467eec467ded79f08c38ee7372921787a1f0d09b0958b51e4f7a02eddf6fd0804bc6970d4573e91d681e8ec189708d
6
+ metadata.gz: 3a08e29a4e3f6cc530392d84713f36d9319be7443bb608d1a4dc3a46dd35f2b2e10a92dcee5bebf9fea95dd14fecca5c56ad516228f5785e71500a0ab8f9b798
7
+ data.tar.gz: ecd160bf4bb2ebbd99825ce120fd8c3d649026c8e99308671ba990494dbcf21072ccf82bcb6245aecb98367548c62c0647f6fa6b31e529b82acd3c9d0b9c9cca
@@ -1,3 +1,3 @@
1
1
  module Sqs2cb
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/sqs2cb.rb CHANGED
@@ -3,6 +3,7 @@ require 'aws-sdk-v1'
3
3
  require 'json'
4
4
  require 'rest_client'
5
5
  require 'logger'
6
+ require 'awesome_print'
6
7
 
7
8
  module Sqs2cb
8
9
  class MessageHandler
@@ -12,6 +13,7 @@ module Sqs2cb
12
13
  @caseBlocksAPIEndpoint = ENV['CB_API_ENDPOINT']
13
14
  @caseBlocksAPIToken = ENV['CB_API_TOKEN']
14
15
  @caseBlocksQueue = ENV['CB_QUEUE']
16
+ @usePatchCommand = (@caseBlocksQueue =~ /patch/)
15
17
  logfile = ENV['SQS2CB_LOGFILE_PATH'].nil? ? STDOUT : File.open(ENV['SQS2CB_LOGFILE_PATH'], 'a')
16
18
  @logger = Logger.new logfile
17
19
  @stop = false
@@ -45,7 +47,11 @@ module Sqs2cb
45
47
  def handle_received_message(message)
46
48
  @logger.info "Message received."
47
49
  @logger.debug message.body
48
- send_to_caseblocks(JSON.parse(message.body))
50
+ if @usePatchCommand
51
+ update_caseblocks(JSON.parse(message.body))
52
+ else
53
+ send_to_caseblocks(JSON.parse(message.body))
54
+ end
49
55
  rescue JSON::ParserError => ex
50
56
  # TODO needs some form of remote notification that an error occured.
51
57
  @logger.error "Bad message format. Unable to deserialize message into JSON."
@@ -55,6 +61,33 @@ module Sqs2cb
55
61
  @logger.error ex
56
62
  end
57
63
 
64
+ def update_caseblocks(msgHash)
65
+ caseId = msgHash.delete('id')
66
+ caseTypeCode = msgHash.delete('case_type_code')
67
+ initialResponse = RestClient.get("#{@caseBlocksAPIEndpoint}/case_blocks/#{caseTypeCode}/#{caseId}.json?auth_token=#{@caseBlocksAPIToken}",
68
+ :content_type => :json,
69
+ :accept => :json,
70
+ "AUTH_TOKEN" => @caseBlocksAPIToken)
71
+ currentCase = JSON.parse(initialResponse.body)
72
+ data = currentCase[caseTypeCode]
73
+ data.merge!(msgHash)
74
+ %w(tasklists conversations).each {|k| data.delete k}
75
+ currentCase[caseTypeCode] = data
76
+
77
+ secondResponse = RestClient.put("#{@caseBlocksAPIEndpoint}/case_blocks/#{caseTypeCode}/#{caseId}.json?auth_token=#{@caseBlocksAPIToken}", currentCase.to_json,
78
+ :content_type => :json,
79
+ :accept => :json,
80
+ "AUTH_TOKEN" => @caseBlocksAPIToken)
81
+ @logger.info "Received #{secondResponse.code}"
82
+ rescue RestClient::ExceptionWithResponse => ex
83
+ if ex.response.nil?
84
+ @logger.error "Received a bad response (null) from CaseBlocks. Re-raising."
85
+ raise ex
86
+ else
87
+ @logger.error "Received #{ex.response.code} bad response: #{ex.response.body}"
88
+ end
89
+ end
90
+
58
91
  def send_to_caseblocks(msgHash)
59
92
  @logger.info "Sending to CaseBlocks"
60
93
  response = RestClient.post("#{@caseBlocksAPIEndpoint}/case_blocks/cases?auth_token=#{@caseBlocksAPIToken}", msgHash.to_json,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqs2cb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - EmergeAdapt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-07 00:00:00.000000000 Z
11
+ date: 2015-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler