Class: Pingdom::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pingdom/client.rb', line 8

def initialize(options = {})
  @options = options.with_indifferent_access.reverse_merge(:http_driver => :excon)
  
  raise ArgumentError, "an application key must be provided (as :key)" unless @options.key?(:key)
  
  @connection = Faraday::Connection.new(:url => "https://api/pingdom.com/api/2.0/") do |builder|
    builder.url_prefix = "https://api.pingdom.com/api/2.0"
    
    builder.adapter :logger, @options[:logger]
    
    builder.adapter @options[:http_driver]
    
    # builder.use Gzip # TODO: write GZip response handler, add Accept-Encoding: gzip header
    builder.response :yajl
    builder.use Tinder::FaradayResponse::WithIndifferentAccess
    
    builder.basic_auth @options[:username], @options[:password]
  end
end

Instance Attribute Details

#limitObject

Returns the value of attribute limit.



6
7
8
# File 'lib/pingdom/client.rb', line 6

def limit
  @limit
end

Instance Method Details

#check(id) ⇒ Object



66
67
68
# File 'lib/pingdom/client.rb', line 66

def check(id)
  Check.parse(self, get("checks/#{id}")).first
end

#checks(options = {}) ⇒ Object



63
64
65
# File 'lib/pingdom/client.rb', line 63

def checks(options = {})
  Check.parse(self, get("checks", options))
end

#contacts(options = {}) ⇒ Object



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

def contacts(options = {})
  Contact.parse(self, get("contacts", options))
end

#get(uri, params = {}, &block) ⇒ Object



38
39
40
41
42
# File 'lib/pingdom/client.rb', line 38

def get(uri, params = {}, &block)
  response = @connection.get(@connection.build_url(uri, prepare_params(params)), "App-Key" => @options[:key], &block)
  update_limits!(response.headers['req-limit-short'], response.headers['req-limit-long'])
  response
end

#parse_limit(limit) ⇒ Object

“Remaining: 394 Time until reset: 3589”



52
53
54
55
56
57
# File 'lib/pingdom/client.rb', line 52

def parse_limit(limit)
  if limit.to_s =~ /Remaining: (\d+) Time until reset: (\d+)/
    { :remaining => $1.to_i,
      :resets_at => $2.to_i.seconds.from_now }
  end
end

#prepare_params(options) ⇒ Object

probes => [1,2,3] #=> probes => “1,2,3”



29
30
31
32
33
34
35
36
# File 'lib/pingdom/client.rb', line 29

def prepare_params(options)
  options.each do |(key, value)|
    options[key] = Array.wrap(value).map(&:to_s).join(',')
    options[key] = value.to_i if value.acts_like?(:time)
  end
  
  options
end

#probes(options = {}) ⇒ Object



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

def probes(options = {})
  Probe.parse(self, get("probes", options))
end

#results(id, options = {}) ⇒ Object

Check ID



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

def results(id, options = {})
  options.reverse_merge!(:includeanalysis => true)
  Result.parse(self, get("results/#{id}", options))
end

#summary(id) ⇒ Object



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

def summary(id)
  Summary.proxy(self, id)
end

#test!(options = {}) ⇒ Object



59
60
61
# File 'lib/pingdom/client.rb', line 59

def test!(options = {})
  Result.parse(self, get("single", options)).first
end

#update_limits!(short, long) ⇒ Object



44
45
46
47
48
49
# File 'lib/pingdom/client.rb', line 44

def update_limits!(short, long)
  @limit ||= {}
  @limit[:short]  = parse_limit(short)
  @limit[:long]   = parse_limit(long)
  @limit
end