class Fluent::SlackClient::Base
The base framework of slack client
Attributes
debug_dev[RW]
endpoint[R]
https_proxy[R]
log[RW]
Public Class Methods
new(endpoint = nil, https_proxy = nil)
click to toggle source
@param [String] endpoint
(Incoming Webhook) required https://hooks.slack.com/services/XXX/XXX/XXX (Slackbot) required https://xxxx.slack.com/services/hooks/slackbot?token=XXXXX (Web API) optional and default to be https://slack.com/api/
@param [String] https_proxy
(optional)
https://proxy.foo.bar:port
# File lib/fluent/plugin/slack_client.rb, line 29 def initialize(endpoint = nil, https_proxy = nil) self.endpoint = endpoint if endpoint self.https_proxy = https_proxy if https_proxy @log = Logger.new('/dev/null') end
Public Instance Methods
endpoint=(endpoint)
click to toggle source
# File lib/fluent/plugin/slack_client.rb, line 35 def endpoint=(endpoint) @endpoint = URI.parse(endpoint) end
https_proxy=(https_proxy)
click to toggle source
# File lib/fluent/plugin/slack_client.rb, line 39 def https_proxy=(https_proxy) @https_proxy = URI.parse(https_proxy) @proxy_class = Net::HTTP.Proxy(@https_proxy.host, @https_proxy.port) end
post(endpoint, params)
click to toggle source
# File lib/fluent/plugin/slack_client.rb, line 48 def post(endpoint, params) http = proxy_class.new(endpoint.host, endpoint.port) http.use_ssl = (endpoint.scheme == 'https') http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.set_debug_output(debug_dev) if debug_dev req = Net::HTTP::Post.new(endpoint.request_uri) req['Host'] = endpoint.host req['Accept'] = 'application/json; charset=utf-8' req['User-Agent'] = 'fluent-plugin-slack' req.body = encode_body(params) res = http.request(req) response_check(res, params) end
proxy_class()
click to toggle source
# File lib/fluent/plugin/slack_client.rb, line 44 def proxy_class @proxy_class ||= Net::HTTP end
Private Instance Methods
api()
click to toggle source
Required to implement to use with_channels_create
channels.create API is available from only Slack Web API
# File lib/fluent/plugin/slack_client.rb, line 82 def api raise NotImplementedError end
encode_body(params)
click to toggle source
# File lib/fluent/plugin/slack_client.rb, line 66 def encode_body(params) raise NotImplementedError end
filter_params(params)
click to toggle source
# File lib/fluent/plugin/slack_client.rb, line 76 def filter_params(params) params.dup.tap {|p| p[:token] = '[FILTERED]' if p[:token] } end
recursive_scrub!(params)
click to toggle source
# File lib/fluent/plugin/slack_client.rb, line 116 def recursive_scrub!(params) case params when Hash params.each {|k, v| recursive_scrub!(v)} when Array params.each {|elm| recursive_scrub!(elm)} when String params.force_encoding(Encoding::UTF_8) if params.encoding == Encoding::ASCII_8BIT params.scrub!('?') if params.respond_to?(:scrub!) else params end end
response_check(res, params)
click to toggle source
# File lib/fluent/plugin/slack_client.rb, line 70 def response_check(res, params) if res.code != "200" raise Error.new(res, params) end end
to_json_with_scrub!(params)
click to toggle source
# File lib/fluent/plugin/slack_client.rb, line 101 def to_json_with_scrub!(params) retries = 1 begin params.to_json rescue Encoding::UndefinedConversionError => e recursive_scrub!(params) if (retries -= 1) >= 0 # one time retry log.warn "out_slack: to_json `#{params}` failed. retry after scrub!. #{e.message}" retry else raise e end end end
with_channels_create(params = {}, opts = {}) { || ... }
click to toggle source
# File lib/fluent/plugin/slack_client.rb, line 86 def with_channels_create(params = {}, opts = {}) retries = 1 begin yield rescue ChannelNotFoundError => e if params[:token] and opts[:auto_channels_create] log.warn "out_slack: channel \"#{params[:channel]}\" is not found. try to create the channel, and then retry to post the message." api.channels_create({name: params[:channel], token: params[:token]}) retry if (retries -= 1) >= 0 # one time retry else raise e end end end