Sha256: 9f9cdd40c54573475fa9c8ddbdb8601a16680b8f370732014f0903cc62db6b7d

Contents?: true

Size: 1.01 KB

Versions: 5

Compression:

Stored size: 1.01 KB

Contents

require 'ur' unless Object.const_defined?(:Ur)

class Ur
  module Middleware
    def initialize(app, options = {})
      @app = app
      @options = options
    end
    attr_reader :app
    attr_reader :options

    def invoke_callback(name, *a, &b)
      if @options[name]
        @options[name].call(*a, &b)
      end
    end
  end

  class FaradayMiddleware < ::Faraday::Middleware
    include Ur::Middleware
    def call(request_env)
      ur = Ur.from_faraday_request(request_env)
      invoke_callback(:before_request, ur)
      ur.logger = options[:logger] if options[:logger]
      ur.faraday_on_complete(@app, request_env) do |response_env|
        invoke_callback(:after_response, ur)
      end
    end
  end

  class RackMiddleware
    include Ur::Middleware
    def call(env)
      ur = Ur.from_rack_request(env)
      invoke_callback(:before_request, ur)
      ur.logger = options[:logger] if options[:logger]
      ur.with_rack_response(@app, env) do
        invoke_callback(:after_response, ur)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ur-0.1.0 lib/ur/middleware.rb
ur-0.0.4 lib/ur/middleware.rb
ur-0.0.3 lib/ur/middleware.rb
ur-0.0.2 lib/ur/middleware.rb
ur-0.0.1 lib/ur/middleware.rb