Class: Watchfor::Client
- Inherits:
-
Object
- Object
- Watchfor::Client
- Defined in:
- lib/watchfor/client.rb
Overview
Defined Under Namespace
Classes: AlertRules, ContactGroups, Contacts, Incidents, MaintenanceWindows, Monitors, Namespace
Instance Attribute Summary collapse
-
#alert_rules ⇒ Object
readonly
Returns the value of attribute alert_rules.
-
#contact_groups ⇒ Object
readonly
Returns the value of attribute contact_groups.
-
#contacts ⇒ Object
readonly
Returns the value of attribute contacts.
-
#incidents ⇒ Object
readonly
Returns the value of attribute incidents.
-
#maintenance_windows ⇒ Object
readonly
Returns the value of attribute maintenance_windows.
-
#monitors ⇒ Object
readonly
Returns the value of attribute monitors.
Instance Method Summary collapse
- #activity(**query) ⇒ Object
- #incident_stats(period = nil) ⇒ Object
-
#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: 30) ⇒ Client
constructor
A new instance of Client.
- #locations ⇒ Object
-
#me ⇒ Object
Top-level reads.
- #monitor_types ⇒ Object
- #notifications(**query) ⇒ Object
- #plan ⇒ Object
-
#request(method, path, query: nil, body: nil, idempotency_key: nil) ⇒ Object
Core request.
- #summary ⇒ Object
Constructor Details
#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: 30) ⇒ Client
Returns a new instance of Client.
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_rules ⇒ Object (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_groups ⇒ Object (readonly)
Returns the value of attribute contact_groups.
28 29 30 |
# File 'lib/watchfor/client.rb', line 28 def contact_groups @contact_groups end |
#contacts ⇒ Object (readonly)
Returns the value of attribute contacts.
28 29 30 |
# File 'lib/watchfor/client.rb', line 28 def contacts @contacts end |
#incidents ⇒ Object (readonly)
Returns the value of attribute incidents.
28 29 30 |
# File 'lib/watchfor/client.rb', line 28 def incidents @incidents end |
#maintenance_windows ⇒ Object (readonly)
Returns the value of attribute maintenance_windows.
28 29 30 |
# File 'lib/watchfor/client.rb', line 28 def maintenance_windows @maintenance_windows end |
#monitors ⇒ Object (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 }) |
#locations ⇒ Object
89 |
# File 'lib/watchfor/client.rb', line 89 def locations = request("GET", "/locations") |
#me ⇒ Object
Top-level reads
85 |
# File 'lib/watchfor/client.rb', line 85 def me = request("GET", "/me") |
#monitor_types ⇒ Object
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) |
#plan ⇒ Object
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 |
#summary ⇒ Object
86 |
# File 'lib/watchfor/client.rb', line 86 def summary = request("GET", "/summary") |