Class: Dingding::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/dingding-notifier.rb,
lib/dingding-notifier/config.rb,
lib/dingding-notifier/version.rb,
lib/dingding-notifier/util/http_client.rb,
lib/dingding-notifier/payload_middleware.rb,
lib/dingding-notifier/payload_middleware/base.rb,
lib/dingding-notifier/payload_middleware/stack.rb,
lib/dingding-notifier/payload_middleware/format_link.rb,
lib/dingding-notifier/payload_middleware/format_text.rb,
lib/dingding-notifier/payload_middleware/format_markdown.rb,
lib/dingding-notifier/payload_middleware/format_feed_card.rb,
lib/dingding-notifier/payload_middleware/format_action_card.rb

Defined Under Namespace

Modules: Util Classes: APIError, Config, PayloadMiddleware

Constant Summary collapse

VERSION =

rubocop:disable Style/RedundantFreeze

"0.0.1.1".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Notifier

Returns a new instance of Notifier.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dingding-notifier.rb', line 17

def initialize(options={})
  @webhook_url =  options.delete(:webhook_url)
  @client = config.http_client
  @atMobiles = options.delete(:atMobiles) || []
  @atUserIds = options.delete(:atUserIds) || []
  @isAtAll =  options.delete(:isAtAll) || false
  @middleware_name = config.middleware(options.delete(:middleware))
  if @middleware_name.is_a?(Array)
    @middleware_name = @middleware_name.first
  end
  middleware.set (@middleware_name)
end

Instance Attribute Details

#atMobilesObject (readonly)

Returns the value of attribute atMobiles.



15
16
17
# File 'lib/dingding-notifier.rb', line 15

def atMobiles
  @atMobiles
end

#atUserIdsObject (readonly)

Returns the value of attribute atUserIds.



15
16
17
# File 'lib/dingding-notifier.rb', line 15

def atUserIds
  @atUserIds
end

#clientObject (readonly)

Returns the value of attribute client.



15
16
17
# File 'lib/dingding-notifier.rb', line 15

def client
  @client
end

#isAtAllObject (readonly)

Returns the value of attribute isAtAll.



15
16
17
# File 'lib/dingding-notifier.rb', line 15

def isAtAll
  @isAtAll
end

#middleware_nameObject (readonly)

Returns the value of attribute middleware_name.



15
16
17
# File 'lib/dingding-notifier.rb', line 15

def middleware_name
  @middleware_name
end

#webhook_urlObject (readonly)

Returns the value of attribute webhook_url.



15
16
17
# File 'lib/dingding-notifier.rb', line 15

def webhook_url
  @webhook_url
end

Instance Method Details

#call(message, options = {}) ⇒ Object



34
35
36
37
38
# File 'lib/dingding-notifier.rb', line 34

def call message, options={}
  options[:format_text] ||= {}
  options[:format_text][:text] = message.to_json
  post options
end

#configObject



30
31
32
# File 'lib/dingding-notifier.rb', line 30

def config
  @_config ||= Config.new
end

#post(payload = {}) ⇒ Object



40
41
42
43
44
# File 'lib/dingding-notifier.rb', line 40

def post payload={}
  payload = {middleware_name.to_sym => payload} unless payload.key?(middleware_name)
  payload.merge!({atMobiles: atMobiles, atUserIds: atUserIds,isAtAll: isAtAll})
  client.post webhook_url, middleware.call(payload).to_json
end