Module: OnsOnRails::Subscriber::ClassMethods

Defined in:
lib/ons_on_rails/subscriber.rb

Instance Method Summary collapse

Instance Method Details

#check_subscriber_definition!Object

Determine whether it is a valid subscriber or not.



46
47
48
49
50
# File 'lib/ons_on_rails/subscriber.rb', line 46

def check_subscriber_definition!
  keys = %i(access_key secret_key consumer_id topic tag)
  keys.each { |key| raise "missing key :#{key} in ons options" unless ons_options.key?(key) }
  raise 'method #consume not implemented' unless instance_methods(false).include?(:consume)
end

#consume(message) ⇒ Boolean

Create a new subscriber instance to consume the incoming message.

Parameters:

  • message (Hash{Symbol => Object})

Options Hash (message):

  • topic, (String)

    the message topic

  • tag, (String)

    the message tag

  • body, (String)

    the message body

  • id, (String)

    the message id

  • key, (String)

    the message key

Returns:

  • (Boolean)

    true/CommitMessage or false/ReconsumeLater



36
37
38
39
40
41
42
43
# File 'lib/ons_on_rails/subscriber.rb', line 36

def consume(message)
  new.consume(message)
  true
rescue => ex
  OnsOnRails.logger.error ex.message
  OnsOnRails.logger.error ex.backtrace.join("\n")
  false
end

#ons_options(options = {}) ⇒ Object

Allows customization for this type of subscriber.

Parameters:

  • options (Hash{String, Symbol => String}) (defaults to: {})

Options Hash (options):

  • :access_key (String)

    the access key to aliyun ONS

  • :secret_key (String)

    the secret key to aliyun ONS

  • :consumer_id (String)

    the consumer ID

  • :topic (String)

    the message topic

  • :tag (String)

    the subscribe expression used to filter messages



17
18
19
20
21
22
23
24
25
# File 'lib/ons_on_rails/subscriber.rb', line 17

def ons_options(options = {})
  @ons_options ||= begin
    opts = OnsOnRails.ons_default_options
    opts.slice(:access_key, :secret_key).merge(opts.fetch(name.to_s.underscore.to_sym, {}))
  end

  return @ons_options if options.blank?
  @ons_options.merge!(options.symbolize_keys)
end