Class: Nagios::API::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/nagios/api/resource.rb

Direct Known Subclasses

Downtime, Host, Service, State

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Resource

Returns a new instance of Resource.



6
7
8
9
10
# File 'lib/nagios/api/resource.rb', line 6

def initialize(args = {})
  @api_client = args.delete(:api_client)
  @attributes = {}
  self.attributes = args
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/nagios/api/resource.rb', line 19

def method_missing(meth, *args, &block)
  if meth.to_s =~ /^(.+)=$/
    @attributes[$1.to_sym] = args[0]
  elsif @attributes.has_key?(meth)
    @attributes[meth]
  else
    super # You *must* call super if you don't handle the
          # method, otherwise you'll mess up Ruby's method
          # lookup.
  end
end

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



3
4
5
# File 'lib/nagios/api/resource.rb', line 3

def api_client
  @api_client
end

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/nagios/api/resource.rb', line 4

def attributes
  @attributes
end

Instance Method Details

#to_hash(options = {}) ⇒ Object



31
32
33
34
35
36
# File 'lib/nagios/api/resource.rb', line 31

def to_hash(options = {})
  hash = attributes
  hash.reject! { |key, value| options[:except].include?(key.to_sym) || options[:except].include?(key.to_s) } if options[:except]
  hash.reject! { |key, value| !options[:only].include?(key.to_sym) && !options[:only].include?(key.to_s) } if options[:only]
  hash
end