klomp 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ Changes
2
+ --------------------------------------------------------------------------------
3
+
4
+ 0.0.2 (2012/06/15)
5
+ ================================================================================
6
+
7
+ - Add `:logger` option to log sending and receiving of messages
8
+ - Duck-type with `#to_json` for `:translate_json` option
9
+ - Clean up options parsing, pass all options through to OnStomp client config
10
+ - Clamp onstomp gem dependency version to 1.0.x
11
+
12
+ 0.0.1 (2012/06/08)
13
+ ================================================================================
14
+
15
+ - Initial release
data/README.md CHANGED
@@ -16,7 +16,8 @@ left behind.
16
16
  Ruby and JSON objects.
17
17
 
18
18
  * If a reply-to header is found in a message, a response is automatically
19
- sent to the reply-to destination.
19
+ sent to the reply-to destination. The response will be the return value of the
20
+ subscribe block.
20
21
 
21
22
  ## Installation
22
23
 
@@ -35,6 +36,31 @@ manages connections. For example, while the `connected?` method normally
35
36
  returns a boolean value, Klomp's `connected?` will return an array of booleans
36
37
  (i.e. one result for each broker).
37
38
 
39
+ ### Additional options for Klomp::Client
40
+
41
+ <table>
42
+ <tr>
43
+ <th>Key</th>
44
+ <th>Default value</th>
45
+ <th>Description</th>
46
+ </tr>
47
+ <tr>
48
+ <td>:translate_json</td>
49
+ <td>true</td>
50
+ <td>Translate message bodies between native Ruby and JSON objects?</td>
51
+ </tr>
52
+ <tr>
53
+ <td>:auto_reply_to</td>
54
+ <td>true</td>
55
+ <td>Automatically send response to reply-to destination?</td>
56
+ </tr>
57
+ <tr>
58
+ <td>:logger</td>
59
+ <td>false</td>
60
+ <td>Logger object</td>
61
+ </tr>
62
+ </table>
63
+
38
64
  ## Developers
39
65
 
40
66
  Set up the environment using `bundle install`. Note that the tests currently
@@ -15,6 +15,6 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Klomp::VERSION
17
17
 
18
- gem.add_dependency("onstomp")
18
+ gem.add_dependency("onstomp", "~> 1.0.7")
19
19
  gem.add_dependency("json")
20
20
  end
@@ -1,9 +1,5 @@
1
- require 'onstomp'
2
- require 'onstomp/failover'
3
- require 'json'
4
-
5
1
  require 'klomp/client'
6
2
 
7
3
  module Klomp
8
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
9
5
  end
@@ -1,44 +1,53 @@
1
+ require 'onstomp'
2
+ require 'onstomp/failover'
3
+ require 'json'
4
+ require 'logger'
5
+
1
6
  module Klomp
2
7
 
3
8
  class Client
4
- attr_reader :options, :read_conn, :write_conn
9
+ attr_reader :read_conn, :write_conn
5
10
 
6
11
  def initialize(uri, options={})
7
- @options ||= {
8
- :translate_json => true,
9
- :auto_reply_to => true
10
- }
12
+ @translate_json = options.fetch(:translate_json, true)
13
+ @auto_reply_to = options.fetch(:auto_reply_to, true)
14
+ @logger = options.fetch(:logger, nil)
11
15
 
12
- ofc_options = options.inject({
13
- :retry_attempts => -1,
14
- :retry_delay => 1
15
- }) { |memo,(k,v)| memo.merge({k => v}) if memo.has_key?(k) }
16
+ # defaults for retry delay and attempts
17
+ options[:retry_delay] ||= 2
18
+ options[:retry_attempts] ||= 10
16
19
 
17
20
  if uri.is_a?(Array)
18
- @write_conn = OnStomp::Failover::Client.new(uri, ofc_options)
19
- @read_conn = uri.inject([]) { |memo,obj| memo + [OnStomp::Failover::Client.new([obj], ofc_options)] }
21
+ @write_conn = OnStomp::Failover::Client.new(uri, options)
22
+ @read_conn = uri.map {|obj| OnStomp::Failover::Client.new([obj], options) }
20
23
  else
21
- @write_conn = OnStomp::Failover::Client.new([uri], ofc_options)
24
+ @write_conn = OnStomp::Failover::Client.new([uri], options)
22
25
  @read_conn = [@write_conn]
23
26
  end
27
+ @all_conn = ([@write_conn] + @read_conn).uniq
28
+ configure_connections
24
29
  end
25
30
 
26
31
  def send(*args, &block)
27
- if @options[:translate_json] && [Array, Hash].any? { |type| args[1].kind_of?(type) }
32
+ if @translate_json && args[1].respond_to?(:to_json)
28
33
  args[1] = args[1].to_json
29
- args[2] = {} if args[2].nil?
34
+ args[2] ||= {}
30
35
  args[2][:'content-type'] = 'application/json'
31
36
  else
32
37
  args[1] = args[1].to_s
33
38
  end
39
+ log.info("[Sending] Destination=#{args[0]} Body=#{args[1]} Headers=#{args[2]}") if log
34
40
  @write_conn.send(*args, &block)
35
41
  end
42
+ alias puts send
43
+ alias publish send
36
44
 
37
45
  def subscribe(*args, &block)
38
46
  frames = []
39
47
  @read_conn.each do |c|
40
48
  frames << c.subscribe(*args) do |msg|
