module ActiveNotifier # ActiveNotifier support for configuration module Configurable extend ActiveSupport::Concern # Core functions for Configuration module ClassMethods # Configure for ActiveNotifier. # # @example # ActiveNotifier.configure do |config| # config.const_name = :Message # config.adapter = :slack # # ... # end # @yield [config] Give a Configuration instance for settings # @raise [ConfigureError] def configure yield config end def config @config ||= Configuration.new end end # Store the configuration data class Configuration # Alias const for ActiveNotifier # @!attribute [r] attr_reader :const_name # Set alias const for ActiveNotifier # @param const_name [#to_sym] (:Notifier) Alias const name # @example # ActiveNotifier.config.const_name = :Message # ::Message == ::ActiveNotifier # => true def const_name=(const_name) const_name = const_name.to_sym const_name = :Notifier if const_name.empty? if Kernel.const_defined?(const_name) raise ActiveNotifier::ConfigureError, "const_name is already defined, please set another value" end Kernel.const_set(const_name, ActiveNotifier) @const_name = const_name end # Message Adapter def adapter @adapter || :dingtalk end # Set message Adapter # @param adapter [#to_sym] (:dingtalk) def adapter=(adapter) adapter = adapter.to_sym unless adapters_with_base_url.key?(adapter) raise ActiveNotifier::ConfigureError, "adapter only support: #{adapters_with_base_url.keys.join(', ')}" end @adapter = adapter end # The tokens of channel def channel_tokens @channel_tokens || {} end # Set the tokens of channel # @param channel_tokens [#to_h] ({}) # @example # ActiveNotifier.config.channel_tokens = { # default: "xxx", # order: "xxx" # } def channel_tokens=(channel_tokens) channel_tokens = channel_tokens.to_h.symbolize_keys @channel_tokens = channel_tokens end # The template home directory def template_home @template_home || "#{File.expand_path(__dir__)}/templates" end # Set the template home directory # @param template_home [#to_s] (lib/active_notifier/templates) def template_home=(template_home) template_home = template_home.to_s raise ActiveNotifier::ConfigureError, "template_home value can't be blank" if template_home.empty? @template_home = template_home end # The priority type for message, if notifier execute without type option and there are multiple valid types, # then ActiveNotifier will use the template with this type first def priority_type @priority_type || :markdown end # Set the priority type # @param priority_type [#to_sym] (:markdown) def priority_type=(priority_type) @priority_type = priority_type.to_sym end # The adapters and its base url def adapters_with_base_url @adapters_with_base_url ||= { dingtalk: "https://oapi.dingtalk.com/robot/send?access_token=" } end end end end