Class: Masscan::Command::Shards Private

Inherits:
CommandMapper::Types::Str
  • Object
show all
Defined in:
lib/masscan/command.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents the type for the --shards option.

Since:

  • 0.2.0

Constant Summary collapse

REGEXP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regular expression for validating --shards values.

Since:

  • 0.2.0

%r{\A\d+/\d+\z}

Instance Method Summary collapse

Instance Method Details

#format(value) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Formats a shards value into a String.

Parameters:

  • value ((Integer, Integer), Rational, #to_s)

    The shards value to format.

Returns:

  • (String)

    The formatted shards value.

Since:

  • 0.2.0



359
360
361
362
363
364
365
366
# File 'lib/masscan/command.rb', line 359

def format(value)
  case value
  when Array
    "#{value[0]}/#{value[1]}"
  else
    super(value)
  end
end

#validate(value) ⇒ true, (false, String)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validates a shards value.

Parameters:

  • value (Array, Rational, String, #to_s)

    The shards value to validate.

Returns:

  • (true, (false, String))

    Returns true if the value is valid, or false and a validation error message if the value is not compatible.

Since:

  • 0.2.0



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/masscan/command.rb', line 319

def validate(value)
  case value
  when Array
    unless value.length == 2
      return [false, "must contain two elements (#{value.inspect})"]
    end

    unless (value[0].kind_of?(Integer) && value[1].kind_of?(Integer))
      return [false, "shard values must be Integers (#{value.inspect})"]
    end

    return true
  when Rational
    return true
  else
    valid, message = super(value)

    unless valid
      return [valid, message]
    end

    string = value.to_s

    unless string =~ REGEXP
      return [false, "invalid shards value (#{value.inspect})"]
    end

    return true
  end
end