Sha256: 97b0347050042676a46e57ac3b2a892fc7af01b3f5186cf9389e7d83bdbfef34

Contents?: true

Size: 942 Bytes

Versions: 5

Compression:

Stored size: 942 Bytes

Contents

module Xing::Services
  class LogJson
    APPJSON_RE = %r{^application/json}.freeze

    def log_string(string)
      obj = JSON.parse(string)
      pretty_str = JSON.pretty_unparse(obj)
      Rails.logger.debug(type_string + ": " + pretty_str)
    end
  end

  class LogJsonRequests < LogJson
    def initialize(app)
      @app = app
    end

    def call(env)
      if defined?(Rails) and env["CONTENT_TYPE"] =~ APPJSON_RE
        log_string(env["rack.input"].read)
        env["rack.input"].rewind
      end
      @app.call(env)
    end

    def type_string
      "Request"
    end
  end

  class LogJsonResponses < LogJson
    def initialize(app)
      @app = app
    end

    def call(env)
      @app.call(env).tap do |status, headers, response|
        if defined?(Rails) and headers["Content-Type"] =~ APPJSON_RE
          log_string(response.body)
        end
      end
    end

    def type_string
      "Response"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
xing-backend-1.0.0.pre.beta lib/xing/services/log_json_responses.rb
xing-backend-0.0.25 lib/xing/services/log_json_responses.rb
xing-backend-0.0.23 lib/xing/services/log_json_responses.rb
xing-backend-0.0.22 lib/xing/services/log_json_responses.rb
xing-backend-0.0.21 lib/xing/services/log_json_responses.rb