Module: Zabby::ZClass::ClassMethods

Defined in:
lib/zabby/zclass.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(zmethod, *args, &block) ⇒ Object

Simulate methods on the object and call the Zabbix Web Service (“host.get”, “item.create”, etc.). See www.zabbix.com/documentation/1.8/api for the API documentation.

Parameters:

  • zmethod (String)

    Name of the Web Service methods

  • args (Array)

    Method arguments

  • block (Proc)

    Unused

Raises:

  • (NoMethodError)

    Raised on invalid method names.



53
54
55
56
57
58
59
# File 'lib/zabby/zclass.rb', line 53

def method_missing(zmethod, *args, &block)
  if @zmethods.include? zmethod
    Zabby::Runner.instance.connection.perform_request(object_name, zmethod, args.first)
  else
    super
  end
end

Instance Attribute Details

#zmethodsObject (readonly)

List of valid Web Service methods for the current Zabbix Object



26
27
28
# File 'lib/zabby/zclass.rb', line 26

def zmethods
  @zmethods
end

Instance Method Details

#add_zmethods(*zmethods) ⇒ Object

Add the list of Web Service methods to the current class.

Examples:

class Host
  include ZClass
  add_zmethods :create, :delete, :exists, :get, :update
end

Parameters:

  • zmethods (Array)

    Method names



43
44
45
# File 'lib/zabby/zclass.rb', line 43

def add_zmethods(*zmethods)
  @zmethods = zmethods.map { |f| f.to_sym }
end

#inspectString

Human representation of the Zabbix Class

Examples:

Host.inspect => "<Zabbix Object 'host', methods: create, delete, exists, get, update>"

Returns:

  • (String)

    Class representation



65
66
67
# File 'lib/zabby/zclass.rb', line 65

def inspect
  "<Zabbix Object '#{object_name}', methods: #{@zmethods.join(', ')}>"
end

#object_nameString

Name of the current class without the namespace

Examples:

Zabby::Host.object_name => "host"

Returns:

  • (String)


32
33
34
# File 'lib/zabby/zclass.rb', line 32

def object_name
  @object_name ||= self.name.gsub(/^.*::/, '').downcase
end