class HonestPubsub::Subscriber

Attributes

context[RW]
delivery_properties[RW]
delivery_routing_data[RW]

Public Class Methods

handle_errors_with(handler) click to toggle source

Sets an error handler for the class

# File lib/honest_pubsub/subscriber.rb, line 40
def self.handle_errors_with(handler)
  error_handler = handler
end
inherited(klass) click to toggle source
# File lib/honest_pubsub/subscriber.rb, line 13
def self.inherited(klass)
  @@registered_subscribers << klass
end
new(delivery_routing_data, delivery_properties, context) click to toggle source

@param [Hash] context Context from invocation @param [Object] delivery_routing_data Contains routing information like originator and routing key @param [Object] delivery_properties

# File lib/honest_pubsub/subscriber.rb, line 47
def initialize(delivery_routing_data, delivery_properties, context)
  @delivery_routing_data = delivery_routing_data
  @delivery_properties   = delivery_properties
  @context               = context
end
subscribe_to(routing_key_name, options = {}) click to toggle source

Specify the routing key that the subscriber class should listen to. @param [String] routing_key_name The routing key to subscribe to. Must be characters only separated by periods (.) @param [Hash] options Allowed option is :on to optionally specify a queue. If not provided, queue name is generated from the routing key

# File lib/honest_pubsub/subscriber.rb, line 20
def self.subscribe_to(routing_key_name, options = {})
  options.assert_valid_keys(:on)
  unless validate_routing_key_name(routing_key_name)
    raise ArgumentError.new("#{routing_key_name} is not supported. Only lower case characters separated by periods are allowed.")
  end
  self.subscribed_key   = routing_key_name
  self.subscribed_queue = generated_queue_name(routing_key_name, options[:on])
end
validates_payload_with(*validators) click to toggle source

Sets the validator for payload

@param validator The validator to use for validating the payload.

Returns false if the payload is not valid.
Proc must accept a payload as an argument.
# File lib/honest_pubsub/subscriber.rb, line 34
def self.validates_payload_with(*validators)
  self.payload_validators ||= []
  self.payload_validators += validators
end

Private Class Methods

generated_queue_name(routing_key, queue_name) click to toggle source
# File lib/honest_pubsub/subscriber.rb, line 93
def self.generated_queue_name(routing_key, queue_name)
  return queue_name if queue_name.present?
  [HonestPubsub::Configuration.application_name.to_s.gsub(/[^\w\_]/, ''), routing_key.gsub(".", '_')].reject(&:blank?).join('_')
end
validate_routing_key_name(key) click to toggle source
# File lib/honest_pubsub/subscriber.rb, line 88
def self.validate_routing_key_name(key)
  return true if key.blank?
  key.match(/\A([a-z]+\.?)*([a-z]+)\Z/).present?
end

Public Instance Methods

perform(payload) click to toggle source

Actual subscribers need to implement perform method. This is the method where the message is actually processed. @param [Object] payload Payload of the message

# File lib/honest_pubsub/subscriber.rb, line 66
def perform(payload)
  raise "Need implementation for your worker."
end
perform!(payload) click to toggle source

Performs validation if validates_payload_with is defined and then calls the perform method @param [Object] payload Payload of the message

# File lib/honest_pubsub/subscriber.rb, line 55
def perform!(payload)
  if !valid_payload?(payload)
    HonestPubsub.logger.error("Payload validation failed for #{self.class.name}")
    raise ::HonestPubsub::PayloadValidationError.new("Invalid Payload for #{self.class.name}")
  end

  perform(context, payload)
end
routing_key() click to toggle source

@return [String] The original routing key with which the current message was published

# File lib/honest_pubsub/subscriber.rb, line 71
def routing_key
  delivery_routing_data[:routing_key]
end
valid_payload?(payload) click to toggle source

Iterates over all the payload validators and returns false if any of them are false @param [Object] payload The payload/arguments of the message @return [Boolen] Should return true or false value - If no validators are specified, then returns true

# File lib/honest_pubsub/subscriber.rb, line 78
def valid_payload?(payload)
  return true unless payload_validators.present?

  payload_validators.inject(true) { |is_valid, validator|
    is_valid && (validator.respond_to?(:call) ? validator.call(payload) : send(validator, payload))
  }
end