Sha256: bd3bdccbfba8aea193fd9f0644d6f8a6ae7d185d55ddeacee31252861d568730

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require 'faraday'
require 'ok_computer/built_in_checks/http_check'

module OkComputer
  class AlgoliaCheck < HttpCheck
    StatusFailed = Class.new(StandardError)

    STATUS_URL = 'https://status.algolia.com/1/status'

    attr_accessor :app_id, :api_key

    def initialize(url: STATUS_URL, app_id: nil, api_key: nil, request_timeout: 5)
      super(url, request_timeout)

      self.app_id = app_id.presence
      self.api_key = api_key.presence
    end

    # Public: Return the status of the Monitoring check
    def check
      status, body = perform_request
      raise(StatusFailed, body) unless status == 200 && body['status'].values.all? { |v| v == 'operational' }

      mark_message('Monitoring check successful')
    rescue StandardError => e
      mark_message("Error: '#{e}'")
      mark_failure
    end

    def perform_request
      response = Faraday.get(url, request: { timeout: request_timeout }) do |req|
        req.headers['Content-Type'] = 'application/json'
        req.headers['X-Algolia-API-Key'] = api_key
        req.headers['X-Algolia-Application-Id'] = app_id
      end

      [response.status, MultiJson.decode(response.body)]
    rescue StandardError => e
      raise(StatusFailed, e)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
okcomputer-checks-1.1.1 lib/ok_computer/checks/algolia_check.rb
okcomputer-checks-1.1.0 lib/ok_computer/checks/algolia_check.rb
okcomputer-checks-1.0.0 lib/ok_computer/checks/algolia_check.rb