class Fluent::DaioikachanInput::App

Attributes

log[R]
router[R]

Public Class Methods

new(plugin) click to toggle source
# File lib/fluent/plugin/in_daioikachan.rb, line 67
def initialize(plugin)
  @router = plugin.router
  @log    = plugin.log
end

Public Instance Methods

notice(params) click to toggle source
# File lib/fluent/plugin/in_daioikachan.rb, line 103
def notice(params)
  channel, message = build_channel(params), build_message(params)
  tag    = "notice.#{channel}"
  record = params.merge('command' => 'notice', 'channel' => channel, 'message' => message)
  router.emit(tag, Fluent::Engine.now, record)
end
privmsg(params) click to toggle source
# File lib/fluent/plugin/in_daioikachan.rb, line 110
def privmsg(params)
  channel, message = build_channel(params), build_message(params)
  tag    = "privmsg.#{channel}"
  record = params.merge('command' => 'privmsg', 'channel' => channel, 'message' => message)
  router.emit(tag, Fluent::Engine.now, record)
end
run(env) click to toggle source
# File lib/fluent/plugin/in_daioikachan.rb, line 72
def run(env)
  req = Rack::Request.new(env)
  begin
    if req.post?
      params = req.params
      case req.path
      when '/notice'
        notice(params)
      when '/privmsg'
        privmsg(params)
      when '/join'
        return ok
      when '/leave'
        return ok
      else
        return not_found
      end
    else
      return not_found
    end
  rescue BadRequest => e
    bad_request(e.message)
  rescue => e
    log.error "out_slack:", :error => e.to_s, :error_class => e.class.to_s
    log.warn_backtrace e.backtrace
    internal_server_error
  else
    ok
  end
end

Private Instance Methods

bad_request(msg = nil) click to toggle source
# File lib/fluent/plugin/in_daioikachan.rb, line 141
def bad_request(msg = nil)
  [400, {'Content-type'=>'text/plain'}, ["Bad Request\n#{msg}"]]
end
build_channel(params) click to toggle source
# File lib/fluent/plugin/in_daioikachan.rb, line 119
def build_channel(params)
  unless channel = params.delete('channel')
    raise BadRequest.new('`channel` parameter is mandatory')
  end
  if channel.start_with?('#')
    channel[1..-1] # remove starting #
  else
    channel
  end
end
build_message(params) click to toggle source
# File lib/fluent/plugin/in_daioikachan.rb, line 130
def build_message(params)
  unless message = params.delete('message')
    raise BadRequest.new('`message` parameter is mandatory')
  end
  message # should I truncate message to max_length?
end
internal_server_error(msg = nil) click to toggle source
# File lib/fluent/plugin/in_daioikachan.rb, line 149
def internal_server_error(msg = nil)
  [500, {'Content-type'=>'text/plain'}, ["Internal Server Error\n#{msg}"]]
end
not_found(msg = nil) click to toggle source
# File lib/fluent/plugin/in_daioikachan.rb, line 145
def not_found(msg = nil)
  [404, {'Content-type'=>'text/plain'}, ["Not Found\n#{msg}"]]
end
ok(msg = nil) click to toggle source
# File lib/fluent/plugin/in_daioikachan.rb, line 137
def ok(msg = nil)
  [200, {'Content-type'=>'text/plain'}, ["OK\n#{msg}"]]
end