Class: Serf::Util::RouteSet

Inherits:
Object
  • Object
show all
Defined in:
lib/serf/util/route_set.rb

Overview

RouteSets hold routing information for ENV hashes to matched endpoints.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RouteSet

Returns a new instance of RouteSet.



12
13
14
15
16
17
18
19
20
21
# File 'lib/serf/util/route_set.rb', line 12

def initialize(options={})
  @routes = {}
  @matchers = []
  @regexp_matcher_factory = options.fetch(:regexp_matcher_factory) {
    ::Serf::Util::RegexpMatcher
  }
  @route_endpoint_factory = options.fetch(:route_endpoint_factory) {
    ::Serf::Util::RouteEndpoint
  }
end

Class Method Details

.build(options = {}) ⇒ Object

Default factory method.



76
77
78
# File 'lib/serf/util/route_set.rb', line 76

def self.build(options={})
  self.new options
end

Instance Method Details

#add_route(options = {}) ⇒ Object

Connects a matcher (String or an Object implementing ===) to an endpoint.

Parameters:

  • opts (Hash)

    a customizable set of options



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/serf/util/route_set.rb', line 33

def add_route(options={})
  # We create our endpoint representation.
  endpoint = @route_endpoint_factory.build(
    handler: options.fetch(:handler),
    action: options.fetch(:action),
    message_parser: options[:message_parser])

  # Maybe we have an non-String matcher. Handle the Regexp case.
  # We only keep track of matchers if it isn't a string because
  # string matchers are just pulled out of routes by key lookup.
  matcher = options.fetch :matcher
  matcher = @regexp_matcher_factory.build matcher if matcher.kind_of? Regexp
  @matchers << matcher unless matcher.is_a? String

  # We add the route (matcher+endpoint) into our routes
  @routes[matcher] ||= []
  @routes[matcher] << endpoint
end

#match_routes(env = {}) ⇒ Array

Returns List of endpoints that matched.

Parameters:

  • env (Hash) (defaults to: {})

    The input message environment to match for routes.

Returns:

  • (Array)

    List of endpoints that matched.



56
57
58
59
60
61
62
63
64
# File 'lib/serf/util/route_set.rb', line 56

def match_routes(env={})
  kind = env[:kind]
  routes = []
  routes.concat Array(@routes[kind])
  @matchers.each do |matcher|
    routes.concat Array(@routes[matcher]) if matcher === env
  end
  return routes
end

#sizeInteger

Returns Number of routes this RouteSet tracks.

Returns:

  • (Integer)

    Number of routes this RouteSet tracks.



69
70
71
# File 'lib/serf/util/route_set.rb', line 69

def size
  return @routes.size
end