lib/eventbus/message.rb in eventbus-0.0.22 vs lib/eventbus/message.rb in eventbus-0.24

- old
+ new

@@ -1,7 +1,6 @@ require 'rubygems' -require 'bunny' require 'yaml' require 'uuid' require 'eventbus/queue' module EventBus @@ -13,25 +12,25 @@ def initialize(application_id) raise "No application ID specified!" if application_id.nil? @data = { - :HEADER => { - :message_id => UUID.new.generate.to_s, - :message_type => self.default_section_name, - :status => "BEGIN", - :application_id => application_id + 'HEADER' => { + 'message_id' => UUID.new.generate.to_s, + 'message_type' => self.default_section_name, + 'status' => "BEGIN", + 'application_id' => application_id }, - :ERROR_INFO => { - :is_error => false, - :backtrace => nil, - :message => nil + 'ERROR_INFO' => { + 'is_error' => false, + 'backtrace' => nil, + 'message' => nil }, - :PAYLOAD => {} + 'PAYLOAD' => {} } - @connection_driver = ENV["EVENTBUS_CONNECTOR"] || "Bunny" + @connection_driver = ENV["EVENTBUS_CONNECTOR"] || "Stomp" driver_module = "#{@connection_driver}ConnectionDriver" require_relative "connectors/#{@connection_driver.downcase}" @@ -45,22 +44,37 @@ end def set(key, val, opts = {}) + # This is for convenience and to make messages more + # human-readable by eliminating the !Binary encoding + # in YAML messages. I'm not willing to make this method + # more expensive by walking deep structures to do the + # same legwork, because this isn't a functionality issue. + # The message deserializes just fine without this. + # If your YAML is popping up with random !binary segments + # in the YAML *and this bothers you*, just do + # a force_encoding("UTF-8") on the members of the structure + # as needed. + if val.is_a?(String) + val = val.dup if val.frozen? # Copy to non-frozen string. + val.force_encoding("UTF-8") + end + raise "You must specify a key name!" if key.nil? section = opts.delete(:section) || self.default_section_name - @data[:PAYLOAD][section] = {} if @data[:PAYLOAD][section].nil? - @data[:PAYLOAD][section][key] = val + @data['PAYLOAD'][section.to_s] = {} if @data['PAYLOAD'][section.to_s].nil? + @data['PAYLOAD'][section.to_s][key.to_s] = val end def get(key, opts = {}) raise "You must specify a key name!" if key.nil? section = opts.delete(:section) || self.default_section_name - return @data[:PAYLOAD][section].nil? ? nil : @data[:PAYLOAD][section][key] + return @data['PAYLOAD'][section.to_s].nil? ? nil : @data['PAYLOAD'][section.to_s][key.to_s] end # opts: # :queue_name -- the base queue name to receive the message. Defaults to "dispatcher" # :global_queue -- Do not prepend the application ID and do not append the environment @@ -87,12 +101,16 @@ set_special(:HEADER, :sender, $0) send_raw self.dump, opts end def set_error(exception) - @data[:ERROR_INFO][:backtrace] = exception.backtrace - @data[:ERROR_INFO][:message] = exception.to_s + + exception.backtrace.map { |frame| frame.force_encoding("UTF-8") } + exception.message.force_encoding("UTF-8") + + @data['ERROR_INFO']['backtrace'] = exception.backtrace + @data['ERROR_INFO']['message'] = exception.message self.is_error = true self.status = exception.class.name end def dump @@ -161,17 +179,19 @@ return self.class.name.split(/::/).last end private - - def set_special(block, key, val) - @data[block][key] = val + if val.is_a?(String) + val = val.dup if val.frozen? # Copy to non-frozen string. + val.force_encoding("UTF-8") + end + @data[block.to_s][key.to_s] = val end def get_special(block, key) - return @data[block][key] + return @data[block.to_s][key.to_s] end end end # module EventBus