barrister-amqp 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a9c06cedf7afd42e8b3f300be62da1abe0c3adff
4
- data.tar.gz: 4d4c8fd93a937b7d9eea6f3a7b0b83049077467a
3
+ metadata.gz: 5d666f62d0f15f879db33946493805770f7ebdfe
4
+ data.tar.gz: 08092e239d1262a62d7acfde0469794733b362b3
5
5
  SHA512:
6
- metadata.gz: 08986cbb2aaf996b1051560ec31187d1907091cf85bcc9f1fd0d1d48caaf3899e0bd585bd9fdbafd7d152be6b41d877db175a57fca15e386ddb8707a03fb6e80
7
- data.tar.gz: cba7f4b7705efb61530e0385a342c3d95d854f108ff05072f927e5119581f2ae5406137aa459bd09f44dd846cbe25a3627be2dbb2c0f28a2135a840de3bae78d
6
+ metadata.gz: b1a1b715eebd09298c9cc83be12a21761eb4005d266a02f4d739c5256d2d9e31b02169de9007fd9acf2c88db2ab2191af0ce37af224405d58388076e04402923
7
+ data.tar.gz: 5af09b06d43e9564c4fac114759f341a19a79b9e43a91bf0d772ae0538ea51a4475b99479100de5bf983739b03f58c4d22e33985670d8d5ca7436315a3343d0f
@@ -3,16 +3,31 @@ require 'bunny'
3
3
 
4
4
  module Barrister
5
5
  module Amqp
6
+ Wrapper = Struct.new(:message) do
7
+ def wrap
8
+ {
9
+ "id" => Barrister.rand_str(22),
10
+ "message" => JSON.generate(message, { :ascii_only=>true })
11
+ }
12
+ end
13
+
14
+ def unwrap
15
+ message
16
+ end
17
+ end
18
+
19
+ Config = OpenStruct.new(debug: false, wrapper: Wrapper)
20
+
6
21
  class Transport
7
22
  # NOTE: transport needs to implement request method for the Barrister::Client to
8
23
  # send requests to the server
9
24
  def initialize(service_name, options={})
10
25
  conn = Bunny.new ENV.fetch('AMQP_URL') #"Please set AMQP_URL to something like: amqp://user:password@host:port/vhost"
11
26
  conn.start
12
- @ch = conn.create_channel
13
- @service_q = @ch.queue(service_name, auto_delete: false)
14
- @reply_q = @ch.queue('', exclusive: true)
15
- @x = @ch.default_exchange
27
+ @ch = conn.create_channel
28
+ @service_q = @ch.queue(service_name, auto_delete: false)
29
+ @reply_q = @ch.queue('', exclusive: true)
30
+ @x = @ch.default_exchange
16
31
  @response_table = Hash.new { |h,k| h[k] = Queue.new }
17
32
 
18
33
  @reply_q.subscribe(block: false) do |delivery_info, properties, payload|
@@ -21,14 +36,18 @@ module Barrister
21
36
  end
22
37
 
23
38
  def request(message)
24
- @x.publish(JSON.generate(message, { :ascii_only=>true }), { correlation_id: message['id'], reply_to: @reply_q.name, routing_key: @service_q.name})
39
+ enveloppe = Config.wrapper.new(message).wrap
40
+ # NOTE message could be an array
41
+ print "[AMQP TRANSPORT --->] \n #{enveloppe} \n" if Config.debug
42
+ @x.publish(enveloppe['message'], { correlation_id: enveloppe['id'], reply_to: @reply_q.name, routing_key: @service_q.name})
25
43
 
26
- response = @response_table[message['id']].pop
27
- @response_table.delete message['id']
44
+ response = @response_table[enveloppe['id']].pop
45
+ @response_table.delete enveloppe['id']
28
46
 
29
47
  begin
30
- puts "got response: #{JSON.parse(response)}"
31
- JSON.parse(response)
48
+ JSON.parse(response).tap do |resp|
49
+ print "[AMQP TRANSPORT <---] \n #{resp} \n" if Config.debug
50
+ end
32
51
  rescue JSON::ParserError => e
33
52
  raise RpcException.new(-32000, "Bad response #{e.message}")
34
53
  end
@@ -62,7 +81,8 @@ module Barrister
62
81
 
63
82
  def start
64
83
  @service_q.subscribe(block: true) do |delivery_info, properties, payload|
65
- puts "handle #{payload}"
84
+ payload = Config.wrapper.new(payload).unwrap
85
+ puts "handling payload: #{payload}" if Config.debug
66
86
  @x.publish(@server.handle_json(payload), { routing_key: properties.reply_to, correlation_id: properties.correlation_id })
67
87
  end
68
88
  end
@@ -1,5 +1,5 @@
1
1
  module Barrister
2
2
  module Amqp
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barrister-amqp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - gregory
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-09 00:00:00.000000000 Z
11
+ date: 2016-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny