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



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

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

#default_publish_optsObject



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

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

#defer(&blk) ⇒ Object



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

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
# File 'lib/droid/request.rb', line 72

def publish(name, data, opts={}, popts={}, &block)
  opts = default_publish_opts.merge(opts)
  if block
    raise StandardError, "async publish/reply no longer directly supported... for now."
  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.publish_to_q(reply_to, data, opts, popts)
end

#requeue(ropts = {}) ⇒ Object



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

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