Class: Pingdom::Base

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

Constant Summary collapse

@@client =
Client.new
@@params =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Base

Returns a new instance of Base.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pingdom/base.rb', line 92

def initialize data
  @additional_field={}
  data.each_pair do |k,v|
    v=Time.at(v) if is_time_attribute?(k)

    method="#{k}=".to_sym
    if self.methods.include?(method)
      self.send(method,v)
    else
      self.add(k, v)
    end
  end
end

Instance Attribute Details

#additional_fieldObject

Returns the value of attribute additional_field.



90
91
92
# File 'lib/pingdom/base.rb', line 90

def additional_field
  @additional_field
end

Class Method Details

.allObject



16
17
18
19
20
21
# File 'lib/pingdom/base.rb', line 16

def all
  parse client.get( params: params , path: path )
  collection.map do |data|
    self.new data
  end
end

.bodyObject



60
61
62
# File 'lib/pingdom/base.rb', line 60

def body
  @@body
end

.clientObject



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

def client
  @@client
end

.collectionObject



76
77
78
# File 'lib/pingdom/base.rb', line 76

def collection
  body[path[1..-1]]
end

.collection_typeObject



43
44
45
# File 'lib/pingdom/base.rb', line 43

def collection_type
  path[1..-2]
end

.error?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/pingdom/base.rb', line 56

def error?
  body.has_key?'error'
end

.find(id, *args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pingdom/base.rb', line 23

def find id, *args
  validator=Pingdom::Validator.new

  @@params=validator.validate input: args, permit: permit, params: params

  parse client.get( path: "#{path}/#{id}" , params: params)

  @@params={}
  raise "#{id} not found" if status!=200
  self.new body[collection_type]

end

.limitedObject



84
85
86
# File 'lib/pingdom/base.rb', line 84

def limited
  body['counts']['limited']
end

.paramsObject



68
69
70
# File 'lib/pingdom/base.rb', line 68

def params
  @@params
end

.params=(value) ⇒ Object



72
73
74
# File 'lib/pingdom/base.rb', line 72

def params= value
  @@params=value
end

.parse(response) ⇒ Object



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

def parse response
  @@body= JSON.parse response.body
  @@status = response.status
end

.pathObject



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

def path
end

.permitObject



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

def permit
  {}
end

.statusObject



64
65
66
# File 'lib/pingdom/base.rb', line 64

def status
  @@status
end

.totalObject



80
81
82
# File 'lib/pingdom/base.rb', line 80

def total
  body['counts']['total']
end

.update_client(username:, password:, key:) ⇒ Object



12
13
14
# File 'lib/pingdom/base.rb', line 12

def update_client  username: , password: , key:
  @@client = Client.new u: username, p: password, k: key
end

Instance Method Details

#add(key, value) ⇒ Object



106
107
108
# File 'lib/pingdom/base.rb', line 106

def add key, value
  additional_field[key.to_sym]=value
end

#get(key) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/pingdom/base.rb', line 110

def get key
  key=key.to_sym
  if self.methods.include?(key)
    self.send key
  else
    additional_field[key]
  end
end