Author: tross
Date: Fri Jul 15 16:57:19 2011
New Revision: 1147239
URL: http://svn.apache.org/viewvc?rev=1147239&view=rev
Log:
QPID-3306 - Provides a more Ruby-like set of APIs on top of the bindings
created by swig.
Applied patch from Darryl Pierce
Created the encode and decode methods in
Qpid::Messaging.
These methods differ from the same methods in Cqpid in that they handle
working with symbols and other non-string values.
Added:
qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid/encoding.rb
qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/test_encoding.rb
Modified:
qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid.rb
qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/ts_bindings.rb
Modified: qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid.rb
URL:
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid.rb?rev=1147239&r1=1147238&r2=1147239&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid.rb (original)
+++ qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid.rb Fri Jul 15 16:57:19 2011
@@ -19,4 +19,5 @@
require 'qpid/errors'
require 'qpid/duration'
+require 'qpid/encoding'
Added: qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid/encoding.rb
URL:
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid/encoding.rb?rev=1147239&view=auto
==============================================================================
--- qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid/encoding.rb (added)
+++ qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid/encoding.rb Fri Jul 15
16:57:19 2011
@@ -0,0 +1,56 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+require 'cqpid'
+
+module Qpid
+
+ module Messaging
+
+ # Encodes the supplied content into the given message.
+ def self.encode content, message, encoding = nil
+ prepared = content
+ case content
+ when Hash
+ prepared = {}
+ content.each_pair do |key,value|
+ prepared[key.to_s] = value.to_s
+ end
+ Cqpid::encode prepared, message.message_impl
+ when Array
+ prepared = []
+ content.each {|value| prepared << value.to_s}
+ Cqpid::encode prepared, message.message_impl
+ end
+ end
+
+ # Decodes and returns the message's content.
+ def self.decode(message, content_type = nil)
+ content_type = message.content_type unless content_type
+
+ case content_type
+ when "amqp/map": Cqpid.decodeMap message.message_impl
+ when "amqp/list": Cqpid.decodeList message.message_impl
+ end
+ end
+
+ end
+
+end
+
Added: qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/test_encoding.rb
URL:
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/test_encoding.rb?rev=1147239&view=auto
==============================================================================
--- qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/test_encoding.rb (added)
+++ qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/test_encoding.rb Fri Jul 15
16:57:19 2011
@@ -0,0 +1,146 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
+
+require 'test/unit'
+require 'flexmock/test_unit'
+
+require 'cqpid'
+require 'qpid/encoding'
+
+class TestEncoding < Test::Unit::TestCase
+
+ def setup
+ @cqpid = flexmock(Cqpid)
+
+ @message = flexmock("message")
+ @message_impl = flexmock("message_impl")
+
+ @encoded = {"foo" => "bar"}
+ end
+
+ def test_encode_map_with_symbols
+ @message.
+ should_receive(:message_impl).
+ once.
+ and_return(@message_impl)
+ @cqpid.
+ should_receive(:encode).
+ once.
+ with({"foo" => "bar"}, @message_impl).
+ and_return(@encoded)
+
+ result = Qpid::Messaging.encode({:foo => :bar}, @message)
+
+ assert_same @encoded, result
+ end
+
+ def test_encode_list_with_symbols
+ @message.
+ should_receive(:message_impl).
+ once.
+ and_return(@message_impl)
+ @cqpid.
+ should_receive(:encode).
+ once.
+ with(["foo", "bar"], @message_impl).
+ and_return(@encoded)
+
+ result = Qpid::Messaging.encode([:foo, :bar], @message)
+
+ assert_same @encoded, result
+ end
+
+ def test_encode_with_content_type
+ @message.
+ should_receive(:message_impl).
+ once.
+ and_return(@message_impl)
+ @cqpid.
+ should_receive(:encode).
+ once.
+ with({"foo" => "bar"}, @message_impl).
+ and_return(@encoded)
+
+ result = Qpid::Messaging.encode({:foo => :bar}, @message)
+
+ assert_same @encoded, result
+ end
+
+ def test_encode
+ @message.
+ should_receive(:message_impl).
+ once.
+ and_return(@message_impl)
+ @cqpid.
+ should_receive(:encode).
+ once.
+ with({"foo" => "bar"}, @message_impl).
+ and_return(@encoded)
+
+ result = Qpid::Messaging.encode({"foo" => "bar"}, @message)
+
+ assert_same @encoded, result
+ end
+
+ def test_decode_for_map
+ decoded = {"foo" => "bar"}
+ @message.
+ should_receive(:content_type).
+ once.
+ and_return("amqp/map")
+ @message.
+ should_receive(:message_impl).
+ once.
+ and_return(@message_impl)
+ @cqpid.
+ should_receive(:decodeMap).
+ once.
+ with(@message_impl).
+ and_return(decoded)
+
+ result = Qpid::Messaging.decode(@message)
+
+ assert_same decoded, result
+ end
+
+ def test_decode_for_list
+ decoded = ["foo", "bar"]
+ @message.
+ should_receive(:content_type).
+ once.
+ and_return("amqp/list")
+ @message.
+ should_receive(:message_impl).
+ once.
+ and_return(@message_impl)
+ @cqpid.
+ should_receive(:decodeList).
+ once.
+ with(@message_impl).
+ and_return(decoded)
+
+ result = Qpid::Messaging.decode(@message)
+
+ assert_same decoded, result
+ end
+
+end
+
Modified: qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/ts_bindings.rb
URL:
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/ts_bindings.rb?rev=1147239&r1=1147238&r2=1147239&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/ts_bindings.rb (original)
+++ qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/ts_bindings.rb Fri Jul 15
16:57:19 2011
@@ -20,4 +20,5 @@
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
require 'test/unit'
+require 'test_encoding'
---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project: http://qpid.apache.org
Use/Interact: mailto:[email protected]