Sha256: c11668611bf233b35eb3ee30598c8c003ed91223e87c3536234601e7e54f0eb6

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

require 'net/http'
require 'json'
require 'zlib'
require 'openssl'

require 'honeybadger/backend/base'
require 'honeybadger/util/http'

module Honeybadger
  module Backend
    class Server < Base
      ENDPOINTS = {
        ping: '/v1/ping'.freeze,
        notices: '/v1/notices'.freeze,
        deploys: '/v1/deploys'.freeze
      }.freeze

      HTTP_ERRORS = [Timeout::Error,
                     Errno::EINVAL,
                     Errno::ECONNRESET,
                     Errno::ECONNREFUSED,
                     Errno::ENETUNREACH,
                     EOFError,
                     Net::HTTPBadResponse,
                     Net::HTTPHeaderSyntaxError,
                     Net::ProtocolError,
                     OpenSSL::SSL::SSLError,
                     SocketError].freeze

      def initialize(config)
        @http = Util::HTTP.new(config)
        super
      end

      # Internal: Post payload to endpoint for feature.
      #
      # feature - The feature which is being notified.
      # payload - The payload to send, responding to `#to_json`.
      #
      # Returns Response.
      def notify(feature, payload)
        ENDPOINTS[feature] or raise(BackendError, "Unknown feature: #{feature}")
        Response.new(@http.post(ENDPOINTS[feature], payload, payload_headers(payload)))
      rescue *HTTP_ERRORS => e
        Response.new(:error, nil, "HTTP Error: #{e.class}").tap do |response|
          error { sprintf('http error class=%s message=%s', e.class, e.message.dump) }
        end
      end

      private

      # Internal: Construct headers for supported payloads.
      #
      # payload - The payload object.
      #
      # Returns Hash headers if supported, otherwise nil.
      def payload_headers(payload)
        if payload.respond_to?(:api_key) && payload.api_key
          {
            'X-API-Key' => payload.api_key
          }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
honeybadger-2.7.2 lib/honeybadger/backend/server.rb
honeybadger-2.7.1 lib/honeybadger/backend/server.rb
honeybadger-2.7.0 lib/honeybadger/backend/server.rb