Class: HrrRbSsh::DataType::NameList

Inherits:
HrrRbSsh::DataType show all
Defined in:
lib/hrr_rb_ssh/data_type/name_list.rb

Overview

NameList provides methods to convert a comma-separated list of names and binary string each other.

Class Method Summary collapse

Class Method Details

.decode(io) ⇒ ::Array

Convert binary string into a comma-separated list of names.

Parameters:

  • io (::IO)

    IO instance that has buffer to be read

Returns:

  • (::Array)

    converted a comma-separated list of names



32
33
34
35
# File 'lib/hrr_rb_ssh/data_type/name_list.rb', line 32

def self.decode io
  length = io.read(4).unpack("N")[0]
  io.read(length).unpack("a*")[0].split(',')
end

.encode(arg) ⇒ ::String

Convert a comma-separated list of names into binary string.

Parameters:

  • arg (::Array)

    an array that containes names to be converted

Returns:

  • (::String)

    converted binary string

Raises:

  • (::ArgumentError)

    when arg is not an array

  • (::ArgumentError)

    when arg array containes an instance of not string



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hrr_rb_ssh/data_type/name_list.rb', line 14

def self.encode arg
  unless arg.kind_of? ::Array
    raise ArgumentError, "must be a kind of Array, but got #{arg.inspect}"
  end
  unless arg.all?{ |e| e.kind_of? ::String }
    raise ArgumentError, "must be with all elements of String, but got #{arg.inspect}"
  end
  joined_arg = arg.join(',')
  if joined_arg.length > 0xffff_ffff
    raise ArgumentError, "must be shorter than or equal to #{0xffff_ffff}, but got length #{joined_arg.length}"
  end
  [joined_arg.length, joined_arg].pack("Na*")
end