Sha256: f6e9957c0153e77aafb6a9f76534864a588c29d1f528668c750f6bd2bc8daf8f

Contents?: true

Size: 1.4 KB

Versions: 43

Compression:

Stored size: 1.4 KB

Contents

module Hydra #:nodoc:
  # Module that implemets methods that auto-serialize and deserialize messaging
  # objects.
  module MessagingIO
    # Read a Message from the input IO object. Automatically build
    # a message from the response and return it.
    #
    #  IO.gets
    #    => Hydra::Message # or subclass
    def gets
      raise IOError unless @reader
      message = @reader.gets
      return nil unless message
      return Message.build(eval(message.chomp))
    rescue SyntaxError, NameError
      # uncomment to help catch remote errors by seeing all traffic
      #$stderr.write "Not a message: [#{message.inspect}]\n"
      return gets
    end

    # Write a Message to the output IO object. It will automatically
    # serialize a Message object.
    #  IO.write Hydra::Message.new
    def write(message)
      raise IOError unless @writer
      raise UnprocessableMessage unless message.is_a?(Hydra::Message)
      @writer.write(message.serialize+"\n")
    rescue Errno::EPIPE
      raise IOError
    end

    # Closes the IO object.
    def close
      @reader.close if @reader
      @writer.close if @writer
    end

    # IO will return this error if it cannot process a message.
    # For example, if you tried to write a string, it would fail,
    # because the string is not a message.
    class UnprocessableMessage < RuntimeError
      # Custom error message
      attr_accessor :message
    end
  end
end

Version data entries

43 entries across 43 versions & 4 rubygems

Version Path
hydra-0.23.3 lib/hydra/messaging_io.rb
causes-hydra-0.21.0 lib/hydra/messaging_io.rb
hydra-0.23.2 lib/hydra/messaging_io.rb
hydra-0.23.1 lib/hydra/messaging_io.rb
hydra-0.23.0 lib/hydra/messaging_io.rb
hydra-0.22.2 lib/hydra/messaging_io.rb
hydra-0.22.1 lib/hydra/messaging_io.rb
hydra-0.22.0 lib/hydra/messaging_io.rb
hydra-0.21.0 lib/hydra/messaging_io.rb
hydra-0.20.0 lib/hydra/messaging_io.rb
bellmyer-hydra-0.20.12 lib/hydra/messaging_io.rb
bellmyer-hydra-0.20.11 lib/hydra/messaging_io.rb
bellmyer-hydra-0.20.10 lib/hydra/messaging_io.rb
bellmyer-hydra-0.20.9 lib/hydra/messaging_io.rb
hydra-0.19.4 lib/hydra/messaging_io.rb
hydra-0.19.3 lib/hydra/messaging_io.rb
hydra-0.19.2 lib/hydra/messaging_io.rb
hydra-0.19.1 lib/hydra/messaging_io.rb
hydra-0.19.0 lib/hydra/messaging_io.rb
hydra-0.18.0 lib/hydra/messaging_io.rb