forgeplus/app/services/notice/write/create_service.rb

42 lines
1005 B
Ruby
Raw Normal View History

2021-09-13 17:51:34 +08:00
class Notice::Write::CreateService < Notice::Write::ClientService
attr_accessor :receivers, :sender, :content, :notification_url, :source, :extra, :type
2021-09-15 18:55:58 +08:00
def initialize(receivers, content, notification_url, source, extra={}, type=1, sender=-1)
2021-09-13 17:51:34 +08:00
@receivers = receivers
@sender = sender
@content = content
@notification_url = notification_url
@source = source
@extra = extra
@type = type
end
def call
2021-09-15 18:55:58 +08:00
return nil if request_receivers.blank?
2021-09-22 14:25:56 +08:00
result = post(url, request_params)
2021-09-13 17:51:34 +08:00
response = render_response(result)
end
private
def request_receivers
2021-09-15 18:55:58 +08:00
receivers.is_a?(Array) ? receivers.join(",") : receivers
2021-09-13 17:51:34 +08:00
end
def request_params
Hash.new.merge(data: {
receivers: request_receivers,
sender: sender,
content: content,
notification_url: notification_url,
source: source,
extra: extra.to_json.to_s,
type: type
}.stringify_keys)
end
2021-09-22 14:25:56 +08:00
def url
"/notification/#{platform}".freeze
end
2021-09-13 17:51:34 +08:00
end