Sha256: e50b407593caa50b31dec32a2a76d48023c454d11d3d5e923d601d422c40ba8c

Contents?: true

Size: 1.08 KB

Versions: 15

Compression:

Stored size: 1.08 KB

Contents

require 'rest-client'
require 'multi_json'
require 'cgi'

module DashingContrib
  module Pingdom
    module Uptime
      extend self

      def calc(credentials, id, from_time, to_time, rounding = 2)
        payload = make_request(credentials, id, from_time, to_time)
        summary = payload[:summary][:status]
        up     = summary[:totalup]
        unkown = summary[:totalunknown]
        down   = summary[:totaldown]

        uptime = (up.to_f - (unkown.to_f + down.to_f)) * 100 / up.to_f
        uptime.round(rounding)
      end

      private
      def make_request(credentials, id, from_time, to_time)
        request_url = uptime_request_url(credentials, id, from_time, to_time)
        response = RestClient.get(request_url, { 'App-Key' => credentials.api_key })
        MultiJson.load(response.body, { symbolize_keys: true })
      end

      def uptime_request_url(credentials, id, from_time, to_time)
        "https://#{credentials.username}:#{credentials.password}@api.pingdom.com/api/2.0/summary.average/#{id}?from=#{from_time}&to=#{to_time}&includeuptime=true"
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
dashing-contrib-0.1.12 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.1.11 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.1.10 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.1.9 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.1.8 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.1.7 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.1.6 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.1.5 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.1.4 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.1.3 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.1.2 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.1.1 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.1.0 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.0.5 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.0.4 lib/dashing-contrib/bottles/pingdom/uptime.rb