lib/boppers/uptime.rb in boppers-uptime-0.1.2 vs lib/boppers/uptime.rb in boppers-uptime-0.1.3

- old
+ new

@@ -7,34 +7,49 @@ module Boppers class Uptime FORMAT = "%Y-%m-%dT%H:%M:%S%:z" - attr_reader :name, :url, :interval, :status, :contain, :min_failures, - :timezone, :format, :failures, :failed_at + attr_reader :name, + :url, + :interval, + :status, + :contain, + :min_failures, + :timezone, + :format, + :failures, + :failed_at - def initialize(name:, url:, interval: 30, status: 200, contain: nil, - min_failures: 1, timezone: Time.now.getlocal.zone, - format: FORMAT) + def initialize( + name:, + url:, + interval: 30, + status: 200, + contain: nil, + min_failures: 2, + timezone: Time.now.getlocal.zone, + format: FORMAT + ) @name = name @url = url @interval = interval @status = [status].flatten.map(&:to_i) @contain = contain @min_failures = min_failures - @timezone = find_timezone(timezone) + @timezone = find_timezone(timezone) || find_timezone("Etc/UTC") @format = format @failures = [] end def call succeed = begin - response = HttpClient.get(url, {}, {}, timeout: 1) - valid_response?(response) - rescue SocketError, Timeout::Error, Net::OpenTimeout - false - end + response = HttpClient.get(url, {}, {}, timeout: 1) + valid_response?(response) + rescue SocketError, Timeout::Error, Net::OpenTimeout # rubocop:disable Lint/ShadowedException + false + end return succeed! if succeed # Check failed, so track the failure and check # if we need to send any notification (only sends a notification @@ -54,29 +69,30 @@ def valid_response?(response) validations = [] validations << status.include?(response.code) - validations << response.body.include?(contain) if contain.kind_of?(String) - validations << response.body.match?(contain) if contain.kind_of?(Regexp) + validations << response.body.include?(contain) if contain.is_a?(String) + validations << response.body.match?(contain) if contain.is_a?(Regexp) validations.all? end def back_online!(offline_at, online_at) duration = RelativeTime.call(offline_at, online_at) title = "#{name} is up" - message = [ - "#{name} is back online at #{format_time(online_at)}, after #{duration} of downtime.", - "You can check it at #{url}." - ].join("\n") + message = "#{name} is back online at #{format_time(online_at)}, " \ + "after #{duration} of downtime.\n" \ + "You can check it at #{url}." - Boppers.notify(:uptime, - title: title, - message: message, - options: {color: :green}) + Boppers.notify( + :uptime, + title: title, + message: message, + options: {color: :green} + ) end def went_offline! failed_at = failures.first title = "#{name} is down" @@ -102,13 +118,11 @@ end def find_timezone(name) TZInfo::Timezone.get(name) rescue TZInfo::InvalidTimezoneIdentifier - name = name.to_sym - TZInfo::Timezone.all.find do |zone| - zone.current_period.abbreviation == name + zone.current_period.abbreviation.to_s == name end end end end