Class: Crossroads::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/crossroads/route.rb

Overview

expression “headers.foo = 1 and body.bar = 1”

processor do |headers, body|

target "/queue/foo" if body....

end

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Route

Returns a new instance of Route.



8
9
10
11
12
13
14
# File 'lib/crossroads/route.rb', line 8

def initialize(name)
  @name = name
  @jgrep = JGrep::JGrep.new
  @expression = nil
  @processor = nil
  @route_count = 0
end

Instance Method Details

#expression(exp) ⇒ Object



43
44
45
46
# File 'lib/crossroads/route.rb', line 43

def expression(exp)
  @expression = exp
  @jgrep.expression = exp
end

#load_route(file) ⇒ Object



16
17
18
19
# File 'lib/crossroads/route.rb', line 16

def load_route(file)
  Log.info("Loading route from file #{file} for router #{@name}")
  eval(File.read(file))
end

#processor(&blk) ⇒ Object



39
40
41
# File 'lib/crossroads/route.rb', line 39

def processor(&blk)
  @processor = blk
end

#route!(msg) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/crossroads/route.rb', line 21

def route!(msg)
  @targets = []

  @start = Time.now

  if @expression && @processor && @jgrep.match_value({"headers" => msg.headers, "body" => JSON.parse(msg.body)})
    @processor.call(msg.headers, msg.body)
  end

  @route_count += 1

  @targets
end

#target(trgt) ⇒ Object



35
36
37
# File 'lib/crossroads/route.rb', line 35

def target(trgt)
  @targets << {:target => trgt, :name => @name, :process_time => (Time.now - @start).to_f}
end