Sha256: 2609e966e7a669b22002ea4854801be8b0cc66d6e1caef49e59782b707562ee2

Contents?: true

Size: 1.41 KB

Versions: 41

Compression:

Stored size: 1.41 KB

Contents

module SSHKit

  class MappingInteractionHandler

    def initialize(mapping, log_level=nil)
      @log_level = log_level
      @mapping_proc = \
        case mapping
        when Hash
          lambda do |server_output|
            first_matching_key_value = mapping.find { |k, _v| k === server_output }
            first_matching_key_value.nil? ? nil : first_matching_key_value.last
          end
        when Proc
          mapping
        else
          raise "Unsupported mapping type: #{mapping.class} - only Hash and Proc mappings are supported"
        end
    end

    def on_data(_command, stream_name, data, channel)
      log("Looking up response for #{stream_name} message #{data.inspect}")

      response_data = @mapping_proc.call(data)

      if response_data.nil?
        log("Unable to find interaction handler mapping for #{stream_name}: #{data.inspect} so no response was sent")
      else
        log("Sending #{response_data.inspect}")
        if channel.respond_to?(:send_data) # Net SSH Channel
          channel.send_data(response_data)
        elsif channel.respond_to?(:write) # Local IO
          channel.write(response_data)
        else
          raise "Unable to write response data to channel #{channel.inspect} - does not support 'send_data' or 'write'"
        end
      end
    end

    private

    def log(message)
      SSHKit.config.output.send(@log_level, message) unless @log_level.nil?
    end

  end

end

Version data entries

41 entries across 41 versions & 2 rubygems

Version Path
sshkit-1.18.2 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.18.1 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.18.0 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.17.0 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.16.1 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.16.0 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.15.1 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.15.0 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.14.0 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.13.1 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.13.0 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.12.0 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.11.5 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.11.4 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.11.3 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.11.2 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.11.1 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.11.0 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.10.0 lib/sshkit/mapping_interaction_handler.rb
sshkit-1.9.0 lib/sshkit/mapping_interaction_handler.rb