Class: Droid::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/droid/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(qobj, header, raw_message) ⇒ Request

Returns a new instance of Request.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/droid/request.rb', line 10

def initialize(qobj, header, raw_message)
	@qobj = qobj
	@header = header
	@raw_message = raw_message

	@droid_headers = Droid::Utils.parse_custom_headers(header.headers)
	@msg = Droid::Utils.parse_message(raw_message)

	@start = Time.now.getgm.to_i

	@acked = false
end

Instance Attribute Details

#droid_headersObject (readonly)

Returns the value of attribute droid_headers.



8
9
10
# File 'lib/droid/request.rb', line 8

def droid_headers
  @droid_headers
end

#headerObject (readonly)

Returns the value of attribute header.



7
8
9
# File 'lib/droid/request.rb', line 7

def header
  @header
end

#msgObject (readonly) Also known as: params

Returns the value of attribute msg.



8
9
10
# File 'lib/droid/request.rb', line 8

def msg
  @msg
end

#qobjObject (readonly)

Returns the value of attribute qobj.



7
8
9
# File 'lib/droid/request.rb', line 7

def qobj
  @qobj
end

#raw_messageObject (readonly)

Returns the value of attribute raw_message.



7
8
9
# File 'lib/droid/request.rb', line 7

def raw_message
  @raw_message
end

#startObject (readonly)

Returns the value of attribute start.



8
9
10
# File 'lib/droid/request.rb', line 8

def start
  @start
end

Instance Method Details

#[](field) ⇒ Object



35
36
37
# File 'lib/droid/request.rb', line 35

def [](field)
	msg[field.to_s]
end

#ackObject



59
60
61
62
63
# File 'lib/droid/request.rb', line 59

def ack
	return if acked?
	header.ack
	@acked = true
end

#acked?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/droid/request.rb', line 55

def acked?
	@acked == true
end

#ageObject



41
42
43
44
# File 'lib/droid/request.rb', line 41

def age
	return -1 unless droid_headers[:published_on]
	start - droid_headers[:published_on]
end

#data_summaryObject



106
107
108
# File 'lib/droid/request.rb', line 106

def data_summary
	Droid::Utils.format_data_summary(msg, droid_headers)
end

#default_publish_optsObject



100
101
102
103
104
# File 'lib/droid/request.rb', line 100

def default_publish_opts
	{
		:event_hash => droid_headers[:event_hash]
	}
end

#defer(&blk) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/droid/request.rb', line 90

def defer(&blk)
	EM.defer(lambda do
		begin
			blk.call
		rescue => e
			Droid.handle_error(e)
		end
	end)
end

#exObject



27
28
29
# File 'lib/droid/request.rb', line 27

def ex
	qobj.ex
end

#expired?Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/droid/request.rb', line 50

def expired?
	return false if ttl == -1
	age > ttl
end

#mqObject



31
32
33
# File 'lib/droid/request.rb', line 31

def mq
	qobj.mq
end

#publish(name, data, opts = {}, popts = {}, &block) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/droid/request.rb', line 72

def publish(name, data, opts={}, popts={}, &block)
	opts = default_publish_opts.merge(opts)
	if block
		opts[:reply_to] ||= Droid::Utils.generate_reply_to(name)
		Droid::ReplyQueue.new(opts[:reply_to]).subscribe(&block)
	end
	Droid.publish(name, data, opts, popts)
end

#qObject



23
24
25
# File 'lib/droid/request.rb', line 23

def q
	qobj.q
end

#reply(data, opts = {}, popts = {}) ⇒ Object

Raises:



65
66
67
68
69
70
# File 'lib/droid/request.rb', line 65

def reply(data, opts={}, popts={})
	opts.merge!(default_publish_opts)
	reply_to = droid_headers[:reply_to] || self.msg['reply_to']
	raise UnknownReplyTo unless reply_to
	Droid.reply_to_q(reply_to, data, opts, popts)
end

#requeue(ropts = {}) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/droid/request.rb', line 81

def requeue(ropts={})
	h = droid_headers.dup
	h[:requeued] = true
	h.delete(:ttl)
	popts = { :headers => h }
	ropts[:ttl] ||= 10
	Droid.publish_to_q(q.name, msg, ropts, popts)
end

#ttlObject



46
47
48
# File 'lib/droid/request.rb', line 46

def ttl
	droid_headers[:ttl] || -1
end