Class: Pingdom::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pingdom/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#limitObject

Returns the value of attribute limit.



5
6
7
# File 'lib/pingdom/client.rb', line 5

def limit
  @limit
end

Instance Method Details

#check(id) ⇒ Object



63
64
65
# File 'lib/pingdom/client.rb', line 63

def check(id)
  Check.parse(self, get("checks/#{id}")).first
end

#checks(options = {}) ⇒ Object



59
60
61
# File 'lib/pingdom/client.rb', line 59

def checks(options = {})
  Check.parse(self, get("checks", options))
end

#contacts(options = {}) ⇒ Object



85
86
87
# File 'lib/pingdom/client.rb', line 85

def contacts(options = {})
  Contact.parse(self, get("contacts", options))
end

#get(uri, params = {}, &block) ⇒ Object



34
35
36
37
38
# 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) ⇒ Object

“Remaining: 394 Time until reset: 3589”



48
49
50
51
52
53
# 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) ⇒ Object

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



25
26
27
28
29
30
31
32
# 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 = {}) ⇒ Object



81
82
83
# File 'lib/pingdom/client.rb', line 81

def probes(options = {})
  Probe.parse(self, get("probes", options))
end

#results(id, options = {}) ⇒ Object

Check ID



76
77
78
79
# 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) ⇒ Object



89
90
91
# File 'lib/pingdom/client.rb', line 89

def summary(id)
  Summary.proxy(self, id)
end

#test!(options = {}) ⇒ Object



55
56
57
# File 'lib/pingdom/client.rb', line 55

def test!(options = {})
  Result.parse(self, get("single", options)).first
end

#tms_recipe(id) ⇒ Object



71
72
73
# File 'lib/pingdom/client.rb', line 71

def tms_recipe(id)
  TMSRecipe.parse(self, get("tms.recipes/#{id}")).first
end

#tms_recipes(options = {}) ⇒ Object



67
68
69
# File 'lib/pingdom/client.rb', line 67

def tms_recipes(options = {})
  TMSRecipe.parse(self, get("tms.recipes", options))
end

#tms_summary(id) ⇒ Object



93
94
95
# File 'lib/pingdom/client.rb', line 93

def tms_summary(id)
  TMSSummary.proxy(self, id)
end

#update_limits!(short, long) ⇒ Object



40
41
42
43
44
45
# 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