Module: UsefulUtilities::Api

Extended by:
Api
Included in:
Api
Defined in:
lib/useful_utilities/api.rb

Overview

API utilities

Constant Summary collapse

RESCUE_PORT_OPEN_EXCEPTIONS =
Note:

Exceptions to handle

See Also:

[
StandardError,
SocketError,
Timeout::Error].freeze

Instance Method Summary collapse

Instance Method Details

#convert_limit(value) ⇒ FalseClass/Numeric

Returns false if value is infinity or value if not.

Parameters:

Returns:

  • (FalseClass/Numeric)

    false if value is infinity or value if not



18
19
20
# File 'lib/useful_utilities/api.rb', line 18

def convert_limit(value)
  value == Float::INFINITY ? false : value
end

#port_open?(ip, port, sleep_time: 1, max_attempts: 3) ⇒ Boolean

Returns check if a port is open or not on a remote host.

Parameters:

  • ip (String)
  • port (Integer)
  • sleep_time (Hash) (defaults to: 1)

    a customizable set of options

  • max_attempts (Hash) (defaults to: 3)

    a customizable set of options

Options Hash (sleep_time:):

  • :sleep_time (Integer) — default: 1

Options Hash (max_attempts:):

  • :max_attempts (Integer) — default: 3

Returns:

  • (Boolean)

    check if a port is open or not on a remote host



27
28
29
30
31
32
33
34
35
36
# File 'lib/useful_utilities/api.rb', line 27

def port_open?(ip, port, sleep_time: 1, max_attempts: 3)
  try_to(max_attempts: max_attempts, sleep_time: sleep_time, rescue_what: RESCUE_PORT_OPEN_EXCEPTIONS) do
    Timeout::timeout(sleep_time) do
      TCPSocket.new(ip, port).close
      true
    end
  end
rescue *RESCUE_PORT_OPEN_EXCEPTIONS
  false
end