Sha256: 4263a9a592e3dd6540d3cb23040cb7fae89f844e48fcb60331d107ae05595368

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

require 'sitehub/constants'
require 'sitehub/forward_proxy'
require 'rack/request'
require 'rack/response'
require 'rack/utils'
require 'em-http'

class SiteHub
  class ForwardProxies
    NOT_FOUND = Rack::Response.new(['page not found'], 404, {})
    def call(env)
      source_request = Rack::Request.new(env)

      forward_proxy = mapped_route(path: source_request.path, request: source_request)

      return forward_proxy.call(env) if forward_proxy
      NOT_FOUND
    end

    def init
      forward_proxies.values.each(&:build)
      self
    end

    def <<(route)
      forward_proxies[route.mapped_path] = route
    end

    def mapped_route(path:, request:)
      fwd_proxy_builder = forward_proxies[mapping(path)]
      fwd_proxy_builder ? fwd_proxy_builder.resolve(id: request.cookies[RECORDED_ROUTES_COOKIE], env: request.env) : nil
    end

    def mapping(path)
      forward_proxies.keys.find do |key|
        case key
        when Regexp
          key.match(path)
        else
          path == key
        end
      end
    end

    def forward_proxies
      @forward_proxies ||= {}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sitehub-0.4.3 lib/sitehub/forward_proxies.rb