Sha256: 9b1f4f0d78106ecc753d2c57749f8b0cb115a0af58b50ddaa0feaeec4fef4e93

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

module Rack
  class ReverseProxyMatcher
    def initialize(matcher,url=nil,options)
      @default_url=url
      @url=url
      @options=options

      if matcher.kind_of?(String)
        @matcher = /^#{matcher.to_s}/
      elsif matcher.respond_to?(:match)
        @matcher = matcher
      else
        raise "Invalid Matcher for reverse_proxy"
      end
    end

    attr_reader :matcher,:url, :default_url,:options

    def match?(path, *args)
      match_path(path, *args) ? true : false
    end

    def get_uri(path,env)
      return nil if url.nil?
      _url=(url.respond_to?(:call) ? url.call(env) : url.clone)
      if _url =~/\$\d/
        match_path(path).to_a.each_with_index { |m, i| _url.gsub!("$#{i.to_s}", m) }
        URI(_url)
      else
        default_url.nil? ? URI.parse(_url) : URI.join(_url, path)
      end
    end

    def to_s
      %Q("#{matcher.to_s}" => "#{url}")
    end

    private
    def match_path(path, *args)
      headers = args[0]
      rackreq = args[1]
      arity = matcher.method(:match).arity
      if arity == -1
        match = matcher.match(path)
      else
        params = [path, (@options[:accept_headers] ? headers : nil), rackreq]
        match = matcher.match(*params[0..(arity - 1)])
      end
      @url = match.url(path) if match && default_url.nil?
      match
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
pact-provider-proxy-2.3.0 vendor/rack-reverse-proxy/lib/rack/reverse_proxy_matcher.rb
pact-provider-proxy-2.2.0 vendor/rack-reverse-proxy/lib/rack/reverse_proxy_matcher.rb
rack-reverse-proxy-0.8.1 lib/rack/reverse_proxy_matcher.rb
pact-provider-proxy-2.1.0 vendor/rack-reverse-proxy/lib/rack/reverse_proxy_matcher.rb
pact-provider-proxy-2.0.0 vendor/rack-reverse-proxy/lib/rack/reverse_proxy_matcher.rb
pact-provider-proxy-1.2.0 vendor/rack-reverse-proxy/lib/rack/reverse_proxy_matcher.rb