Module: ActiveNotifier::Notifiable::ClassMethods

Defined in:
lib/active_notifier/notifiable.rb

Overview

Core functions for Notifiable

Instance Method Summary collapse

Instance Method Details

#exec(channel = nil, **options) ⇒ Object

Message execute.

Examples:

Command Usage

ActiveNotifier.exec(token: "your-webhook-token", message: "your-message")

Common Usage

# Configure it
ActiveNotifier.configure do |config|
  config.channel_tokens = { my_channel: "xxx" }
  config.template_home = Rails.root.join("app", "views", "active_notifier")
end

# Add template
echo "## #{data[:title]\n> #{data[:body]}}" > app/views/active_notifier/my_channel.markdown.erb

# Execute it
ActiveNotifier.exec(:my_channel, data: { title: "Message Title", body: "Message Body" })

Parameters:

  • channel (#to_sym) (defaults to: nil)

    (nil) Message channel, it will set template and token_channel together

  • options (#to_h)

    ({})

Options Hash (**options):

  • :token (#to_s) — default: nil

    Message webhook token,

  • :message (#to_s) — default: nil

    Message

  • :token_channel (#to_sym) — default: nil

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

  • :template (#to_sym) — default: nil

    Message template, will use `channel` when this value is blank

  • :adapter (#to_sym) — default: ActiveNotifier.config.adapter

    Message adapter

  • :type (#to_sym) — 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 (#to_h) — default: {}

    Message variable data for template, it can used in erb template file

  • 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:

See Also:

  • more see README

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_notifier/notifiable.rb', line 41

def exec(channel = nil, **options)
  channel       = channel&.to_sym
  token         = options[:token]&.to_s
  message       = options[:message]&.to_s
  token_channel = options[:token_channel]&.to_sym
  template      = options[:template]&.to_sym
  adapter       = options[:adapter]&.to_sym
  type          = options[:type]&.to_sym
  data          = options[:data].to_h
  token = fetch_token(token, token_channel, channel)
  template ||= channel
  type ||= get_type_by_template(template) || :text
  message ||= get_message(template, type, data)
  adapter_options = options.except(:token, :message, :token_channel, :template, :adapter, :type, :data)
  adapter ||= ActiveNotifier.config.adapter.to_sym
  notify(adapter, token, type, message, adapter_options)
end