harmony-service 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb896371b665b87c0a744f49a48c24f5a097a2cc
4
- data.tar.gz: dfb996bc17cccf72310a8e6485edcbe492dd62ff
3
+ metadata.gz: 33baa1a1af30cb9c400ff84ee9a8db3f6e2b2321
4
+ data.tar.gz: 25faf44981c956a92b05499f405b74b4bfdcf6a1
5
5
  SHA512:
6
- metadata.gz: 1a6562346a73bdb7aae171748f830c59ea8bf2fad3ba8d9c023c1def7ed66ffabbc9fef327ffab63c035ddb0028c604d7beea819bf15c7d454eb2fb5b8341914
7
- data.tar.gz: 2c9bb8223a9bde4d46c92c6b8344de2fecd25afaab3f8d612b5ec2b6ae4b65a9fa2897ddb95d5bcc029f82764895abea5f2d60541b625f4b1893a1771f1fc41c
6
+ metadata.gz: ebe90d52a3b7d840a5c8c579d1954e9b84120fa641b17a8499425ef4ea051986ea1a82525fb9984e71935ecb97d74e714964318e947ef4e69123690ddf1294ce
7
+ data.tar.gz: 9deb6c4a0109d4eb0a0d0e4cbec171b04e8fa9b019c99d8e7093ebcdaadb5521aa57c520cccdc6d68d44e3dfe559b3919b509fc64055c1ee9fd2570b44a3b2bd
@@ -1,3 +1,3 @@
1
1
  class Harmony::Service::ErrorResponse < Harmony::Service::Response
2
- attr_accessor :message, :detailed_message
2
+ attr_accessor :message, :exception
3
3
  end
@@ -21,21 +21,14 @@ module Harmony
21
21
  result = new_handler.work_with_request(request)
22
22
  raise "Unacceptable response class: #{result.class}" unless response_classes.include?(result.class)
23
23
 
24
- json = Oj.dump(result)
25
- logger.debug "Response: #{json}"
26
- send_response(json, metadata.reply_to, metadata.correlation_id)
24
+ send_response(result, metadata)
27
25
  ack!
28
26
  rescue StandardError => error
29
27
  logger.error error.message
30
28
  logger.error error.backtrace.join("\n")
31
29
 
32
- error_response = ErrorResponse.new
33
- error_response.message = "An error occurred."
34
- error_response.detailed_message = error.message
35
- json = Oj.dump(error_response)
36
- logger.debug "Response: #{json}"
37
-
38
- send_response(json, metadata.reply_to, metadata.correlation_id)
30
+ error_response = ErrorResponse.new message: error.message, exception: error
31
+ send_response(error_response, metadata)
39
32
  reject!
40
33
  end
41
34
  end
@@ -66,11 +59,19 @@ module Harmony
66
59
  ch.exchange(AMQ::Protocol::EMPTY_STRING, :auto_delete => true)
67
60
  end
68
61
 
69
- def send_response(result, reply_to, correlation_id)
70
- reply_to_exchange.publish(result, :routing_key => reply_to, :correlation_id => correlation_id)
71
- end
72
62
 
73
63
  private
64
+ def send_response(result, metadata)
65
+ json = Oj.dump(result)
66
+ logger.debug "Response: #{json}"
67
+ send_response_json(json, metadata.reply_to, metadata.correlation_id)
68
+ end
69
+
70
+ def send_response_json(json, routing_key, correlation_id)
71
+ reply_to_exchange.publish(json, :routing_key => routing_key, :correlation_id => correlation_id)
72
+ end
73
+
74
+
74
75
  def request_response_mapping
