lib/faraday/rack_builder.rb in faraday-0.12.1 vs lib/faraday/rack_builder.rb in faraday-0.12.2

- old
+ new

@@ -82,10 +82,11 @@ def use(klass, *args, &block) if klass.is_a? Symbol use_symbol(Faraday::Middleware, klass, *args, &block) else raise_if_locked + warn_middleware_after_adapter if adapter_set? @handlers << self.class::Handler.new(klass, *args, &block) end end def request(key, *args, &block) @@ -103,10 +104,11 @@ ## methods to push onto the various positions in the stack: def insert(index, *args, &block) raise_if_locked index = assert_index(index) + warn_middleware_after_adapter if inserting_after_adapter?(index) handler = self.class::Handler.new(*args, &block) @handlers.insert(index, handler) end alias_method :insert_before, :insert @@ -196,9 +198,29 @@ private def raise_if_locked raise StackLocked, "can't modify middleware stack after making a request" if locked? + end + + def warn_middleware_after_adapter + warn "WARNING: Unexpected middleware set after the adapter. " \ + "This won't be supported from Faraday 1.0." + end + + def adapter_set? + @handlers.any? { |handler| is_adapter?(handler) } + end + + def inserting_after_adapter?(index) + adapter_index = @handlers.find_index { |handler| is_adapter?(handler) } + return false if adapter_index.nil? + + index > adapter_index + end + + def is_adapter?(handler) + handler.klass.ancestors.include? Faraday::Adapter end def use_symbol(mod, key, *args, &block) use(mod.lookup_middleware(key), *args, &block) end