class Pingdom::Client

Attributes

limit[RW]

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

check(id) click to toggle source
# File lib/pingdom/client.rb, line 66
def check(id)
  Check.parse(self, get("checks/#{id}")).first
end
checks(options = {}) click to toggle source
# File lib/pingdom/client.rb, line 63
def checks(options = {})
  Check.parse(self, get("checks", options))
end
contacts(options = {}) click to toggle source
# File lib/pingdom/client.rb, line 80
def contacts(options = {})
  Contact.parse(self, get("contacts", options))
end
get(uri, params = {}, &block) click to toggle source
# 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) click to toggle source

“Remaining: 394 Time until reset: 3589”

# 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) click to toggle source

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

# 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 = {}) click to toggle source
# File lib/pingdom/client.rb, line 76
def probes(options = {})
  Probe.parse(self, get("probes", options))
end
results(id, options = {}) click to toggle source

Check ID

# 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) click to toggle source
# File lib/pingdom/client.rb, line 84
def summary(id)
  Summary.proxy(self, id)
end
test!(options = {}) click to toggle source
# File lib/pingdom/client.rb, line 59
def test!(options = {})
  Result.parse(self, get("single", options)).first
end
update_limits!(short, long) click to toggle source
# 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