75
76
  {
76
77
  Calculator::Request => [Calculator::Response],
@@ -1,6 +1,6 @@
1
1
  module Harmony
2
2
  module Service
3
- VERSION = "0.4.1"
3
+ VERSION = "0.4.2"
4
4
  end
5
5
  end
6
6
 
@@ -8,10 +8,11 @@ describe Harmony::Service::RpcService do
8
8
 
9
9
  context "ack!" do
10
10
  before(:each) do
11
- expect(handler).to receive(:work_with_request).with(kind_of(request.class)) { response }
11
+ allow(handler).to receive(:work_with_request).with(kind_of(request.class)) { response }
12
12
  allow(subject).to receive(:new_handler) { handler }
13
- allow(subject).to receive(:send_response)
14
- allow(subject).to receive(:ack!)
13
+ allow(subject).to receive(:send_response_json)
14
+ allow(subject).to receive(:ack!)
15
+ allow(subject).to receive(:reject!)
15
16
  subject.work_with_params(Oj.dump(request), {}, metadata)
16
17
  end
17
18
 
@@ -19,7 +20,7 @@ describe Harmony::Service::RpcService do
19
20
  let(:request) { Harmony::Service::Calculator::Request.new(harmony_user_email: "matt@futureworkshops.com", inputs: {"lat" => "51.0", "lon" => "0.1"}) }
20
21
  let(:response) { Harmony::Service::Calculator::Response.new(outputs: {price: 50})}
21
22
 
22
- it { expect(subject).to have_received(:send_response).with("{\"^o\":\"Harmony::Service::Calculator::Response\",\"outputs\":{\":price\":50}}", "harmony.trello", "abc123") }
23
+ it { expect(subject).to have_received(:send_response_json).with("{\"^o\":\"Harmony::Service::Calculator::Response\",\"outputs\":{\":price\":50}}", "harmony.trello", "abc123", any_args) }
23
24
  it { expect(subject).to have_received(:ack!) }
24
25
  end
25
26
 
@@ -28,7 +29,7 @@ describe Harmony::Service::RpcService do
28
29
  let(:request) { Harmony::Service::ActionList::ListRequest.new(harmony_user_email: "matt@futureworkshops.com", page: 0, per_page: 10) }
29
30
  let(:response) { [Harmony::Service::ActionList::Item.new({id: 1, title: "Carrots"})]}
30
31
 
31
- it { expect(subject).to have_received(:send_response).with("[{\"^o\":\"Harmony::Service::ActionList::Item\",\"id\":1,\"title\":\"Carrots\"}]", "harmony.trello", "abc123") }
32
+ it { expect(subject).to have_received(:send_response_json).with("[{\"^o\":\"Harmony::Service::ActionList::Item\",\"id\":1,\"title\":\"Carrots\"}]", "harmony.trello", "abc123") }
32
33
  it { expect(subject).to have_received(:ack!) }
33
34
  end
34
35
 
@@ -36,7 +37,7 @@ describe Harmony::Service::RpcService do
36
37
  let(:request) { Harmony::Service::ActionList::ItemRequest.new(harmony_user_email: "matt@futureworkshops.com", id: 1) }
37
38
  let(:response) { Harmony::Service::ActionList::Item.new({id: 1, title: "Carrots", subtitle: "Bag of Carrots", detail_html: "<html><body>Carrots</body></html>"})}
38
39
 
39
- it { expect(subject).to have_received(:send_response).with("{\"^o\":\"Harmony::Service::ActionList::Item\",\"id\":1,\"title\":\"Carrots\",\"subtitle\":\"Bag of Carrots\",\"detail_html\":\"<html><body>Carrots</body></html>\"}", "harmony.trello", "abc123") }
40
+ it { expect(subject).to have_received(:send_response_json).with("{\"^o\":\"Harmony::Service::ActionList::Item\",\"id\":1,\"title\":\"Carrots\",\"subtitle\":\"Bag of Carrots\",\"detail_html\":\"<html><body>Carrots</body></html>\"}", "harmony.trello", "abc123") }
40
41
  it { expect(subject).to have_received(:ack!) }
41
42
  end
42
43
 
@@ -44,7 +45,7 @@ describe Harmony::Service::RpcService do
44
45
  let(:request) { Harmony::Service::ActionList::ActionRequest.new(harmony_user_email: "matt@futureworkshops.com", id: 1, action: "Done") }
45
46
  let(:response) { Harmony::Service::Response.new }
46
47
 
47
- it { expect(subject).to have_received(:send_response).with("{\"^o\":\"Harmony::Service::Response\"}", "harmony.trello", "abc123") }
48
+ it { expect(subject).to have_received(:send_response_json).with("{\"^o\":\"Harmony::Service::Response\"}", "harmony.trello", "abc123") }
48
49
  it { expect(subject).to have_received(:ack!) }
49
50
  end
50
51
  end
@@ -52,7 +53,7 @@ describe Harmony::Service::RpcService do
52
53
  context "chart" do
53
54
  let(:request) { Harmony::Service::Chart::Request.new(harmony_user_email: "matt@futureworkshops.com") }
54
55
  let(:response) { Harmony::Service::Chart::Response.new(x_values: ["Jan", "Feb", "Mar"], y_values: [10, 20, 40])}
55
- it { expect(subject).to have_received(:send_response).with("{\"^o\":\"Harmony::Service::Chart::Response\",\"x_values\":[\"Jan\",\"Feb\",\"Mar\"],\"y_values\":[10,20,40]}", "harmony.trello", "abc123") }
56
+ it { expect(subject).to have_received(:send_response_json).with("{\"^o\":\"Harmony::Service::Chart::Response\",\"x_values\":[\"Jan\",\"Feb\",\"Mar\"],\"y_values\":[10,20,40]}", "harmony.trello", "abc123") }
56
57
  it { expect(subject).to have_received(:ack!) }
57
58
  end
58
59
 
@@ -60,7 +61,7 @@ describe Harmony::Service::RpcService do
60
61
  context 'get' do
61
62
  let(:request) { Harmony::Service::Form::GetRequest.new(harmony_user_email: "matt@futureworkshops.com", inputs: ['satisfaction_level']) }
62
63
  let(:response) { Harmony::Service::Form::GetResponse.new({input_values: [{'satisfaction_level': ['1','2','3']}]})}
63
- it { expect(subject).to have_received(:send_response).with("{\"^o\":\"Harmony::Service::Form::GetResponse\",\"input_values\":[{\":satisfaction_level\":[\"1\",\"2\",\"3\"]}],\"options\":{}}", "harmony.trello", "abc123") }
64
+ it { expect(subject).to have_received(:send_response_json).with("{\"^o\":\"Harmony::Service::Form::GetResponse\",\"input_values\":[{\":satisfaction_level\":[\"1\",\"2\",\"3\"]}],\"options\":{}}", "harmony.trello", "abc123") }
64
65
  it { expect(subject).to have_received(:ack!) }
65
66
  end
66
67
  end
@@ -68,37 +69,50 @@ describe Harmony::Service::RpcService do
68
69
  context "flow ended" do
69
70
  let(:request) { Harmony::Service::Flow::EndedRequest.new(harmony_user_email: "matt@futureworkshops.com", pages: [{page_id: 1}]) }
70
71
  let(:response) { Harmony::Service::Response.new }
71
- it { expect(subject).to have_received(:send_response).with("{\"^o\":\"Harmony::Service::Response\"}", "harmony.trello", "abc123") }
72
+ it { expect(subject).to have_received(:send_response_json).with("{\"^o\":\"Harmony::Service::Response\"}", "harmony.trello", "abc123") }
72
73
  it { expect(subject).to have_received(:ack!) }
73
74
  end
74
75
 
75
76
  context "app notification" do
76
77
  let(:request) { Harmony::Service::Notification::Request.new }
77
78
  let(:response) { Harmony::Service::Notification::AppResponse.new(text: 'Check out the new data')}
78
- it { expect(subject).to have_received(:send_response).with("{\"^o\":\"Harmony::Service::Notification::AppResponse\",\"text\":\"Check out the new data\"}", "harmony.trello", "abc123") }
79
+ it { expect(subject).to have_received(:send_response_json).with("{\"^o\":\"Harmony::Service::Notification::AppResponse\",\"text\":\"Check out the new data\"}", "harmony.trello", "abc123") }
79
80
  it { expect(subject).to have_received(:ack!) }
80
81
  end
81
82
 
82
83
  context "attribute names" do
83
84
  let(:request) { Harmony::Service::AttributeNamesRequest.new }
84
85
  let(:response) { Harmony::Service::AttributeNamesResponse.new}
85
- it { expect(subject).to have_received(:send_response).with("{\"^o\":\"Harmony::Service::AttributeNamesResponse\",\"options\":{}}", "harmony.trello", "abc123") }
86
+ it { expect(subject).to have_received(:send_response_json).with("{\"^o\":\"Harmony::Service::AttributeNamesResponse\",\"options\":{}}", "harmony.trello", "abc123") }
86
87
  it { expect(subject).to have_received(:ack!) }
87
88
  end
88
89
 
90
+ context "exception" do
91
+ let(:handler) do
92
+ handler = double("Object")
93
+ expect(handler).to receive(:work_with_request).and_raise("Could not complete.")
94
+ handler
95
+ end
96
+
97
+ let(:request) { Harmony::Service::Notification::Request.new }
98
+
99
+ it { expect(subject).to have_received(:send_response_json).with(/Could not complete/,"harmony.trello", "abc123") }
100
+ it { expect(subject).to have_received(:reject!) }
101
+ end
102
+
89
103
  end
90
104
 
91
105
  context "unacceptable request class" do
92
106
  before(:each) do
93
107
  allow(subject).to receive(:work_with_request).and_raise("A timeout occured")
94
- allow(subject).to receive(:send_response)
108
+ allow(subject).to receive(:send_response_json)
95
109
  allow(subject).to receive(:reject!)
96
110
 
97
111
  metadata = instance_double("Metadata", reply_to: "harmony.trello", correlation_id: "abc123")
98
112
  subject.work_with_params("{\"trello_board_id\": \"12345\"}", {}, metadata)
99
113
  end
100
114
 
101
- it { expect(subject).to have_received(:send_response).with("{\"^o\":\"Harmony::Service::ErrorResponse\",\"message\":\"An error occurred.\",\"detailed_message\":\"Unacceptable request class: Hash\"}", "harmony.trello", "abc123") }
115
+ it { expect(subject).to have_received(:send_response_json).with(/Unacceptable request class: Hash/,"harmony.trello", "abc123") }
102
116
  it { expect(subject).to have_received(:reject!) }
103
117
  end
104
118
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: harmony-service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Brooke-Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-25 00:00:00.000000000 Z
11
+ date: 2017-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sneakers