Class: Watchfor::Client

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

Overview

Client for the WatchFor REST API (/api/v1). Zero dependencies.

wf = Watchfor::Client.new(api_key: "wf_live_...")
wf.summary
wf.monitors.list["data"].each { |m| puts "#{m['name']} #{m['status']}" }

Defined Under Namespace

Classes: AlertRules, ContactGroups, Contacts, Incidents, MaintenanceWindows, Monitors, Namespace

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: 30) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/watchfor/client.rb', line 31

def initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: 30)
  raise ArgumentError, "api_key is required" if api_key.nil? || api_key.empty?

  @api_key = api_key
  @base_url = base_url.chomp("/")
  @timeout = timeout
  @monitors = Monitors.new(self)
  @alert_rules = AlertRules.new(self)
  @incidents = Incidents.new(self)
  @maintenance_windows = MaintenanceWindows.new(self)
  @contacts = Contacts.new(self)
  @contact_groups = ContactGroups.new(self)
end

Instance Attribute Details

#alert_rulesObject (readonly)

Returns the value of attribute alert_rules.



28
29
30
# File 'lib/watchfor/client.rb', line 28

def alert_rules
  @alert_rules
end

#contact_groupsObject (readonly)

Returns the value of attribute contact_groups.



28
29
30
# File 'lib/watchfor/client.rb', line 28

def contact_groups
  @contact_groups
end

#contactsObject (readonly)

Returns the value of attribute contacts.



28
29
30
# File 'lib/watchfor/client.rb', line 28

def contacts
  @contacts
end

#incidentsObject (readonly)

Returns the value of attribute incidents.



28
29
30
# File 'lib/watchfor/client.rb', line 28

def incidents
  @incidents
end

#maintenance_windowsObject (readonly)

Returns the value of attribute maintenance_windows.



28
29
30
# File 'lib/watchfor/client.rb', line 28

def maintenance_windows
  @maintenance_windows
end

#monitorsObject (readonly)

Returns the value of attribute monitors.



28
29
30
# File 'lib/watchfor/client.rb', line 28

def monitors
  @monitors
end

Instance Method Details

#activity(**query) ⇒ Object



92
# File 'lib/watchfor/client.rb', line 92

def activity(**query) = request("GET", "/activity", query: query)

#incident_stats(period = nil) ⇒ Object



88
# File 'lib/watchfor/client.rb', line 88

def incident_stats(period = nil) = request("GET", "/incidents/stats", query: { period: period })

#locationsObject



89
# File 'lib/watchfor/client.rb', line 89

def locations = request("GET", "/locations")

#meObject

Top-level reads



85
# File 'lib/watchfor/client.rb', line 85

def me = request("GET", "/me")

#monitor_typesObject



90
# File 'lib/watchfor/client.rb', line 90

def monitor_types = request("GET", "/meta/monitor-types")

#notifications(**query) ⇒ Object



91
# File 'lib/watchfor/client.rb', line 91

def notifications(**query) = request("GET", "/notifications", query: query)

#planObject



87
# File 'lib/watchfor/client.rb', line 87

def plan = request("GET", "/plan")

#request(method, path, query: nil, body: nil, idempotency_key: nil) ⇒ Object

Core request. query and body are optional hashes.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/watchfor/client.rb', line 46

def request(method, path, query: nil, body: nil, idempotency_key: nil)
  uri = URI("#{@base_url}#{path}")
  unless query.nil? || query.empty?
    cleaned = query.reject { |_, v| v.nil? || v == "" }
    uri.query = URI.encode_www_form(cleaned) unless cleaned.empty?
  end

  klass = {
    "GET" => Net::HTTP::Get, "POST" => Net::HTTP::Post,
    "PATCH" => Net::HTTP::Patch, "PUT" => Net::HTTP::Put,
    "DELETE" => Net::HTTP::Delete
  }.fetch(method)
  req = klass.new(uri)
  req["Authorization"] = "Bearer #{@api_key}"
  req["Accept"] = "application/json"
  req["User-Agent"] = "watchfor-ruby/#{Watchfor::VERSION}"
  req["Idempotency-Key"] = idempotency_key if idempotency_key
  unless body.nil?
    req["Content-Type"] = "application/json"
    req.body = JSON.generate(body)
  end

  res = Net::HTTP.start(uri.hostname, uri.port,
                        use_ssl: uri.scheme == "https",
                        open_timeout: @timeout, read_timeout: @timeout) do |http|
    http.request(req)
  end

  raw = res.body.to_s
  parsed = raw.empty? ? nil : (JSON.parse(raw) rescue nil)
  unless res.code.to_i.between?(200, 299)
    err = parsed.is_a?(Hash) ? (parsed["error"] || {}) : {}
    raise Error.new(res.code.to_i, err["code"] || "http_error",
                    err["message"] || "HTTP #{res.code}")
  end
  parsed.nil? ? { "ok" => true, "status" => res.code.to_i } : parsed
end

#summaryObject



86
# File 'lib/watchfor/client.rb', line 86

def summary = request("GET", "/summary")