Class: SiteHub::ForwardProxyBuilder

Inherits:
Object
  • Object
show all
Extended by:
GetterSetterMethods
Includes:
Equality, Middleware, Resolver, Rules
Defined in:
lib/sitehub/forward_proxy_builder.rb

Defined Under Namespace

Classes: InvalidDefinitionException

Constant Summary collapse

INVALID_SPLIT_MSG =
'url must be defined if not supplying a block'.freeze
ROUTES_WITH_SPLITS_MSG =
'you cant register routes and splits at the same level'.freeze
INVALID_ROUTE_DEF_MSG =
'rule must be specified when supplying a block'.freeze
IGNORING_URL_LABEL_MSG =
'Block supplied, ignoring URL and Label parameters'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GetterSetterMethods

getter_setter, getter_setters

Methods included from Rules

#applies?, #rule

Methods included from Equality

#==, #_clazz, included

Methods included from Middleware

#apply_middleware, #middleware?, #middlewares, #use

Constructor Details

#initialize(url: nil, mapped_path:, rule: nil, sitehub_cookie_name: nil, &block) ⇒ ForwardProxyBuilder

Returns a new instance of ForwardProxyBuilder.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sitehub/forward_proxy_builder.rb', line 30

def initialize(url: nil, mapped_path:, rule: nil, sitehub_cookie_name: nil, &block)
  @id = UUID.generate(:compact)
  @mapped_path = mapped_path
  @splits = Collection::SplitRouteCollection.new
  @routes = Collection::RouteCollection.new
  @sitehub_cookie_name = sitehub_cookie_name
  rule(rule) if rule
  default(url: url) if url

  return unless block_given?

  instance_eval(&block)
  raise InvalidDefinitionException unless valid?
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



28
29
30
# File 'lib/sitehub/forward_proxy_builder.rb', line 28

def id
  @id
end

#mapped_pathObject (readonly)

Returns the value of attribute mapped_path.



28
29
30
# File 'lib/sitehub/forward_proxy_builder.rb', line 28

def mapped_path
  @mapped_path
end

Returns the value of attribute sitehub_cookie_name.



28
29
30
# File 'lib/sitehub/forward_proxy_builder.rb', line 28

def sitehub_cookie_name
  @sitehub_cookie_name
end

Instance Method Details

#buildObject



86
87
88
89
90
91
92
93
# File 'lib/sitehub/forward_proxy_builder.rb', line 86

def build
  if middleware?
    build_with_middleware
    build_default_with_middleware if default_proxy
  end

  self
end

#default(url:) ⇒ Object



82
83
84
# File 'lib/sitehub/forward_proxy_builder.rb', line 82

def default(url:)
  default_proxy(forward_proxy(label: :default, url: url))
end

#endpoints(collection = nil) ⇒ Object



50
51
52
53
54
55
# File 'lib/sitehub/forward_proxy_builder.rb', line 50

def endpoints(collection = nil)
  return @endpoints || Collection::RouteCollection.new unless collection

  raise InvalidDefinitionException, ROUTES_WITH_SPLITS_MSG if @endpoints && @endpoints != collection
  @endpoints = collection
end

#forward_proxy(label:, url:, rule: nil) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/sitehub/forward_proxy_builder.rb', line 100

def forward_proxy(label:, url:, rule: nil)
  label ||= UUID.generate(:compact)
  ForwardProxy.new(sitehub_cookie_path: sitehub_cookie_path,
                   sitehub_cookie_name: sitehub_cookie_name,
                   id: label.to_sym,
                   rule: rule,
                   mapped_url: url,
                   mapped_path: mapped_path)
end

#resolve(id: nil, env:) ⇒ Object



95
96
97
98
# File 'lib/sitehub/forward_proxy_builder.rb', line 95

def resolve(id: nil, env:)
  id = id.to_s.to_sym
  endpoints[id] || endpoints.resolve(env: env) || default_proxy
end

#route(url: nil, label: nil, rule: nil, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sitehub/forward_proxy_builder.rb', line 70

def route(url: nil, label: nil, rule: nil, &block)
  endpoint = if block
               raise InvalidDefinitionException, INVALID_ROUTE_DEF_MSG unless rule
               warn(IGNORING_URL_LABEL_MSG) if url || label
               new(rule: rule, &block).build
             else
               forward_proxy(url: url, label: label, rule: rule)
             end

  routes.add(endpoint.id, endpoint)
end

#split(percentage:, url: nil, label: nil, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sitehub/forward_proxy_builder.rb', line 57

def split(percentage:, url: nil, label: nil, &block)
  raise InvalidDefinitionException, INVALID_SPLIT_MSG unless block || url

  proxy = if block
            warn(IGNORING_URL_LABEL_MSG) if url || label
            new(&block).build
          else
            forward_proxy(label: label, url: url)
          end

  splits.add proxy.id, proxy, percentage
end

#valid?Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/sitehub/forward_proxy_builder.rb', line 45

def valid?
  return true if default_proxy
  endpoints.valid?
end