pingdom-ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTM1MGFiMDA5ZThmZjI4ZTVlOTVkYjcwOTc3ODJjMTZmOWY1ZDE2Mw==
5
+ data.tar.gz: !binary |-
6
+ ZWE2ZTkxOTQ0MDUzYWE0MWY0MmMxYWMyNjUzYzJiYmJiNTBjZjY1Mg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjEzYWNlZjkyMGQ1MjI0ZWNjZmQwY2YxOWJiN2IxZGI5YmU2MGRmZGU2NzM1
10
+ NTY3NzRhNjgwOTU0NzdhMjM2YmNmNjJjOGIxZmE5NWY1ZDRkYWU4ZTlmOGEy
11
+ ZmI5ZmJhNDIxZjE5ZmQwN2M2OGRkOWZjODk1MmNjNTUyNmY3ZjE=
12
+ data.tar.gz: !binary |-
13
+ YmQ1NjBkMzVmZGM4ZGU4NjUzZGE1NzhlNjMzMDkwZWFlMTMwMGY1M2UyMzBk
14
+ YzVmMjY2NWRlMmVhZWZhZTZiNjZjNTZiMDEzY2M0ZjA4ZWY2MzhiNjU4MTc1
15
+ MTdlMmIxNWE5NzgyMzc5NTIzNmY2ZjcwYTZkZjE1MjIxN2M1Njc=
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "faraday", '~> 0.8.8'
4
+
5
+ gem "excon"
6
+ gem "yajl-ruby", "~> 0.8.1", :require => "yajl"
7
+
8
+ gem "activesupport", "~> 3.0.4"
9
+ gem "i18n", "~> 0.5.0" # ActiveSupport dependency
10
+
11
+ group :development do
12
+ gem "bundler"
13
+ gem "rake", '~> 10.1.0'
14
+ gem "rspec", '~> 2.14.1'
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ activesupport (3.0.20)
5
+ diff-lcs (1.2.4)
6
+ excon (0.26.0)
7
+ faraday (0.8.8)
8
+ multipart-post (~> 1.2.0)
9
+ i18n (0.5.0)
10
+ multipart-post (1.2.0)
11
+ rake (10.1.0)
12
+ rspec (2.14.1)
13
+ rspec-core (~> 2.14.0)
14
+ rspec-expectations (~> 2.14.0)
15
+ rspec-mocks (~> 2.14.0)
16
+ rspec-core (2.14.5)
17
+ rspec-expectations (2.14.3)
18
+ diff-lcs (>= 1.1.3, < 2.0)
19
+ rspec-mocks (2.14.3)
20
+ yajl-ruby (0.8.3)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ activesupport (~> 3.0.4)
27
+ bundler
28
+ excon
29
+ faraday (~> 0.8.8)
30
+ i18n (~> 0.5.0)
31
+ rake (~> 10.1.0)
32
+ rspec (~> 2.14.1)
33
+ yajl-ruby (~> 0.8.1)
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'rspec/core/rake_task'
12
+ RSpec::Core::RakeTask.new do |t|
13
+ t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
14
+ t.pattern = 'spec/**/*_spec.rb'
15
+ end
16
+
17
+ task :console do
18
+ exec %(ruby -rirb -rubygems -r bundler/setup -r lib/pingdom-ruby -e '$credentials = YAML.load_file("credentials.yml").with_indifferent_access; $client = Pingdom::Client.new($credentials); IRB.start')
19
+ end
20
+
21
+ task :default => :spec
data/Readme.md ADDED
@@ -0,0 +1,56 @@
1
+ # Pingdom RESTful API Client
2
+
3
+ Pingdom RESTful API wrapper for Ruby.
4
+
5
+ NOTE: This is a 3rd party gem and not an official product from Pingdom.
6
+
7
+ ## Usage
8
+
9
+ client = Pingdom::Client.new :username => u, :password => p, :key => k
10
+ check = client.checks.first #=> #<Pingdom::Check>
11
+ check.last_response_time #=> 200 (ms)
12
+ check.status #=> "up"
13
+
14
+ result = check.results.first(:probes => [1,2,3], :status => [:up, :down])
15
+ #=> #<Pingdom::Result>
16
+ result.status #=> :up
17
+ result.up? #=> true
18
+ result.response_time #=> 20000 (microsecs)
19
+
20
+ avg = check.summary.average(:from => 1.month.ago,
21
+ :probes => [1,2,3])
22
+ #=> #<Pingdom::Summary::Average>
23
+ avg.response_time #=> 200 (ms)
24
+ probe_avg = avg.averages.first
25
+ probe_avg.response_time #=> 120 (ms)
26
+ probe_avg.probe.name #=> "Atlanta, GA"
27
+
28
+ ## Contributors
29
+
30
+ * Updated for Ruby 1.9.x/2.0.0 by Jason Straughan ([jdstraughan](http://github.com/jdstraughan))
31
+ * Emily Price ([duien](http://github.com/duien))
32
+ * Based on [pingom-client](https://github.com/mtodd/pingdom-client) by [Matt Todd](https://github.com/mtodd)
33
+
34
+ ## License
35
+
36
+ The MIT License
37
+
38
+ Copyright (c) 2013 Jason Straughan.
39
+
40
+ Permission is hereby granted, free of charge, to any person obtaining a copy
41
+ of this software and associated documentation files (the "Software"), to deal
42
+ in the Software without restriction, including without limitation the rights
43
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
44
+ copies of the Software, and to permit persons to whom the Software is
45
+ furnished to do so, subject to the following conditions:
46
+
47
+ The above copyright notice and this permission notice shall be included in
48
+ all copies or substantial portions of the Software.
49
+
50
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
53
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
54
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
55
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
56
+ THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'faraday'
4
+ require 'tinder/faraday_response'
5
+
6
+ require 'active_support/core_ext/numeric/time'
7
+ require 'active_support/core_ext/time/acts_like'
8
+ require 'active_support/core_ext/time/calculations'
9
+ require 'active_support/core_ext/hash/indifferent_access'
10
+ require 'active_support/core_ext/hash/reverse_merge'
11
+ require 'active_support/core_ext/array/wrap'
12
+ require 'active_support/core_ext/hash/slice'
13
+ require 'active_support/inflector'
14
+
15
+ require 'pingdom'
16
+ require 'pingdom/client'
17
+ require 'pingdom/base'
18
+ require 'pingdom/check'
19
+ require 'pingdom/result'
20
+ require 'pingdom/probe'
21
+ require 'pingdom/contact'
22
+ require 'pingdom/summary'
23
+ require 'pingdom/summary/average'
24
+ require 'pingdom/summary/outage'
25
+ require 'pingdom/summary/performance'
data/lib/pingdom.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Pingdom
2
+
3
+ VERSION = '0.0.6.alpha'
4
+
5
+ class Error < RuntimeError
6
+ end
7
+
8
+ end
@@ -0,0 +1,54 @@
1
+ module Pingdom
2
+ class Base
3
+
4
+ def initialize(client, response, attributes = {})
5
+ @client = client
6
+ @response = response
7
+ @attributes = attributes
8
+ end
9
+
10
+ def self.attributes(hash)
11
+ hash.each do |(attribute, aliases)|
12
+ class_eval <<-"end;" unless instance_methods.include?(attribute.to_s)
13
+ def #{attribute}
14
+ @attributes[:#{attribute}]
15
+ end
16
+ end;
17
+
18
+ Array.wrap(aliases).each do |aliased|
19
+ alias_method aliased, attribute
20
+ end
21
+ end
22
+ end
23
+
24
+ def method_missing(name, *args, &block)
25
+ @attributes[name] or super
26
+ end
27
+
28
+ def respond_to?(name)
29
+ super(name) || @attributes.key?(name)
30
+ end
31
+
32
+ def id
33
+ @attributes[:id]
34
+ end
35
+
36
+ def inspect
37
+ "#<%s %s>" % [self.class.to_s, @attributes.inject([]){ |a, (k,v)| a << "%s: %s" % [k,v.inspect]; a }.join(' ')]
38
+ end
39
+
40
+ def self.check_error!(response)
41
+ if response.body.key?(:error)
42
+ raise Error, "%s (%s %s)" % [ response.body[:error][:errormessage],
43
+ response.body[:error][:statuscode],
44
+ response.body[:error][:statusdesc] ]
45
+ end
46
+ end
47
+
48
+ def self.parse(client, response)
49
+ check_error!(response)
50
+ response.body
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,34 @@
1
+ module Pingdom
2
+
3
+ # {"name"=>"Autocomplete", "id"=>259103, "type"=>"http", "lastresponsetime"=>203173, "status"=>"up", "lasttesttime"=>1298102416}
4
+ class Check < Base
5
+ def self.parse(client, response)
6
+ checks = super
7
+ Array.wrap(checks[:checks] || checks[:check]).map do |check|
8
+ new(client, response, check)
9
+ end
10
+ end
11
+
12
+ attributes :lastresponsetime => :last_response_time,
13
+ :lasttesttime => :last_test_time,
14
+ :lasterrortime => :last_error_time
15
+
16
+ def results(options = {})
17
+ @client.results(id, options)
18
+ end
19
+
20
+ def summary
21
+ @client.summary(id)
22
+ end
23
+
24
+ def lasttesttime
25
+ Time.at(super)
26
+ end
27
+
28
+ def lasterrortime
29
+ Time.at(super)
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,89 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'pingdom-ruby') unless defined? Pingdom
2
+
3
+ module Pingdom
4
+ class Client
5
+
6
+ attr_accessor :limit
7
+
8
+ def initialize(options = {})
9
+ @options = options.with_indifferent_access.reverse_merge(:http_driver => :excon)
10
+
11
+ raise ArgumentError, "an application key must be provided (as :key)" unless @options.key?(:key)
12
+
13
+ @connection = Faraday::Connection.new(:url => "https://api/pingdom.com/api/2.0/") do |builder|
14
+ builder.url_prefix = "https://api.pingdom.com/api/2.0"
15
+
16
+ builder.adapter :logger, @options[:logger]
17
+
18
+ builder.adapter @options[:http_driver]
19
+
20
+ # builder.use Gzip # TODO: write GZip response handler, add Accept-Encoding: gzip header
21
+ builder.response :yajl
22
+ builder.use Tinder::FaradayResponse::WithIndifferentAccess
23
+
24
+ builder.basic_auth @options[:username], @options[:password]
25
+ end
26
+ end
27
+
28
+ # probes => [1,2,3] #=> probes => "1,2,3"
29
+ def prepare_params(options)
30
+ options.each do |(key, value)|
31
+ options[key] = Array.wrap(value).map(&:to_s).join(',')
32
+ options[key] = value.to_i if value.acts_like?(:time)
33
+ end
34
+
35
+ options
36
+ end
37
+
38
+ def get(uri, params = {}, &block)
39
+ response = @connection.get(@connection.build_url(uri, prepare_params(params)), "App-Key" => @options[:key], &block)
40
+ update_limits!(response.headers['req-limit-short'], response.headers['req-limit-long'])
41
+ response
42
+ end
43
+
44
+ def update_limits!(short, long)
45
+ @limit ||= {}
46
+ @limit[:short] = parse_limit(short)
47
+ @limit[:long] = parse_limit(long)
48
+ @limit
49
+ end
50
+
51
+ # "Remaining: 394 Time until reset: 3589"
52
+ def parse_limit(limit)
53
+ if limit.to_s =~ /Remaining: (\d+) Time until reset: (\d+)/
54
+ { :remaining => $1.to_i,
55
+ :resets_at => $2.to_i.seconds.from_now }
56
+ end
57
+ end
58
+
59
+ def test!(options = {})
60
+ Result.parse(self, get("single", options)).first
61
+ end
62
+
63
+ def checks(options = {})
64
+ Check.parse(self, get("checks", options))
65
+ end
66
+ def check(id)
67
+ Check.parse(self, get("checks/#{id}")).first
68
+ end
69
+
70
+ # Check ID
71
+ def results(id, options = {})
72
+ options.reverse_merge!(:includeanalysis => true)
73
+ Result.parse(self, get("results/#{id}", options))
74
+ end
75
+
76
+ def probes(options = {})
77
+ Probe.parse(self, get("probes", options))
78
+ end
79
+
80
+ def contacts(options = {})
81
+ Contact.parse(self, get("contacts", options))
82
+ end
83
+
84
+ def summary(id)
85
+ Summary.proxy(self, id)
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,16 @@
1
+ module Pingdom
2
+
3
+ # {"name"=>"Larry Bernstein", "directtwitter"=>false, "id"=>142762, "cellphone"=>"1-510-501-7401",
4
+ # "paused"=>false, "defaultsmsprovider"=>"clickatell", "email"=>"lbernstein@demandbase.com"}
5
+ class Contact < Base
6
+ def self.parse(client, response)
7
+ super[:contacts].map do |contact|
8
+ new(client, response, contact)
9
+ end
10
+ end
11
+
12
+ attributes :cellphone => :phone
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,20 @@
1
+ module Pingdom
2
+
3
+ # {"city"=>"Manchester", "name"=>"Manchester, UK", "country"=>"United Kingdom",
4
+ # "countryiso"=>"GB", "id"=>46, "ip"=>"212.84.74.156", "hostname"=>"s424.pingdom.com", "active"=>true}
5
+ class Probe < Base
6
+ def self.parse(client, response)
7
+ super[:probes].map do |probe|
8
+ new(client, response, probe)
9
+ end
10
+ end
11
+
12
+ attributes :countryiso => [:country_iso, :country_code]
13
+
14
+ def test!(options)
15
+ @client.test!(options.merge(:probeid => id))
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,20 @@
1
+ module Pingdom
2
+
3
+ # {"statusdesclong"=>"OK", "probeid"=>28, "responsetime"=>221, "statusdesc"=>"OK", "status"=>"up", "probedesc"=>"Amsterdam 2, Netherlands"}
4
+ class Result < Base
5
+ def self.parse(client, response)
6
+ results = super
7
+ Array.wrap(results[:results] || results[:result]).map do |result|
8
+ new(client, response, result)
9
+ end
10
+ end
11
+
12
+ attributes :responsetime => :response_time,
13
+ :probeid => :probe_id
14
+
15
+ def probe
16
+ @client.probes.detect{ |probe| probe.id == probe_id }
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,44 @@
1
+ module Pingdom
2
+
3
+ class Summary < Base
4
+
5
+ class Proxy < Struct.new(:client, :check_id)
6
+ def average(options = {})
7
+ options.reverse_merge!(:byprobe => true, :includeuptime => true)
8
+ Average.parse(client, client.get("summary.average/#{check_id}", options))
9
+ end
10
+ alias_method :averages, :average
11
+
12
+ def outage(options = {})
13
+ options.reverse_merge!(:byprobe => true, :includeuptime => true)
14
+ Outage.parse(client, client.get("summary.outage/#{check_id}", options))
15
+ end
16
+ alias_method :outages, :outage
17
+
18
+ def performance(options = {})
19
+ options.reverse_merge!(:resolution => :day, :includeuptime => true)
20
+ Performance.parse(client, client.get("summary.performance/#{check_id}", options))
21
+ end
22
+ end
23
+
24
+ def self.proxy(client, check)
25
+ Proxy.new(client, check)
26
+ end
27
+
28
+ def from
29
+ Time.at(@attributes[:from])
30
+ end
31
+
32
+ def to
33
+ Time.at(@attributes[:to])
34
+ end
35
+
36
+ attributes :responsetime => :response_time
37
+
38
+ # {"status"=>{"totalup"=>5035757, "totalunknown"=>1293069551, "totaldown"=>5078}}
39
+ class Status < Base
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,47 @@
1
+ module Pingdom
2
+ class Summary
3
+
4
+ # summary.average includeuptime probes=34,35 byprobe
5
+ # { "responsetime"=>{
6
+ # "from"=>0, "to"=>1298110456, "probes"=>"34, 35", "avgresponse"=>[
7
+ # {"probeid"=>35, "avgresponse"=>94},
8
+ # {"probeid"=>34, "avgresponse"=>125} ]},
9
+ # "status"=>{"totalup"=>5035757, "totalunknown"=>1293069551, "totaldown"=>5078}}
10
+ class Average < Base
11
+ def self.parse(client, response)
12
+ body = super[:summary]
13
+ sum = body[:responsetime]
14
+ attrs = sum.slice(:from, :to)
15
+ attrs[:probes] = (attrs[:probes] || "").gsub(/\w+/, '').split(',').map{|e| e.to_i }
16
+
17
+ sum[:status] = Status.new(client, response, body[:status]) if body.key?(:status)
18
+
19
+ case sum[:avgresponse]
20
+ when Array
21
+ sum[:responsetime] = 0
22
+ sum[:averages] =
23
+ sum.delete(:avgresponse).map do |avg|
24
+ sum[:responsetime] += avg[:avgresponse]
25
+ new(client, response, avg)
26
+ end
27
+ sum[:responsetime] = sum[:responsetime] / sum[:averages].size if sum[:averages].size > 0
28
+
29
+ when Integer
30
+ sum[:responsetime] = sum.delete(:avgresponse)
31
+
32
+ end
33
+
34
+ sum = Summary.new(client, response, sum)
35
+ end
36
+
37
+ attributes :probeid => :probe_id,
38
+ :responsetime => :response_time
39
+
40
+ def probe
41
+ @client.probes.detect{ |probe| probe.id == probe_id }
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,35 @@
1
+ module Pingdom
2
+ class Summary
3
+
4
+ # summary.outage
5
+ # {"states"=>[{"timeto"=>1297587576, "timefrom"=>1297475316, "status"=>"up"},
6
+ # {"timeto"=>1297587906, "timefrom"=>1297587576, "status"=>"down"},
7
+ # {"timeto"=>1298110749, "timefrom"=>1297587906, "status"=>"up"}]}
8
+ class Outage < Base
9
+ def self.parse(client, response)
10
+ super[:summary][:states].
11
+ select{ |s| s[:status] == "down" }.
12
+ map do |outage|
13
+ new(client, response, outage)
14
+ end
15
+ end
16
+
17
+ def downtime
18
+ (@attributes[:timeto] - @attributes[:timefrom]).seconds
19
+ end
20
+
21
+ def timefrom
22
+ Time.at(@attributes[:timefrom])
23
+ end
24
+
25
+ def timeto
26
+ Time.at(@attributes[:timeto])
27
+ end
28
+
29
+ attributes :timefrom => [:time_from, :from],
30
+ :timeto => [:time_to, :to,]
31
+
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,65 @@
1
+ module Pingdom
2
+ class Summary
3
+
4
+ # summary.performance includeuptime resolution=day
5
+ # {"days"=>[{"unmonitored"=>0, "downtime"=>0, "starttime"=>1297238400, "uptime"=>86400, "avgresponse"=>234},
6
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1297324800, "uptime"=>86400, "avgresponse"=>215},
7
+ # {"unmonitored"=>0, "downtime"=>2648, "starttime"=>1297411200, "uptime"=>83752, "avgresponse"=>211},
8
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1297497600, "uptime"=>86400, "avgresponse"=>207},
9
+ # {"unmonitored"=>0, "downtime"=>330, "starttime"=>1297584000, "uptime"=>86070, "avgresponse"=>228},
10
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1297670400, "uptime"=>86400, "avgresponse"=>236},
11
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1297756800, "uptime"=>86400, "avgresponse"=>230},
12
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1297843200, "uptime"=>86400, "avgresponse"=>256},
13
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1297929600, "uptime"=>86400, "avgresponse"=>216},
14
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1298016000, "uptime"=>86400, "avgresponse"=>251},
15
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1298102400, "uptime"=>8646, "avgresponse"=>223}]}
16
+ class Performance < Base
17
+ INTERVALS = {
18
+ "hour" => 1.hour,
19
+ "day" => 1.day,
20
+ "week" => 1.week
21
+ }
22
+
23
+ def self.parse(client, response)
24
+ body = super[:summary]
25
+ interval = body.keys.detect{ |k| INTERVALS.keys.include?(k.chomp('s').to_s) }.chomp('s').to_sym
26
+ intervals = body[interval.to_s.pluralize]
27
+
28
+ intervals.map do |perf|
29
+ perf[:interval] = interval
30
+ new(client, response, perf)
31
+ end
32
+ end
33
+
34
+ def starttime
35
+ Time.at(@attributes[:starttime])
36
+ end
37
+ alias_method :start_at, :starttime
38
+
39
+ def endtime
40
+ starttime + INTERVALS[interval.to_s].to_i
41
+ end
42
+ alias_method :end_at, :endtime
43
+
44
+ def uptime
45
+ @attributes[:uptime].seconds
46
+ end
47
+ def downtime
48
+ @attributes[:downtime].seconds
49
+ end
50
+ def unmonitored
51
+ @attributes[:unmonitored].seconds
52
+ end
53
+ def monitored
54
+ uptime + downtime
55
+ end
56
+ def period
57
+ monitored + unmonitored
58
+ end
59
+
60
+ attributes :avgresponse => :response_time
61
+
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,56 @@
1
+ # Taken from:
2
+ # https://github.com/collectiveidea/tinder/raw/master/lib/tinder/middleware.rb
3
+ # See:
4
+ # https://github.com/collectiveidea/tinder/blob/master/MIT-LICENSE
5
+ #
6
+ # Copyright (c) 2006-2010 Brandon Keepers, Collective Idea
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining
9
+ # a copy of this software and associated documentation files (the
10
+ # "Software"), to deal in the Software without restriction, including
11
+ # without limitation the rights to use, copy, modify, merge, publish,
12
+ # distribute, sublicense, and/or sell copies of the Software, and to
13
+ # permit persons to whom the Software is furnished to do so, subject to
14
+ # the following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be
17
+ # included in all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
22
+ # NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ #
27
+ module Tinder
28
+ module FaradayResponse
29
+ class WithIndifferentAccess < ::Faraday::Response::Middleware
30
+ begin
31
+ require 'active_support/core_ext/hash/indifferent_access'
32
+ rescue LoadError, NameError => error
33
+ self.load_error = error
34
+ end
35
+
36
+ def self.register_on_complete(env)
37
+ env[:response].on_complete do |response|
38
+ json = response[:body]
39
+ if json.is_a?(Hash)
40
+ response[:body] = ::HashWithIndifferentAccess.new(json)
41
+ elsif json.is_a?(Array) and json.first.is_a?(Hash)
42
+ response[:body] = json.map{|item| ::HashWithIndifferentAccess.new(item) }
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ class RaiseOnAuthenticationFailure < ::Faraday::Response::Middleware
49
+ def self.register_on_complete(env)
50
+ env[:response].on_complete do |response|
51
+ raise AuthenticationFailed if response[:status] == 401
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,39 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{pingdom-ruby}
3
+ s.version = "1.0.0"
4
+
5
+ s.authors = ["Jason Straughan"]
6
+ s.date = %q{2013-10-05}
7
+ s.description = %q{Pingdom Ruby Client}
8
+ s.email = %q{jasons@grok-interactive.com}
9
+ s.files = [
10
+ "pingdom-ruby.gemspec",
11
+ "Gemfile",
12
+ "Gemfile.lock",
13
+ "lib/pingdom/base.rb",
14
+ "lib/pingdom/check.rb",
15
+ "lib/pingdom/client.rb",
16
+ "lib/pingdom/contact.rb",
17
+ "lib/pingdom/probe.rb",
18
+ "lib/pingdom/result.rb",
19
+ "lib/pingdom/summary/average.rb",
20
+ "lib/pingdom/summary/outage.rb",
21
+ "lib/pingdom/summary/performance.rb",
22
+ "lib/pingdom/summary.rb",
23
+ "lib/pingdom-ruby.rb",
24
+ "lib/pingdom.rb",
25
+ "lib/tinder/faraday_response.rb",
26
+ "Rakefile",
27
+ "Readme.md",
28
+ "spec/pingdom-ruby_spec.rb",
29
+ "spec/spec_helper.rb"
30
+ ]
31
+ s.licenses = ["MIT"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.7}
34
+ s.summary = %q{Pingdom Ruby Client}
35
+ s.test_files = [
36
+ "spec/spec_helper.rb",
37
+ "spec/pingdom-ruby_spec.rb"
38
+ ]
39
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pingdom::Client do
4
+ let(:client){ Pingdom::Client.new(CREDENTIALS.merge(:logger => LOGGER)) }
5
+
6
+ describe "#test!" do
7
+ it "should test a single endpoint" do
8
+ response = client.test!(:host => "pingdom.com", :type => "http")
9
+
10
+ response.status.should == "up"
11
+ response.responsetime.should be_a(Numeric)
12
+ end
13
+ end
14
+
15
+ describe "#checks" do
16
+ it "should get a list of checks" do
17
+ checks = client.checks
18
+
19
+ first = checks.first
20
+ first.should be_a(Pingdom::Check)
21
+ first.last_response_time.should be_a(Numeric)
22
+ end
23
+ end
24
+
25
+ describe "#limit" do
26
+ { :short => "short term",
27
+ :long => "long term" }.each do |(key, label)|
28
+ describe label do
29
+ let(:limit){ client.test!(:host => "pingdom.com", :type => "http"); client.limit[key] }
30
+
31
+ it "should indicate how many requests can be made" do
32
+ limit[:remaining].should be_a(Numeric)
33
+ end
34
+
35
+ it "should indicate when the current limit will be reset" do
36
+ limit[:resets_at].acts_like?(:time).should be_true
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,10 @@
1
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'pingdom-ruby'
5
+
6
+ require 'logger'
7
+ require 'rspec'
8
+
9
+ LOGGER = Logger.new(File.join(File.dirname(__FILE__), 'test.log'))
10
+ CREDENTIALS = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'credentials.yml')).with_indifferent_access
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pingdom-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Straughan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Pingdom Ruby Client
14
+ email: jasons@grok-interactive.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - pingdom-ruby.gemspec
20
+ - Gemfile
21
+ - Gemfile.lock
22
+ - lib/pingdom/base.rb
23
+ - lib/pingdom/check.rb
24
+ - lib/pingdom/client.rb
25
+ - lib/pingdom/contact.rb
26
+ - lib/pingdom/probe.rb
27
+ - lib/pingdom/result.rb
28
+ - lib/pingdom/summary/average.rb
29
+ - lib/pingdom/summary/outage.rb
30
+ - lib/pingdom/summary/performance.rb
31
+ - lib/pingdom/summary.rb
32
+ - lib/pingdom-ruby.rb
33
+ - lib/pingdom.rb
34
+ - lib/tinder/faraday_response.rb
35
+ - Rakefile
36
+ - Readme.md
37
+ - spec/pingdom-ruby_spec.rb
38
+ - spec/spec_helper.rb
39
+ homepage:
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.1.5
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Pingdom Ruby Client
63
+ test_files:
64
+ - spec/spec_helper.rb
65
+ - spec/pingdom-ruby_spec.rb
66
+ has_rdoc: