Module: Axon

Defined in:
lib/axon-nats.rb,
lib/axon-nats/msg.rb,
lib/axon-nats/error.rb,
lib/axon-nats/version.rb

Defined Under Namespace

Classes: AxonError, AxonMsg

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.method_missing(method, *args, &block) ⇒ Object



40
41
42
# File 'lib/axon-nats.rb', line 40

def self.method_missing(method, *args)
	NATS.send(method, *args) if NATS.respond_to? method
end

.publish(route, msg = {}) ⇒ Object



37
38
39
# File 'lib/axon-nats.rb', line 37

def self.publish(route, msg = {})
	NATS.publish(route, AxonMsg.new(msg))
end

.request(route, req = {}, &block) ⇒ Object



32
33
34
35
36
# File 'lib/axon-nats.rb', line 32

def self.request(route, req = {}, &block)
	NATS.request(route, AxonMsg.new(req)) { |msg_s|
		block.call(AxonMsg.parse(msg_s))
	}
end

.respond(route, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/axon-nats.rb', line 17

def self.respond(route, &block)
	NATS.subscribe(route) { |msg_s, reply| 
		begin
			NATS.publish(reply, AxonMsg.new(block.call(AxonMsg.parse(msg_s))))
		rescue AxonError => e
			NATS.publish(reply, AxonMsg.new(e.data, e.message))
		rescue => e
			if e.is_a? StandardError
				STDERR.puts e
				STDERR.puts e.backtrace
			end
			NATS.publish(reply, AxonMsg.new({}, e.message))
		end
	}
end

.subscribe(route, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/axon-nats.rb', line 7

def self.subscribe(route, &block)
	NATS.subscribe(route) { |msg_s|
		begin
			block.call(AxonMsg.parse(msg_s))
		rescue => e
			STDERR.puts e
			STDERR.puts e.backtrace
		end
	}
end