Sha256: c8888f1e09bcb8fdb959296b162feb04c28a39495bba1a7a72c24efd0aaa9ab2

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module Coach
  class RequestSerializer
    def self.header_rules
      @header_rules ||= {}
    end

    # Sets global rules on how to sanitize headers. An optional block can be supplied
    # that will determine how to transform the original header value, otherwise a default
    # string is used.
    def self.sanitize_header(header, &rule)
      header_rules[header] = rule || ->(_value) { "[FILTERED]" }
    end

    # Applies sanitizing rules. Expects `header` to be in 'http_header_name' form.
    def self.apply_header_rule(header, value)
      return value if header_rules[header].nil?

      header_rules[header].call(value)
    end

    # Resets all header sanitizing
    def self.clear_header_rules!
      @header_rules = {}
    end

    def initialize(request)
      @request = request
    end

    def serialize
      {
        # Identification
        request_id: @request.uuid,

        # Request details
        method: @request.method,
        path: request_path,
        format: @request.format.try(:ref),
        params: @request.filtered_parameters, # uses config.filter_parameters

        # Extra request info
        headers: filtered_headers,
        session_id: @request.remote_ip, # TODO: remove in a future release
        session_ip: @request.remote_ip,
      }
    end

    private

    def request_path
      @request.fullpath
    rescue StandardError
      "unknown"
    end

    def filtered_headers
      header_value_pairs = @request.filtered_env.map do |key, value|
        next unless key =~ /^HTTP_/

        [key.downcase, self.class.apply_header_rule(key.downcase, value)]
      end.compact

      Hash[header_value_pairs]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
coach-2.0.0 lib/coach/request_serializer.rb