Class: ActionSubscriber::Middleware::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/action_subscriber/middleware/env.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subscriber, encoded_payload, properties) ⇒ Env

Returns a new instance of Env.

Parameters:

  • subscriber (Class)

    the class that will handle this message

  • encoded_payload (String)

    the payload as it was received from RabbitMQ

  • properties (Hash)

    that must contain the following keys (as symbols) :channel => RabbitMQ channel for doing acknowledgement :content_type => String :delivery_tag => String (the message identifier to send back to rabbitmq for acknowledgement) :exchange => String :headers => Hash[ String => String ] :message_id => String :routing_key => String



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/action_subscriber/middleware/env.rb', line 29

def initialize(subscriber, encoded_payload, properties)
  @action = properties.fetch(:action)
  @channel = properties[:channel]
  @content_type = properties.fetch(:content_type)
  @delivery_tag = properties.fetch(:delivery_tag)
  @encoded_payload = encoded_payload
  @exchange = properties.fetch(:exchange)
  @has_been_acked = false
  @has_been_nacked = false
  @has_been_rejected = false
  @headers = properties.fetch(:headers, {})
  @message_id = properties.fetch(:message_id, ::SecureRandom.hex(3))
  @queue = properties.fetch(:queue)
  @routing_key = properties.fetch(:routing_key)
  @subscriber = subscriber
  @uses_acknowledgements = properties.fetch(:uses_acknowledgements, false)
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



8
9
10
# File 'lib/action_subscriber/middleware/env.rb', line 8

def action
  @action
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



8
9
10
# File 'lib/action_subscriber/middleware/env.rb', line 8

def content_type
  @content_type
end

#encoded_payloadObject (readonly)

Returns the value of attribute encoded_payload.



8
9
10
# File 'lib/action_subscriber/middleware/env.rb', line 8

def encoded_payload
  @encoded_payload
end

#exchangeObject (readonly)

Returns the value of attribute exchange.



8
9
10
# File 'lib/action_subscriber/middleware/env.rb', line 8

def exchange
  @exchange
end

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/action_subscriber/middleware/env.rb', line 8

def headers
  @headers
end

#message_idObject (readonly)

Returns the value of attribute message_id.



8
9
10
# File 'lib/action_subscriber/middleware/env.rb', line 8

def message_id
  @message_id
end

#payloadObject

Returns the value of attribute payload.



6
7
8
# File 'lib/action_subscriber/middleware/env.rb', line 6

def payload
  @payload
end

#queueObject (readonly)

Returns the value of attribute queue.



8
9
10
# File 'lib/action_subscriber/middleware/env.rb', line 8

def queue
  @queue
end

#routing_keyObject (readonly)

Returns the value of attribute routing_key.



8
9
10
# File 'lib/action_subscriber/middleware/env.rb', line 8

def routing_key
  @routing_key
end

#subscriberObject (readonly)

Returns the value of attribute subscriber.



8
9
10
# File 'lib/action_subscriber/middleware/env.rb', line 8

def subscriber
  @subscriber
end

Instance Method Details

#acknowledgeObject



47
48
49
50
51
52
53
# File 'lib/action_subscriber/middleware/env.rb', line 47

def acknowledge
  fail ::RuntimeError, "you can't acknowledge messages under the polling API" unless @channel
  acknowledge_multiple_messages = false
  @has_been_acked = true
  @channel.ack(@delivery_tag, acknowledge_multiple_messages)
  true
end

#nackObject



55
56
57
58
59
60
61
62
# File 'lib/action_subscriber/middleware/env.rb', line 55

def nack
  fail ::RuntimeError, "you can't acknowledge messages under the polling API" unless @channel
  nack_multiple_messages = false
  requeue_message = true
  @has_been_nacked = true
  @channel.nack(@delivery_tag, nack_multiple_messages, requeue_message)
  true
end

#rejectObject



64
65
66
67
68
69
70
# File 'lib/action_subscriber/middleware/env.rb', line 64

def reject
  fail ::RuntimeError, "you can't acknowledge messages under the polling API" unless @channel
  requeue_message = true
  @has_been_rejected = true
  @channel.reject(@delivery_tag, requeue_message)
  true
end

#safe_acknowledgeObject



72
73
74
# File 'lib/action_subscriber/middleware/env.rb', line 72

def safe_acknowledge
  acknowledge if uses_acknowledgements? && @channel && !has_used_delivery_tag?
end

#safe_nackObject



76
77
78
# File 'lib/action_subscriber/middleware/env.rb', line 76

def safe_nack
  nack if uses_acknowledgements? && @channel && !has_used_delivery_tag?
end

#safe_rejectObject



80
81
82
# File 'lib/action_subscriber/middleware/env.rb', line 80

def safe_reject
  reject if uses_acknowledgements? && @channel && !has_used_delivery_tag?
end

#to_hashObject Also known as: to_h



84
85
86
87
88
89
90
91
92
# File 'lib/action_subscriber/middleware/env.rb', line 84

def to_hash
  {
    :action => action,
    :content_type => content_type,
    :exchange => exchange,
    :routing_key => routing_key,
    :payload => payload
  }
end