Class: Pingdom::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pingdom/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, response, attributes = {}) ⇒ Base

Returns a new instance of Base.



4
5
6
7
8
# File 'lib/pingdom/base.rb', line 4

def initialize(client, response, attributes = {})
  @client     = client
  @response   = response
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



24
25
26
# File 'lib/pingdom/base.rb', line 24

def method_missing(name, *args, &block)
  @attributes[name] or super
end

Class Method Details

.attributes(hash) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pingdom/base.rb', line 10

def self.attributes(hash)
  hash.each do |(attribute, aliases)|
    class_eval <<-"end;" unless instance_methods.include?(attribute.to_s)
      def #{attribute}
        @attributes[:#{attribute}]
      end
    end;
    
    Array.wrap(aliases).each do |aliased|
      alias_method aliased, attribute
    end
  end
end

.check_error!(response) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/pingdom/base.rb', line 40

def self.check_error!(response)
  if response.body.key?(:error)
    raise Error, "%s (%s %s)" % [ response.body[:error][:errormessage],
                                  response.body[:error][:statuscode],
                                  response.body[:error][:statusdesc] ]
  end
end

.parse(client, response) ⇒ Object



48
49
50
51
# File 'lib/pingdom/base.rb', line 48

def self.parse(client, response)
  check_error!(response)
  response.body
end

Instance Method Details

#idObject



32
33
34
# File 'lib/pingdom/base.rb', line 32

def id
  @attributes[:id]
end

#inspectObject



36
37
38
# File 'lib/pingdom/base.rb', line 36

def inspect
  "#<%s %s>" % [self.class.to_s, @attributes.inject([]){ |a, (k,v)| a << "%s: %s" % [k,v.inspect]; a }.join(' ')]
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/pingdom/base.rb', line 28

def respond_to?(name)
  super(name) || @attributes.key?(name)
end