module ActiveNotifier module Configurable def self.included(base) base.extend ClassMethods end module ClassMethods def config @config ||= Configuration.new end def configure yield config end end class Configuration # 常量名, 简化调用, 符号,默认 :Notifier (如果被占用请更换) attr_reader :const_name def const_name=(const_name) const_name = const_name.to_sym const_name = "Notifier" if const_name.empty? raise ActiveNotifier::ConfigureError, "const_name 已经存在,请配置其他值" if Kernel.const_defined?(const_name) Kernel.const_set(const_name, ActiveNotifier) @const_name = const_name end # 消息适配器, 字符串, 默认 :dingtalk def adapter @adapter || :dingtalk end def adapter=(adapter) adapter = adapter.to_sym unless adapters_with_base_url.key?(adapter) raise ActiveNotifier::ConfigureError, "adapter 当前只支持:#{adapters_with_base_url.keys.join(', ')}" end @adapter = adapter end # 消息渠道的 channel_tokens 设置 def channel_tokens @channel_tokens || {} end def channel_tokens=(channel_tokens) channel_tokens = channel_tokens.to_h.symbolize_keys @channel_tokens = channel_tokens end # 消息模版主目录, 字符串, 默认 views/notifier/application), 不需要加模版后缀 def template_home @template_home || "#{File.expand_path(__dir__)}/templates" end def template_home=(template_home) template_home = template_home.to_s raise ActiveNotifier::ConfigureError, "模板主目录不能为空" if template_home.empty? @template_home = template_home end # 消息优先类型, 字符串, 默认 :markdown, 对应模板后缀名,如果未指定该参数且存在多个模板,则优先选择此类型 def priority_type @priority_type || :markdown end def priority_type=(priority_type) @priority_type = priority_type.to_sym end # 适配器以及基本连接 def adapters_with_base_url @adapters_with_base_url ||= { dingtalk: "https://oapi.dingtalk.com/robot/send?access_token=" } end end end end