class Alerty::Plugin::Slack::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/alerty/plugin/slack/slack_client.rb, line 48
def initialize(endpoint = nil, https_proxy = nil)
  self.endpoint    = endpoint    if endpoint
  self.https_proxy = https_proxy if https_proxy
  @log = Logger.new(nil)
end

Public Instance Methods

endpoint=(endpoint) click to toggle source
# File lib/alerty/plugin/slack/slack_client.rb, line 54
def endpoint=(endpoint)
  @endpoint    = URI.parse(endpoint)
end
https_proxy=(https_proxy) click to toggle source
# File lib/alerty/plugin/slack/slack_client.rb, line 58
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/alerty/plugin/slack/slack_client.rb, line 67
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
  # http.open_timeout = # default: 60s
  # http.read_timeout = # default: 60s

  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/alerty/plugin/slack/slack_client.rb, line 63
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/alerty/plugin/slack/slack_client.rb, line 103
def api
  raise NotImplementedError
end
encode_body(params) click to toggle source
# File lib/alerty/plugin/slack/slack_client.rb, line 87
def encode_body(params)
  raise NotImplementedError
end
filter_params(params) click to toggle source
# File lib/alerty/plugin/slack/slack_client.rb, line 97
def filter_params(params)
  params.dup.tap {|p| p['token'] = '[FILTERED]' if p['token'] }
end
response_check(res, params) click to toggle source
# File lib/alerty/plugin/slack/slack_client.rb, line 91
def response_check(res, params)
  if res.code != "200"
    raise Error.new(res, params)
  end
end
with_channels_create(params = {}, opts = {}) { || ... } click to toggle source
# File lib/alerty/plugin/slack/slack_client.rb, line 107
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