Sha256: 7dcafc67911eacd5aef6845a6d438758076b9236aafc6d61f625c55ab3d0f8d1

Contents?: true

Size: 1.29 KB

Versions: 6

Compression:

Stored size: 1.29 KB

Contents

begin
  require "rack"
rescue LoadError
end

if defined?(::Rack::Request)
  module Timber
    module Util
      # @private
      class Request < ::Rack::Request
        HTTP_PREFIX = 'HTTP_'.freeze

        def body_content
          content = body.read
          body.rewind
          content
        end

        # Returns a list of request headers. The rack env contains a lot of data, this function
        # identifies those that were the actual request headers.
        def headers
          @headers ||= ::Hash[
            *@env.select { |k,v| k.is_a?(String) && k.start_with?(HTTP_PREFIX) }
              .collect { |k,v| [k.sub(/^#{HTTP_PREFIX}/, ''), v] }
              .collect { |k,v| [k.split('_').collect(&:capitalize).join('-'), v] }
              .sort
              .flatten
          ]
        end

        def ip
          @ip ||= if @env["action_dispatch.remote_ip"]
            @env["action_dispatch.remote_ip"].to_s || super
          else
            super
          end
        end

        def referer
          # Rails 3.X returns "/" for some reason
          @referer ||= super == "/" ? nil : super
        end

        def request_id
          @request_id ||= @env["action_dispatch.request_id"] || @env["X-Request-ID"] ||
            @env["X-Request-Id"]
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
timber-2.1.0.rc3 lib/timber/util/request.rb
timber-2.1.0.rc2 lib/timber/util/request.rb
timber-2.1.0.rc1 lib/timber/util/request.rb
timber-2.0.24 lib/timber/util/request.rb
timber-2.0.23 lib/timber/util/request.rb
timber-2.0.22 lib/timber/util/request.rb