You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by mc...@apache.org on 2014/11/10 22:16:02 UTC

[1/2] qpid-proton git commit: PROTON-736: Created the UTFString and BinaryString classes for Ruby.

Repository: qpid-proton
Updated Branches:
  refs/heads/master bdeaf3470 -> 5521a7ec6


PROTON-736: Created the UTFString and BinaryString classes for Ruby.

This allows users to explictly call out that a string is either UTF-8 or
a binary string.

For the UTFString type, it validates that the content provided is
actually UTF-8 and raises a RuntimeError if it's not.

Results returned from a Message object are wrapped in the appropriate
class.

This solution is Ruby 1.8, 1.9 and 2.0 compatible.


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/6371e9eb
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/6371e9eb
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/6371e9eb

Branch: refs/heads/master
Commit: 6371e9eba3b82a4927bcfb06919d4b3ea419a8d8
Parents: bdeaf34
Author: Darryl L. Pierce <mc...@gmail.com>
Authored: Mon Nov 10 09:30:47 2014 -0500
Committer: Darryl L. Pierce <mc...@gmail.com>
Committed: Mon Nov 10 11:20:15 2014 -0500

----------------------------------------------------------------------
 proton-c/bindings/ruby/lib/qpid_proton.rb       |  1 +
 proton-c/bindings/ruby/lib/qpid_proton/data.rb  |  4 +-
 .../bindings/ruby/lib/qpid_proton/mapping.rb    | 22 +------
 .../bindings/ruby/lib/qpid_proton/strings.rb    | 69 ++++++++++++++++++++
 4 files changed, 75 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6371e9eb/proton-c/bindings/ruby/lib/qpid_proton.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/qpid_proton.rb b/proton-c/bindings/ruby/lib/qpid_proton.rb
index e28c684..4da4e11 100644
--- a/proton-c/bindings/ruby/lib/qpid_proton.rb
+++ b/proton-c/bindings/ruby/lib/qpid_proton.rb
@@ -22,6 +22,7 @@ require "date"
 
 require "qpid_proton/version"
 require "qpid_proton/described"
+require "qpid_proton/strings"
 require "qpid_proton/mapping"
 require "qpid_proton/array"
 require "qpid_proton/hash"

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6371e9eb/proton-c/bindings/ruby/lib/qpid_proton/data.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/qpid_proton/data.rb b/proton-c/bindings/ruby/lib/qpid_proton/data.rb
index 9644fb4..1e515c8 100644
--- a/proton-c/bindings/ruby/lib/qpid_proton/data.rb
+++ b/proton-c/bindings/ruby/lib/qpid_proton/data.rb
@@ -725,7 +725,7 @@ module Qpid
       # If the current node is binary, returns its value. Otherwise, it returns
       # an empty string ("").
       def binary
-        Cproton.pn_data_get_binary(@data)
+        Qpid::Proton::BinaryString.new(Cproton.pn_data_get_binary(@data))
       end
 
       # Puts a unicode string value.
@@ -742,7 +742,7 @@ module Qpid
       # If the current node is a string, returns its value. Otherwise, it
       # returns an empty string ("").
       def string
-        Cproton.pn_data_get_string(@data)
+        Qpid::Proton::UTFString.new(Cproton.pn_data_get_string(@data))
       end
 
       # Puts a symbolic value.

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6371e9eb/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb b/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb
index 841156c..e7e3322 100644
--- a/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb
+++ b/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb
@@ -110,26 +110,10 @@ module Qpid # :nodoc:
 
     class << STRING
       def put(data, value)
-        # In Ruby 1.9+ we have encoding methods that can check the content of
-        # the string, so use them to see if what we have is unicode. If so,
-        # good! If not, then just treat is as binary.
-        #
-        # No such thing in Ruby 1.8. So there we need to use Iconv to try and
-        # convert it to unicode. If it works, good! But if it raises an
-        # exception then we'll treat it as binary.
-        if RUBY_VERSION >= "1.9"
-          if value.encoding == "UTF-8" || value.force_encoding("UTF-8").valid_encoding?
-            data.string = value.to_s
-          else
-            data.binary = value.to_s
-          end
+        if value.is_a?(Qpid::Proton::UTFString) || Qpid::Proton.is_valid_utf?(value)
+          data.string = value.to_s
         else
