Sha256: e0e237da5a3ffd9e43d312d6a0a625738c3c4034354976b278f51508db71c598

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module ElasticAPM
  module Transport
    # @api private
    class Headers
      HEADERS = {
        'Content-Type' => 'application/x-ndjson',
        'Transfer-Encoding' => 'chunked'
      }.freeze
      GZIP_HEADERS = HEADERS.merge(
        'Content-Encoding' => 'gzip'
      ).freeze

      def initialize(config, initial: {})
        @config = config
        @hash = build!(initial)
      end

      attr_accessor :hash

      def [](key)
        @hash[key]
      end

      def []=(key, value)
        @hash[key] = value
      end

      def merge(other)
        self.class.new(@config, initial: @hash.merge(other))
      end

      def merge!(other)
        @hash.merge!(other)
        self
      end

      def to_h
        @hash
      end

      def chunked
        merge(
          @config.http_compression? ? GZIP_HEADERS : HEADERS
        )
      end

      private

      def build!(headers)
        headers[:'User-Agent'] = UserAgent.new(@config).to_s

        if (token = @config.secret_token)
          headers[:Authorization] = "Bearer #{token}"
        end

        headers
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
elastic-apm-3.3.0 lib/elastic_apm/transport/headers.rb
elastic-apm-3.2.0 lib/elastic_apm/transport/headers.rb