Sha256: e7f0d9ee863c4981ef39a1b5b4c501f23c05070a7f8f01792c0b4cf8b1cfef8b

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

require 'cronitor/version'
require 'cronitor/error'
require 'net/http'
require 'unirest'

Unirest.default_header 'Accept', 'application/json'
Unirest.default_header 'Content-Type', 'application/json'

class Cronitor
  attr_accessor :token, :opts, :code
  API_URL = 'https://cronitor.io/v1'
  PING_URL = 'https://cronitor.link'

  def initialize(token: nil, opts: {}, code: nil)
    @token = token
    @opts = opts
    @code = code

    exists? opts[:name] if opts && opts.key?(:name)

    if token.nil? && @code.nil?
      fail(
        Cronitor::Error,
        'Either a Cronitor API token or an existing monitor code must be ' \
        'provided')
    end

    create if @code.nil?
  end

  def create
    response = Unirest.post(
      "#{API_URL}/monitors",
      auth: { user: token },
      parameters: opts.to_json
    )

    @code = response.body['code'] if valid? response
  end

  def exists?(name)
    response = Unirest.get(
      "#{API_URL}/monitors/#{URI.escape name}",
      auth: { user: token }
    )
    return false unless response.code == 200

    @code = response.body['code']

    true
  end

  def ping(type, msg = nil)
    url = "#{PING_URL}/#{code}/#{type}"
    url += "?msg=#{URI.escape msg}" if type == 'fail' && !msg.nil?

    response = Unirest.get url
    valid? response
  end

  private

  def valid?(response)
    return true if [200, 201].include? response.code
    server_error? response

    fail Cronitor::Error, error_msg(response.body)
  end

  def error_msg(body, msg = [])
    body.each do |opt, value|
      if value.respond_to? 'each'
        value.each do |error_msg|
          msg << "#{opt}: #{error_msg}"
        end
      else
        msg << "#{opt}: #{value}"
      end
    end

    msg.join ' '
  end

  def server_error?(response)
    return unless [301, 302, 404, 500, 502, 503, 504].include? response.code

    fail(
      Cronitor::Error,
      "Something else has gone awry. HTTP status: #{response.code}"
    )
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cronitor-0.4.0 lib/cronitor.rb