Module: ActiveNotifier::Core::ClassMethods

Defined in:
lib/active_notifier/core.rb

Instance Method Summary collapse

Instance Method Details

#exec(channel, **options) ⇒ Object

Message execute

Examples:

ActiveNotifer.config.channel_tokens = {
  default: "xxx"
}
ActiveNotifier.exec(:default)

Parameters:

  • channel (#to_sym)

    Message channel, it will set template and token together

  • options (Hash)

    ({})

Options Hash (**options):

  • :template (String) — default: nil

    Message template, will use ‘options` when this value is blank

  • :token (String, Symbol) — default: nil

    Message webhook token, will use ‘options` when this value is blank

  • :type (Symbol) — default: nil

    Message type, it will use a specific template file, for example, when type is :text and template is order, then ActiveNotifier will choose ‘order.text.erb` for template file

  • :data (Hash) — default: {}

    Message variable data for template, it can used in erb template file: ## #:title > #.to_json

  • Other (Anything)

    options will used in adapter message execute, like Dingtalk require title for message, we can pass it here: ‘ActiveNotifer.exec(:default, title: “dingtalk title”)`

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_notifier/core.rb', line 30

def exec(channel, **options)
  channel = channel&.to_sym
  raise ArgumentError, ":channel can't be blank" if channel.blank?
  template = options[:template].to_s.presence || channel.to_s
  token = get_token(channel, options[:token])
  raise UndefinedTokenError if token.blank?
  type = options[:type]&.to_sym.presence || guess_type_by_files(template)
  data = options[:data].to_h

  message = get_message(template, type, data)
  adapter = ActiveNotifier.adapt(ActiveNotifier.config.adapter)
  adapter_options = options.except(:token, :template, :type, :data)
  adapter.notify(token, type, message, adapter_options)
end