Sha256: 4153c344a683e7b3cc0b3308097245b6cc2de981cc51050d8786dc8153ae9068

Contents?: true

Size: 1.55 KB

Versions: 9

Compression:

Stored size: 1.55 KB

Contents

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
      }
    end

    private

    def request_path
      @request.fullpath
    rescue
      "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

9 entries across 9 versions & 1 rubygems

Version Path
coach-0.4.0 lib/coach/request_serializer.rb
coach-0.3.0 lib/coach/request_serializer.rb
coach-0.2.3 lib/coach/request_serializer.rb
coach-0.2.2 lib/coach/request_serializer.rb
coach-0.2.1 lib/coach/request_serializer.rb
coach-0.2.0 lib/coach/request_serializer.rb
coach-0.0.2 lib/coach/request_serializer.rb
coach-0.0.1 lib/coach/request_serializer.rb
coach-0.0.0 lib/coach/request_serializer.rb