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.



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

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



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

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

Class Method Details

.attributes(hash) ⇒ Object



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

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



43
44
45
46
47
48
49
50
51
# File 'lib/pingdom/base.rb', line 43

def self.check_error!(response)
  if response.body.is_a?(String)
    raise Error, response.body
  elsif 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



53
54
55
56
# File 'lib/pingdom/base.rb', line 53

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

Instance Method Details

#idObject



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

def id
  @attributes["id"]
end

#inspectObject



39
40
41
# File 'lib/pingdom/base.rb', line 39

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

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(name, include_private = false)
  @attributes.key?(name.to_s) || super
end