41
- if @options[:translate_json]
49
+ log.info("[Received] Body=#{msg.body} Headers=#{msg.headers.to_hash.sort}") if log
50
+ if @translate_json
42
51
  msg.body = begin
43
52
  JSON.parse(msg.body)
44
53
  rescue JSON::ParserError
@@ -46,7 +55,7 @@ module Klomp
46
55
  end
47
56
  end
48
57
  reply_args = yield msg
49
- if @options[:auto_reply_to] && !msg.headers[:'reply-to'].nil?
58
+ if @auto_reply_to && !msg.headers[:'reply-to'].nil?
50
59
  if reply_args.is_a?(Array)
51
60
  send(msg.headers[:'reply-to'], *reply_args)
52
61
  else
@@ -58,31 +67,44 @@ module Klomp
58
67
  frames
59
68
  end
60
69
 
61
- def method_missing(method, *args, &block)
62
- write_only_methods = [
63
- :abort,
64
- :begin,
65
- :commit,
66
- ]
67
- read_only_methods = [
68
- :ack,
69
- :nack,
70
- :unsubscribe
71
- ]
72
- returns = {
73
- :connect => self
74
- }
70
+ def log
71
+ @logger
72
+ end
73
+
74
+ WRITE_ONLY_METHODS = [
75
+ :abort,
76
+ :begin,
77
+ :commit,
78
+ ]
79
+
80
+ READ_ONLY_METHODS = [
81
+ :ack,
82
+ :nack,
83
+ :unsubscribe,
84
+ ]
75
85
 
76
- result = if write_only_methods.include?(method)
86
+ def method_missing(method, *args, &block)
87
+ case method
88
+ when *WRITE_ONLY_METHODS
77
89
  @write_conn.send(method, *args, &block)
78
- elsif read_only_methods.include?(method)
79
- @read_conn.map { |c| c.__send__(method, *args, &block) }
90
+ when *READ_ONLY_METHODS
91
+ @read_conn.map {|c| c.__send__(method, *args, &block) }
92
+ when :connect
93
+ @all_conn.each {|c| c.connect}
94
+ self
80
95
  else
81
- ([@write_conn] + @read_conn).uniq.map { |c| c.__send__(method, *args) }
96
+ @all_conn.map {|c| c.__send__(method, *args) }
82
97
  end
83
- returns.include?(method) ? returns[method] : result
84
98
  end
85
99
 
100
+ private
101
+ def configure_connections
102
+ @all_conn.each do |c|
103
+ c.on_failover_connect_failure do
104
+ raise if OnStomp::FatalConnectionError === $!
105
+ end
106
+ end
107
+ end
86
108
  end
87
109
 
88
110
  end
@@ -34,6 +34,12 @@ describe Klomp::Client do
34
34
  client.disconnect
35
35
  end
36
36
 
37
+ it 'raises an error if authentication fails' do
38
+ assert_raises OnStomp::ConnectFailedError do
39
+ Klomp::Client.new(@uris.first.sub('password', 'psswrd')).connect
40
+ end
41
+ end
42
+
37
43
  it 'disconnnects' do
38
44
  client = Klomp::Client.new(@uris.first).connect
39
45
  assert client.write_conn.connected?
@@ -41,6 +47,15 @@ describe Klomp::Client do
41
47
  refute client.write_conn.connected?
42
48
  end
43
49
 
50
+ it 'has a logger' do
51
+ logger = Logger.new(STDOUT)
52
+ client = Klomp::Client.new(@uris, :logger=>logger).connect
53
+
54
+ assert_equal client.log, logger
55
+
56
+ client.disconnect
57
+ end
58
+
44
59
  it 'sends heartbeat' do
45
60
  client = Klomp::Client.new(@uris).connect.beat
46
61
  end
@@ -66,7 +81,7 @@ describe Klomp::Client do
66
81
  client = Klomp::Client.new(@uris).connect
67
82
  reply_to_body = { 'reply_to_body' => rand(36**128).to_s(36) }
68
83
 
69
- client.send(@destination, nil, { 'reply-to' => @destination })
84
+ client.puts(@destination, nil, { 'reply-to' => @destination })
70
85
 
71
86
  got_message = false
72
87
  client.subscribe(@destination) do |msg|
@@ -88,4 +103,9 @@ describe Klomp::Client do
88
103
  client.disconnect
89
104
  end
90
105
 
106
+ it 'sends all unknown options through to OnStomp' do
107
+ client = Klomp::Client.new(@uris.first, :haz_cheezburgers => true, :retry_attempts => 42).connect
108
+ assert client.write_conn.connected?
109
+ assert_equal 42, client.write_conn.retry_attempts
110
+ end
91
111
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klomp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-11 00:00:00.000000000 Z
12
+ date: 2012-06-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: onstomp
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 1.0.7
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: 1.0.7
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: json
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -51,6 +51,7 @@ extensions: []
51
51
  extra_rdoc_files: []
52
52
  files:
53
53
  - .gitignore
54
+ - ChangeLog.md
54
55
  - Gemfile
55
56
  - Gemfile.lock
56
57
  - Procfile
@@ -81,10 +82,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  version: '0'
82
83
  requirements: []
83
84
  rubyforge_project:
84
- rubygems_version: 1.8.23
85
+ rubygems_version: 1.8.21
85
86
  signing_key:
86
87
  specification_version: 3
87
88
  summary: A simple wrapper around the OnStomp library with additional features
88
89
  test_files:
89
90
  - test/test_client.rb
90
- has_rdoc: