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
# 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)
  @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
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



43
44
45
46
47
48
# File 'lib/action_subscriber/middleware/env.rb', line 43

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

#rejectObject



50
51
52
53
54
55
# File 'lib/action_subscriber/middleware/env.rb', line 50

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

#to_hashObject Also known as: to_h



57
58
59
60
61
62
63
64
65
# File 'lib/action_subscriber/middleware/env.rb', line 57

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