pingdom-faraday 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f26cf1e85c2425f106961017c6f5c6d437aeafe
4
+ data.tar.gz: 324b806d70787226dd6e39e3ef8d9f95ab171747
5
+ SHA512:
6
+ metadata.gz: 20ce30814659bfd92feee570ae769930df22a7377fd10556695ad3a463f26ae16e157b0165c840c2f1b26344df904355edce668472d8df25b10c122c66b461ea
7
+ data.tar.gz: 917fa1bc04dff18edab52c659808e19033581364d899a90defe5c1c7af976758408d12b17cbf5cdd8f81c175666d2d650248ce6bafdaf10b9c8ce72d328dd3a3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pingdom-faraday (0.0.2)
5
+ activesupport (~> 4.2)
6
+ faraday
7
+ faraday_middleware
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (4.2.3)
13
+ i18n (~> 0.7)
14
+ json (~> 1.7, >= 1.7.7)
15
+ minitest (~> 5.1)
16
+ thread_safe (~> 0.3, >= 0.3.4)
17
+ tzinfo (~> 1.1)
18
+ ast (2.1.0)
19
+ astrolabe (1.3.1)
20
+ parser (~> 2.2)
21
+ diff-lcs (1.2.5)
22
+ faraday (0.9.1)
23
+ multipart-post (>= 1.2, < 3)
24
+ faraday_middleware (0.10.0)
25
+ faraday (>= 0.7.4, < 0.10)
26
+ i18n (0.7.0)
27
+ json (1.8.3)
28
+ minitest (5.8.0)
29
+ multipart-post (2.0.0)
30
+ parser (2.2.2.6)
31
+ ast (>= 1.1, < 3.0)
32
+ powerpack (0.1.1)
33
+ rainbow (2.0.0)
34
+ rspec (3.3.0)
35
+ rspec-core (~> 3.3.0)
36
+ rspec-expectations (~> 3.3.0)
37
+ rspec-mocks (~> 3.3.0)
38
+ rspec-core (3.3.2)
39
+ rspec-support (~> 3.3.0)
40
+ rspec-expectations (3.3.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.3.0)
43
+ rspec-mocks (3.3.2)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.3.0)
46
+ rspec-support (3.3.0)
47
+ rubocop (0.33.0)
48
+ astrolabe (~> 1.3)
49
+ parser (>= 2.2.2.5, < 3.0)
50
+ powerpack (~> 0.1)
51
+ rainbow (>= 1.99.1, < 3.0)
52
+ ruby-progressbar (~> 1.4)
53
+ ruby-progressbar (1.7.5)
54
+ thread_safe (0.3.5)
55
+ tzinfo (1.2.2)
56
+ thread_safe (~> 0.1)
57
+
58
+ PLATFORMS
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ pingdom-faraday!
63
+ rspec (~> 3.3)
64
+ rubocop
65
+
66
+ BUNDLED WITH
67
+ 1.10.6
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.
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
@@ -0,0 +1,52 @@
1
+ module Pingdom
2
+ class Base
3
+ def initialize(client, response, attributes = {})
4
+ @client = client
5
+ @response = response
6
+ @attributes = attributes
7
+ end
8
+
9
+ def self.attributes(hash)
10
+ hash.each do |(attribute, aliases)|
11
+ class_eval <<-"end;" unless instance_methods.include?(attribute.to_s)
12
+ def #{attribute}
13
+ @attributes["#{attribute}"]
14
+ end
15
+ end;
16
+
17
+ Array.wrap(aliases).each do |aliased|
18
+ alias_method aliased, attribute
19
+ end
20
+ end
21
+ end
22
+
23
+ def method_missing(name, *args, &block)
24
+ @attributes[name.to_s] or super
25
+ end
26
+
27
+ def respond_to?(name)
28
+ super(name) || @attributes.key?(name.to_s)
29
+ end
30
+
31
+ def id
32
+ @attributes["id"]
33
+ end
34
+
35
+ def inspect
36
+ "#<%s %s>" % [self.class.to_s, @attributes.reduce([]) { |a, (k, v)| a << "%s: %s" % [k, v.inspect]; a }.join(" ")]
37
+ end
38
+
39
+ def self.check_error!(response)
40
+ if response.body.key?("error")
41
+ raise Error, "%s (%s %s)" % [response.body["error"]["errormessage"],
42
+ response.body["error"]["statuscode"],
43
+ response.body["error"]["statusdesc"]]
44
+ end
45
+ end
46
+
47
+ def self.parse(_client, response)
48
+ check_error!(response)
49
+ response.body
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,31 @@
1
+ module Pingdom
2
+ # {"name"=>"Autocomplete", "id"=>259103, "type"=>"http", "lastresponsetime"=>203173, "status"=>"up", "lasttesttime"=>1298102416}
3
+ class Check < Base
4
+ def self.parse(client, response)
5
+ checks = super
6
+ Array.wrap(checks["checks"] || checks["check"]).map do |check|
7
+ new(client, response, check)
8
+ end
9
+ end
10
+
11
+ attributes lastresponsetime: :last_response_time,
12
+ lasttesttime: :last_test_time,
13
+ lasterrortime: :last_error_time
14
+
15
+ def results(options = {})
16
+ @client.results(id, options)
17
+ end
18
+
19
+ def summary
20
+ @client.summary(id)
21
+ end
22
+
23
+ def lasttesttime
24
+ Time.at(super)
25
+ end
26
+
27
+ def lasterrortime
28
+ Time.at(super)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,88 @@
1
+ require File.join(File.dirname(__FILE__), "..", "pingdom-ruby") unless defined? Pingdom
2
+
3
+ module Pingdom
4
+ class Client
5
+ attr_accessor :limit
6
+
7
+ def initialize(options = {})
8
+ @options = options.with_indifferent_access.reverse_merge(http_driver: Faraday.default_adapter)
9
+
10
+ raise ArgumentError, "an application key must be provided (as :key)" unless @options.key?(:key)
11
+
12
+ @connection = Faraday::Connection.new(url: "https://api/pingdom.com/api/2.0/", ssl: { verify: false }) do |builder|
13
+ builder.url_prefix = "https://api.pingdom.com/api/2.0"
14
+
15
+ builder.adapter @options[:http_driver]
16
+
17
+ # builder.use Gzip # TODO: write GZip response handler, add Accept-Encoding: gzip header
18
+ builder.response :json, content_type: /\bjson$/
19
+ builder.use Tinder::FaradayResponse::WithIndifferentAccess
20
+
21
+ builder.basic_auth @options[:username], @options[:password]
22
+ builder.headers["App-Key"] = @options[:key]
23
+ builder.headers["Account-Email"] = @options[:account_email] if @options[:account_email]
24
+ end
25
+ end
26
+
27
+ # probes => [1,2,3] #=> probes => "1,2,3"
28
+ def prepare_params(options)
29
+ options.each do |(key, value)|
30
+ options[key] = Array.wrap(value).map(&:to_s).join(",")
31
+ options[key] = value.to_i if value.acts_like?(:time)
32
+ end
33
+
34
+ options
35
+ end
36
+
37
+ def get(uri, params = {}, &block)
38
+ response = @connection.get(@connection.build_url(uri, prepare_params(params)), &block)
39
+ update_limits!(response.headers["req-limit-short"], response.headers["req-limit-long"])
40
+ response
41
+ end
42
+
43
+ def update_limits!(short, long)
44
+ @limit ||= {}
45
+ @limit[:short] = parse_limit(short)
46
+ @limit[:long] = parse_limit(long)
47
+ @limit
48
+ end
49
+
50
+ # "Remaining: 394 Time until reset: 3589"
51
+ def parse_limit(limit)
52
+ if limit.to_s =~ /Remaining: (\d+) Time until reset: (\d+)/
53
+ { remaining: $1.to_i,
54
+ resets_at: $2.to_i.seconds.from_now }
55
+ end
56
+ end
57
+
58
+ def test!(options = {})
59
+ Result.parse(self, get("single", options)).first
60
+ end
61
+
62
+ def checks(options = {})
63
+ Check.parse(self, get("checks", options))
64
+ end
65
+
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
+ end
88
+ end
@@ -0,0 +1,13 @@
1
+ module Pingdom
2
+ # {"name"=>"Larry Bernstein", "directtwitter"=>false, "id"=>142762, "cellphone"=>"1-510-501-7401",
3
+ # "paused"=>false, "defaultsmsprovider"=>"clickatell", "email"=>"lbernstein@demandbase.com"}
4
+ class Contact < Base
5
+ def self.parse(client, response)
6
+ super["contacts"].map do |contact|
7
+ new(client, response, contact)
8
+ end
9
+ end
10
+
11
+ attributes cellphone: :phone
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ module Pingdom
2
+ # {"city"=>"Manchester", "name"=>"Manchester, UK", "country"=>"United Kingdom",
3
+ # "countryiso"=>"GB", "id"=>46, "ip"=>"212.84.74.156", "hostname"=>"s424.pingdom.com", "active"=>true}
4
+ class Probe < Base
5
+ def self.parse(client, response)
6
+ super["probes"].map do |probe|
7
+ new(client, response, probe)
8
+ end
9
+ end
10
+
11
+ attributes countryiso: [:country_iso, :country_code]
12
+
13
+ def test!(options)
14
+ @client.test!(options.merge(probeid: id))
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ module Pingdom
2
+ # {"statusdesclong"=>"OK", "probeid"=>28, "responsetime"=>221, "statusdesc"=>"OK", "status"=>"up", "probedesc"=>"Amsterdam 2, Netherlands"}
3
+ class Result < Base
4
+ def self.parse(client, response)
5
+ results = super
6
+ Array.wrap(results["results"] || results["result"]).map do |result|
7
+ new(client, response, result)
8
+ end
9
+ end
10
+
11
+ attributes responsetime: :response_time,
12
+ probeid: :probe_id
13
+
14
+ def probe
15
+ @client.probes.detect { |probe| probe.id == probe_id }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ module Pingdom
2
+ class Summary
3
+ # summary.average includeuptime probes=34,35 byprobe
4
+ # { "responsetime"=>{
5
+ # "from"=>0, "to"=>1298110456, "probes"=>"34, 35", "avgresponse"=>[
6
+ # {"probeid"=>35, "avgresponse"=>94},
7
+ # {"probeid"=>34, "avgresponse"=>125} ]},
8
+ # "status"=>{"totalup"=>5035757, "totalunknown"=>1293069551, "totaldown"=>5078}}
9
+ class Average < Base
10
+ def self.parse(client, response)
11
+ body = super["summary"]
12
+ sum = body["responsetime"]
13
+ attrs = sum.slice("from", "to")
14
+ attrs["probes"] = (attrs["probes"] || "").gsub(/\w+/, "").split(",").map(&:to_i)
15
+
16
+ sum["status"] = Status.new(client, response, body["status"]) if body.key?("status")
17
+
18
+ case sum["avgresponse"]
19
+ when Array
20
+ sum["responsetime"] = 0
21
+ sum["averages"] = sum.delete("avgresponse").map do |avg|
22
+ sum["responsetime"] += avg["avgresponse"]
23
+ new(client, response, avg)
24
+ end
25
+ sum["responsetime"] = sum["responsetime"] / sum["averages"].size if sum["averages"].size > 0
26
+
27
+ when Integer
28
+ sum["responsetime"] = sum.delete("avgresponse")
29
+ end
30
+
31
+ sum = Summary.new(client, response, sum)
32
+ end
33
+
34
+ attributes probeid: :probe_id,
35
+ responsetime: :response_time
36
+
37
+ def probe
38
+ @client.probes.detect { |probe| probe.id == probe_id }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ module Pingdom
2
+ class Summary
3
+ # summary.outage
4
+ # {"states"=>[{"timeto"=>1297587576, "timefrom"=>1297475316, "status"=>"up"},
5
+ # {"timeto"=>1297587906, "timefrom"=>1297587576, "status"=>"down"},
6
+ # {"timeto"=>1298110749, "timefrom"=>1297587906, "status"=>"up"}]}
7
+ class Outage < Base
8
+ def self.parse(client, response)
9
+ super["summary"]["states"].select { |s| s["status"] == "down" }.
10
+ map do |outage|
11
+ new(client, response, outage)
12
+ end
13
+ end
14
+
15
+ def downtime
16
+ (@attributes["timeto"] - @attributes["timefrom"]).seconds
17
+ end
18
+
19
+ def timefrom
20
+ Time.at(@attributes["timefrom"])
21
+ end
22
+
23
+ def timeto
24
+ Time.at(@attributes["timeto"])
25
+ end
26
+
27
+ attributes timefrom: [:time_from, :from],
28
+ timeto: [:time_to, :to]
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,66 @@
1
+ module Pingdom
2
+ class Summary
3
+ # summary.performance includeuptime resolution=day
4
+ # {"days"=>[{"unmonitored"=>0, "downtime"=>0, "starttime"=>1297238400, "uptime"=>86400, "avgresponse"=>234},
5
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1297324800, "uptime"=>86400, "avgresponse"=>215},
6
+ # {"unmonitored"=>0, "downtime"=>2648, "starttime"=>1297411200, "uptime"=>83752, "avgresponse"=>211},
7
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1297497600, "uptime"=>86400, "avgresponse"=>207},
8
+ # {"unmonitored"=>0, "downtime"=>330, "starttime"=>1297584000, "uptime"=>86070, "avgresponse"=>228},
9
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1297670400, "uptime"=>86400, "avgresponse"=>236},
10
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1297756800, "uptime"=>86400, "avgresponse"=>230},
11
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1297843200, "uptime"=>86400, "avgresponse"=>256},
12
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1297929600, "uptime"=>86400, "avgresponse"=>216},
13
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1298016000, "uptime"=>86400, "avgresponse"=>251},
14
+ # {"unmonitored"=>0, "downtime"=>0, "starttime"=>1298102400, "uptime"=>8646, "avgresponse"=>223}]}
15
+ class Performance < Base
16
+ INTERVALS = {
17
+ "hour" => 1.hour,
18
+ "day" => 1.day,
19
+ "week" => 1.week,
20
+ }
21
+
22
+ def self.parse(client, response)
23
+ body = super["summary"]
24
+ interval = body.keys.detect { |k| INTERVALS.keys.include?(k.chomp("s").to_s) }.chomp("s").to_sym
25
+ intervals = body[interval.to_s.pluralize]
26
+
27
+ intervals.map do |perf|
28
+ perf["interval"] = interval
29
+ new(client, response, perf)
30
+ end
31
+ end
32
+
33
+ def starttime
34
+ Time.at(@attributes["starttime"])
35
+ end
36
+ alias_method :start_at, :starttime
37
+
38
+ def endtime
39
+ starttime + INTERVALS[interval.to_s].to_i
40
+ end
41
+ alias_method :end_at, :endtime
42
+
43
+ def uptime
44
+ @attributes["uptime"].seconds
45
+ end
46
+
47
+ def downtime
48
+ @attributes["downtime"].seconds
49
+ end
50
+
51
+ def unmonitored
52
+ @attributes["unmonitored"].seconds
53
+ end
54
+
55
+ def monitored
56
+ uptime + downtime
57
+ end
58
+
59
+ def period
60
+ monitored + unmonitored
61
+ end
62
+
63
+ attributes avgresponse: :response_time
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,40 @@
1
+ module Pingdom
2
+ class Summary < Base
3
+ class Proxy < Struct.new(:client, :check_id)
4
+ def average(options = {})
5
+ options.reverse_merge!(byprobe: true, includeuptime: true)
6
+ Average.parse(client, client.get("summary.average/#{check_id}", options))
7
+ end
8
+ alias_method :averages, :average
9
+
10
+ def outage(options = {})
11
+ options.reverse_merge!(byprobe: true, includeuptime: true)
12
+ Outage.parse(client, client.get("summary.outage/#{check_id}", options))
13
+ end
14
+ alias_method :outages, :outage
15
+
16
+ def performance(options = {})
17
+ options.reverse_merge!(resolution: :day, includeuptime: true)
18
+ Performance.parse(client, client.get("summary.performance/#{check_id}", options))
19
+ end
20
+ end
21
+
22
+ def self.proxy(client, check)
23
+ Proxy.new(client, check)
24
+ end
25
+
26
+ def from
27
+ Time.at(@attributes[:from])
28
+ end
29
+
30
+ def to
31
+ Time.at(@attributes[:to])
32
+ end
33
+
34
+ attributes responsetime: :response_time
35
+
36
+ # {"status"=>{"totalup"=>5035757, "totalunknown"=>1293069551, "totaldown"=>5078}}
37
+ class Status < Base
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,26 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require "faraday"
4
+ require "faraday_middleware"
5
+ require "tinder/faraday_response"
6
+
7
+ require "active_support/core_ext/numeric/time"
8
+ require "active_support/core_ext/time/acts_like"
9
+ require "active_support/core_ext/time/calculations"
10
+ require "active_support/core_ext/hash/indifferent_access"
11
+ require "active_support/core_ext/hash/reverse_merge"
12
+ require "active_support/core_ext/array/wrap"
13
+ require "active_support/core_ext/hash/slice"
14
+ require "active_support/inflector"
15
+
16
+ require "pingdom"
17
+ require "pingdom/client"
18
+ require "pingdom/base"
19
+ require "pingdom/check"
20
+ require "pingdom/result"
21
+ require "pingdom/probe"
22
+ require "pingdom/contact"
23
+ require "pingdom/summary"
24
+ require "pingdom/summary/average"
25
+ require "pingdom/summary/outage"
26
+ require "pingdom/summary/performance"
data/lib/pingdom.rb ADDED
@@ -0,0 +1,6 @@
1
+ module Pingdom
2
+ VERSION = "0.0.1"
3
+
4
+ class Error < RuntimeError
5
+ end
6
+ 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) && 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,44 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "pingdom-faraday"
3
+ s.version = "0.0.2"
4
+
5
+ s.authors = ["Andy Brennan", "Jason Straughan"]
6
+ s.homepage = "https://github.com/Valdez42/pingdom-faraday"
7
+ s.date = "2015-08-11"
8
+ s.description = "Pingdom Ruby Client"
9
+ s.email = ["andyvaldez75@gmail.com", "jasons@grok-interactive.com"]
10
+ s.files = [
11
+ "pingdom-faraday.gemspec",
12
+ "Gemfile",
13
+ "Gemfile.lock",
14
+ "lib/pingdom/base.rb",
15
+ "lib/pingdom/check.rb",
16
+ "lib/pingdom/client.rb",
17
+ "lib/pingdom/contact.rb",
18
+ "lib/pingdom/probe.rb",
19
+ "lib/pingdom/result.rb",
20
+ "lib/pingdom/summary/average.rb",
21
+ "lib/pingdom/summary/outage.rb",
22
+ "lib/pingdom/summary/performance.rb",
23
+ "lib/pingdom/summary.rb",
24
+ "lib/pingdom-faraday.rb",
25
+ "lib/pingdom.rb",
26
+ "lib/tinder/faraday_response.rb",
27
+ "Rakefile",
28
+ "README.md",
29
+ "spec/pingdom-faraday_spec.rb",
30
+ "spec/spec_helper.rb",
31
+ ]
32
+ s.licenses = ["MIT"]
33
+ s.require_paths = ["lib"]
34
+ s.add_dependency "faraday"
35
+ s.add_dependency "faraday_middleware"
36
+ s.add_dependency "activesupport", "~> 4.2"
37
+ s.add_development_dependency "rspec", "~> 3.3"
38
+ s.add_development_dependency "rubocop"
39
+ s.summary = "Pingdom Ruby Client"
40
+ s.test_files = [
41
+ "spec/spec_helper.rb",
42
+ "spec/pingdom-faraday_spec.rb",
43
+ ]
44
+ end
@@ -0,0 +1,43 @@
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
+ response.status.should == "up"
10
+ response.responsetime.should be_a(Numeric)
11
+ end
12
+ end
13
+
14
+ describe "#checks" do
15
+ it "should get a list of checks" do
16
+ checks = client.checks
17
+
18
+ first = checks.first
19
+ first.should be_a(Pingdom::Check)
20
+ first.last_response_time.should be_a(Numeric)
21
+ end
22
+ end
23
+
24
+ describe "#limit" do
25
+ { short: "short term",
26
+ long: "long term" }.each do |(key, label)|
27
+ describe label do
28
+ let(:limit) do
29
+ client.test!(host: "pingdom.com", type: "http")
30
+ client.limit[key]
31
+ end
32
+
33
+ it "should indicate how many requests can be made" do
34
+ limit[:remaining].should be_a(Numeric)
35
+ end
36
+
37
+ it "should indicate when the current limit will be reset" do
38
+ limit[:resets_at].acts_like?(:time).should be_true
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,11 @@
1
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
2
+ require "rubygems"
3
+ require "bundler/setup"
4
+ require "pingdom-faraday"
5
+
6
+ require "logger"
7
+ require "rspec"
8
+ require "yaml"
9
+
10
+ LOGGER = Logger.new(File.join(File.dirname(__FILE__), "test.log"))
11
+ CREDENTIALS = YAML.load_file(File.join(File.dirname(__FILE__), "..", "credentials.yml")).with_indifferent_access
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pingdom-faraday
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Andy Brennan
8
+ - Jason Straughan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-08-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: faraday_middleware
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: activesupport
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '4.2'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '4.2'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.3'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.3'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rubocop
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: Pingdom Ruby Client
85
+ email:
86
+ - andyvaldez75@gmail.com
87
+ - jasons@grok-interactive.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - README.md
95
+ - Rakefile
96
+ - lib/pingdom-faraday.rb
97
+ - lib/pingdom.rb
98
+ - lib/pingdom/base.rb
99
+ - lib/pingdom/check.rb
100
+ - lib/pingdom/client.rb
101
+ - lib/pingdom/contact.rb
102
+ - lib/pingdom/probe.rb
103
+ - lib/pingdom/result.rb
104
+ - lib/pingdom/summary.rb
105
+ - lib/pingdom/summary/average.rb
106
+ - lib/pingdom/summary/outage.rb
107
+ - lib/pingdom/summary/performance.rb
108
+ - lib/tinder/faraday_response.rb
109
+ - pingdom-faraday.gemspec
110
+ - spec/pingdom-faraday_spec.rb
111
+ - spec/spec_helper.rb
112
+ homepage: https://github.com/Valdez42/pingdom-faraday
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.4.8
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Pingdom Ruby Client
136
+ test_files:
137
+ - spec/spec_helper.rb
138
+ - spec/pingdom-faraday_spec.rb