Module: Sc4ry::Helpers

Defined in:
lib/sc4ry/helpers.rb

Overview

Note:

namespace

Sc4ry::Helpers module

Class Method Summary collapse

Class Method Details

.log(message:, target: nil, level: :info) ⇒ Boolean

class method (module) to help logging messages

Parameters:

  • target (Symbol) (defaults to: nil)

    a specific logger, restored old after

  • level (Symbol) (defaults to: :info)

    (default :info) a logging level (see Logger Stdlib)

  • message (String)

    your message

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/sc4ry/helpers.rb', line 14

def self.log(message:, target: nil, level: :info)
  save = Sc4ry::Loggers.current
  Sc4ry::Loggers.current = target if target
  Sc4ry::Loggers.get.send level, "Sc4ry : #{message}"
  Sc4ry::Loggers.current = save
  true
end

.notify(options = {}) ⇒ Boolean

class method (module) to help send notifiesby Sc4ry::Notifiers

Parameters:

  • options (Hash) (defaults to: {})

    a Notifying structure

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/sc4ry/helpers.rb', line 51

def self.notify(options = {})
  Sc4ry::Notifiers.list.each do |record|
    notifier = Sc4ry::Notifiers.get name: record
    notifier[:class].notify(options) if options[:config][:notifiers].include? record
  end
end

.verify_service(options = {}) ⇒ Bool

TCP/IP service checker

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :host (String)

    hostname

  • :port (String)

    TCP port

  • :url (String)

    full URL, priority on :host and :port

Returns:

  • (Bool)

    status



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sc4ry/helpers.rb', line 28

def self.verify_service(options = {})
  if options[:url]
    uri = URI.parse(options[:url])
    host = uri.host
    port = uri.port
  else
    host = options[:host]
    port = options[:port]
  end
  Timeout.timeout(1) do
    s = TCPSocket.new(host, port)
    s.close
    return true
  rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
    return false
  end
rescue Timeout::Error
  false
end