-          begin
-            newval = Iconv.new("UTF8//TRANSLIT//IGNORE", "UTF8").iconv(value.to_s)
-            data.string = newval
-          rescue
-            data.binary = value
-          end
+          data.binary = value.to_s
         end
       end
     end

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6371e9eb/proton-c/bindings/ruby/lib/qpid_proton/strings.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/qpid_proton/strings.rb b/proton-c/bindings/ruby/lib/qpid_proton/strings.rb
new file mode 100644
index 0000000..dad96ad
--- /dev/null
+++ b/proton-c/bindings/ruby/lib/qpid_proton/strings.rb
@@ -0,0 +1,69 @@
+#
+# 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.
+#
+
+module Qpid # :nodoc:
+
+  module Proton # :nodoc:
+
+    def self.is_valid_utf?(value)
+      # In Ruby 1.9+ we have encoding methods that can check the content of
+      # the string, so use them to see if what we have is unicode. If so,
+      # good! If not, then just treat is as binary.
+      #
+      # No such thing in Ruby 1.8. So there we need to use Iconv to try and
+      # convert it to unicode. If it works, good! But if it raises an
+      # exception then we'll treat it as binary.
+      if RUBY_VERSION >= "1.9"
+        return true if (value.encoding == "UTF-8" ||
+                        value.force_encoding("UTF-8").valid_encoding?)
+
+        return false
+      else
+        begin
+          newval = Iconv.new("UTF8//TRANSLIT//IGNORE", "UTF8").iconv(value.to_s)
+          return true
+        rescue
+          return false
+        end
+      end
+    end
+
+    # UTFString lets an application explicitly state that a
+    # string of characters is to be UTF-8 encoded.
+    #
+    class UTFString < ::String
+
+      def initialize(value)
+        if !Qpid::Proton.is_valid_utf?(value)
+          raise RuntimeError.new("invalid UTF string")
+        end
+
+        super(value)
+      end
+
+    end
+
+    # BinaryString lets an application explicitly declare that
+    # a string value represents arbitrary data.
+    #
+    class BinaryString < ::String; end
+
+  end
+
+end


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


[2/2] qpid-proton git commit: NO JIRA: Updated the documentation for the Ruby Message class.

Posted by mc...@apache.org.
NO JIRA: Updated the documentation for the Ruby Message class.


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/5521a7ec
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/5521a7ec
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/5521a7ec

Branch: refs/heads/master
Commit: 5521a7ec68622492cea687ab4df9903ac2f20ce2
Parents: 6371e9e
Author: Darryl L. Pierce <mc...@gmail.com>
Authored: Mon Nov 10 15:33:26 2014 -0500
Committer: Darryl L. Pierce <mc...@gmail.com>
Committed: Mon Nov 10 16:15:04 2014 -0500

----------------------------------------------------------------------
 .../bindings/ruby/lib/qpid_proton/message.rb    | 24 ++++++++++++++++++++
 1 file changed, 24 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/5521a7ec/proton-c/bindings/ruby/lib/qpid_proton/message.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/qpid_proton/message.rb b/proton-c/bindings/ruby/lib/qpid_proton/message.rb
index d840299..b7a4fd0 100644
--- a/proton-c/bindings/ruby/lib/qpid_proton/message.rb
+++ b/proton-c/bindings/ruby/lib/qpid_proton/message.rb
@@ -23,8 +23,24 @@ module Qpid
 
     # A Message represents an addressable quantity of data.
     #
+    # ==== Message Body
+    #
+    # The message body can be set using the #body= method. The message will
+    # then attempt to determine how exactly to encode the content.
+    #
     # ==== Examples
     #
+    # To create a message for sending:
+    #
+    #   # send a simple text message
+    #   msg = Qpid::Proton::Message.new
+    #   msg.body = "STATE: update"
+    #
+    #   # send a binary chunk of data
+    #   data = File.binread("/home/qpid/binfile.tar.gz")
+    #   msg = Qpid::Proton::Message.new
+    #   msg.body = Qpid::Proton::BinaryString.new(data)
+    #
     class Message
 
       # Decodes a message from supplied AMQP data and returns the number
@@ -372,6 +388,8 @@ module Qpid
       #
       # See MessageFormat for more details on formats.
       #
+      # *Warning:* This method has been deprecated.
+      #
       # ==== Options
       #
       # * format - the format
@@ -383,6 +401,12 @@ module Qpid
 
       # Returns the message format
       #
+      # *Warning:* This method has been deprecated.
+      #
+      # ==== Note
+      #
+      # This method is now deprecated.
+      #
       def format
         Qpid::Proton::MessageFormat.by_value(Cproton.pn_message_get_format(@impl))
       end


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org