class Pingdom::Client

Attributes

limit[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/pingdom/client.rb, line 7
def initialize(options = {})
  @options = options.with_indifferent_access.reverse_merge(http_driver: Faraday.default_adapter)

  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/", ssl: { verify: false }) do |builder|
    # builder.use Gzip # TODO: write GZip response handler, add Accept-Encoding: gzip header
    builder.response :json, content_type: /\bjson$/
    builder.use Tinder::FaradayResponse::WithIndifferentAccess
    builder.adapter @options[:http_driver]

    builder.basic_auth @options[:username], @options[:password]
    builder.headers["App-Key"] = @options[:key]
    builder.headers["Account-Email"] = @options[:account_email] if @options[:account_email]
  end
end

Public Instance Methods

check(id) click to toggle source
# File lib/pingdom/client.rb, line 63
def check(id)
  Check.parse(self, get("checks/#{id}")).first
end
checks(options = {}) click to toggle source
# File lib/pingdom/client.rb, line 59
def checks(options = {})
  Check.parse(self, get("checks", options))
end
contacts(options = {}) click to toggle source
# File lib/pingdom/client.rb, line 85
def contacts(options = {})
  Contact.parse(self, get("contacts", options))
end
get(uri, params = {}, &block) click to toggle source
# File lib/pingdom/client.rb, line 34
def get(uri, params = {}, &block)
  response = @connection.get(@connection.build_url(uri, prepare_params(params)), &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 48
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 25
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 81
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 76
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 89
def summary(id)
  Summary.proxy(self, id)
end
test!(options = {}) click to toggle source
# File lib/pingdom/client.rb, line 55
def test!(options = {})
  Result.parse(self, get("single", options)).first
end
tms_recipe(id) click to toggle source
# File lib/pingdom/client.rb, line 71
def tms_recipe(id)
  TMSRecipe.parse(self, get("tms.recipes/#{id}")).first
end
tms_recipes(options = {}) click to toggle source
# File lib/pingdom/client.rb, line 67
def tms_recipes(options = {})
  TMSRecipe.parse(self, get("tms.recipes", options))
end
tms_summary(id) click to toggle source
# File lib/pingdom/client.rb, line 93
def tms_summary(id)
  TMSSummary.proxy(self, id)
end
update_limits!(short, long) click to toggle source
# File lib/pingdom/client.rb, line 40
def update_limits!(short, long)
  @limit ||= {}
  @limit[:short]  = parse_limit(short)
  @limit[:long]   = parse_limit(long)
  @limit
end