Sha256: 6a14e9e3fc6b7aa632c9ad09cedc211bc22d0eced7764b6bf740770fe310c058
Contents?: true
Size: 1.54 KB
Versions: 15
Compression:
Stored size: 1.54 KB
Contents
require 'faraday' module FaradayMiddleware # Public: Writes the original HTTP method to "X-Http-Method-Override" header # and sends the request as POST. # # This can be used to work around technical issues with making non-POST # requests, e.g. faulty HTTP client or server router. # # This header is recognized in Rack apps by default, courtesy of the # Rack::MethodOverride module. See # http://rack.rubyforge.org/doc/classes/Rack/MethodOverride.html class MethodOverride < Faraday::Middleware HEADER = "X-Http-Method-Override".freeze # Public: Initialize the middleware. # # app - the Faraday app to wrap # options - (optional) # :rewrite - Array of HTTP methods to rewrite # (default: all but GET and POST) def initialize(app, options = nil) super(app) @methods = options && options.fetch(:rewrite).map { |method| method = method.downcase if method.respond_to? :downcase method.to_sym } end def call(env) method = env[:method] rewrite_request(env, method) if rewrite_request?(method) @app.call(env) end def rewrite_request?(method) if @methods.nil? or @methods.empty? method != :get and method != :post else @methods.include? method end end # Internal: Write the original HTTP method to header, change method to POST. def rewrite_request(env, original_method) env[:request_headers][HEADER] = original_method.to_s.upcase env[:method] = :post end end end
Version data entries
15 entries across 15 versions & 4 rubygems