Sha256: d9f9a16902326fe0edc7a9492a820c80921afcec6ee6f034b01c558a582e73ef

Contents?: true

Size: 995 Bytes

Versions: 1

Compression:

Stored size: 995 Bytes

Contents

module Fuprint
  class Request

    def initialize(app)
      @app = app
    end

    # Receive the request and print info
    def call(env)

      # Only active in development mode
      if Fuprint.mode == 'development'
        r = ::Rack::Request.new(env)

        puts env.inspect if Fuprint.debug

        # Delete the splat and captures if Fuprint.splat = false (default)
        r.params.delete_if{|k, v| %w[splat captures].include?(k)} unless Fuprint.splat

        # Strip all params if Fuprint.strip = true (default)
        r.params.each{|k, v| r.params[k] = (v.is_a?(String) ? v.strip : v)} if Fuprint.strip

        begin
          puts "\n@ #{o(r.request_method.upcase)} #{o(r.fullpath)}"
          puts "$ #{o(r.params)}"
        rescue => e
          puts "! #{e}"
        end
      end

      @app.call(env)
    end

    private

    # Colorize output, 33 is :green (default), 31 is :red
    def o(s, c = :green)
      %{\e[#{c == :green ? 33 : 31}m#{s}\e[0m}
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fuprint-0.1.2 lib/fuprint/request.rb