Sha256: 4b5e354f72c5ad88b5140a1be3e895361b00ade87d1545675ec106f8eb7c3300

Contents?: true

Size: 1.43 KB

Versions: 13

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require_relative "../base"

module Neetob
  class CLI
    module Cronitor
      class Base < CLI::Base
        MONITORS_URL = "https://cronitor.io/api/monitors"

        def initialize
          super()
        end

        def process(*args)
          raise(StandardError, "CRONITOR_ONE_API_KEY is not given") if ENV["CRONITOR_ONE_API_KEY"].nil?
          raise(StandardError, "CRONITOR_THREE_API_KEY is not given") if ENV["CRONITOR_THREE_API_KEY"].nil?

          run(*args)
        end

        private

          def parse_response(http_result)
            case http_result
            when Net::HTTPSuccess
              JSON.parse(http_result.body, symbolize_names: true)
            else
              error_message = JSON.parse(http_result.body)["errors"][0]["message"]
              raise(StandardError, "Request failed with status code #{http_result.code}: #{error_message}")
            end
          end

          def get(username:, password:, headers: { "Accept" => "application/json" })
            uri = URI(MONITORS_URL)
            request = Net::HTTP::Get.new(uri)
            headers.each { |key, value| request[key] = value }
            request.basic_auth(username, password)

            response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
              http.request(request)
            end

            parse_response(response)
          end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
neetob-0.5.29 lib/neetob/cli/cronitor/base.rb
neetob-0.5.28 lib/neetob/cli/cronitor/base.rb
neetob-0.5.27 lib/neetob/cli/cronitor/base.rb
neetob-0.5.26 lib/neetob/cli/cronitor/base.rb
neetob-0.5.25 lib/neetob/cli/cronitor/base.rb
neetob-0.5.24 lib/neetob/cli/cronitor/base.rb
neetob-0.5.23 lib/neetob/cli/cronitor/base.rb
neetob-0.5.22 lib/neetob/cli/cronitor/base.rb
neetob-0.5.21 lib/neetob/cli/cronitor/base.rb
neetob-0.5.20 lib/neetob/cli/cronitor/base.rb
neetob-0.5.19 lib/neetob/cli/cronitor/base.rb
neetob-0.5.18 lib/neetob/cli/cronitor/base.rb
neetob-0.5.17 lib/neetob/cli/cronitor/base.rb