Class: SiteHub::Middleware::ForwardProxies

Inherits:
Hash
  • Object
show all
Defined in:
lib/sitehub/middleware/forward_proxies.rb

Constant Summary collapse

NIL_PROXY =
NilProxy.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sitehub_cookie_name) ⇒ ForwardProxies

Returns a new instance of ForwardProxies.



16
17
18
19
# File 'lib/sitehub/middleware/forward_proxies.rb', line 16

def initialize(sitehub_cookie_name)
  @sitehub_cookie_name = sitehub_cookie_name
  self.default = NIL_PROXY
end

Instance Attribute Details

Returns the value of attribute sitehub_cookie_name.



14
15
16
# File 'lib/sitehub/middleware/forward_proxies.rb', line 14

def sitehub_cookie_name
  @sitehub_cookie_name
end

Instance Method Details

#add_proxy(url: nil, mapped_path:, &block) ⇒ Object



34
35
36
37
38
39
# File 'lib/sitehub/middleware/forward_proxies.rb', line 34

def add_proxy(url: nil, mapped_path:, &block)
  self[mapped_path] = ForwardProxyBuilder.new(sitehub_cookie_name: sitehub_cookie_name,
                                              url: url,
                                              mapped_path: mapped_path,
                                              &block)
end

#call(env) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/sitehub/middleware/forward_proxies.rb', line 21

def call(env)
  source_request = Rack::Request.new(env)

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

  forward_proxy.call(env)
end

#initObject



29
30
31
32
# File 'lib/sitehub/middleware/forward_proxies.rb', line 29

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

#mapped_proxy(path:, request:) ⇒ Object



41
42
43
# File 'lib/sitehub/middleware/forward_proxies.rb', line 41

def mapped_proxy(path:, request:)
  self[mapping(path)].resolve(id: request.cookies[sitehub_cookie_name], env: request.env)
end

#mapping(path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/sitehub/middleware/forward_proxies.rb', line 45

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