class SardonyxRing::App

Public Class Methods

new(options = {}) click to toggle source
# File lib/sardonyx_ring/app.rb, line 7
def initialize(options = {})
  @app_token = options[:app_token]
  @bot_token = options[:bot_token]
  @bot_auth = nil
  @logger = options[:logger] || Logger.new($stdout, level: :info)
end

Public Instance Methods

client() click to toggle source
# File lib/sardonyx_ring/app.rb, line 14
def client
  @client ||= Slack::Web::Client.new(token: @bot_token)
end
socket_start!() click to toggle source
# File lib/sardonyx_ring/app.rb, line 18
def socket_start!
  fetch_bot_auth

  EM.run do
    start_cron

    socket_client = Services::SlackSocketClient.new(
      token: @app_token,
      logger: @logger
    )
    socket_client.on_message(&method(:handle_event))
    socket_client.connect!
  end
end

Private Instance Methods

fetch_bot_auth() click to toggle source
# File lib/sardonyx_ring/app.rb, line 50
def fetch_bot_auth
  resp = client.auth_test
  raise 'auth error' unless resp['ok']

  @bot_auth = OpenStruct.new(resp)
end
handle_event(payload) click to toggle source
# File lib/sardonyx_ring/app.rb, line 57
def handle_event(payload)
  case payload.type
  when 'block_actions'
    on_action(payload)
  when 'view_submission'
    on_view(payload)
  when 'event_callback'
    if payload.event.type == 'message'
      on_message(payload)
    else
      on_event(payload)
    end
  end
end
on_action(payload) click to toggle source
# File lib/sardonyx_ring/app.rb, line 72
def on_action(payload)
  @logger.debug("action payload: #{payload}")

  payload.actions.each do |current_action|
    action_event = Events::ActionEvent.new(payload, current_action)

    action_handlers.each do |handler|
      next unless handler.match?(action_event)

      handler.run(self, action_event)
      break
    end
  end
end
on_event(payload) click to toggle source
# File lib/sardonyx_ring/app.rb, line 127
def on_event(payload)
  @logger.debug("event payload: #{payload}")

  event = Events::GeneralEvent.new(payload)

  event_handlers.each do |handler|
    next unless handler.match?(event)

    handler.run(self, event)
  end
end
on_message(payload) click to toggle source
# File lib/sardonyx_ring/app.rb, line 100
def on_message(payload)
  @logger.debug("message payload: #{payload}")

  return if payload.event.user == @bot_auth.user
  return if payload.event.bot_id

  message = Events::MessageEvent.new(payload)
  message.register_say_handler(method(:say))

  message_handlers.each do |handler|
    match = handler.match(payload.event)
    next unless match

    handler.run(self, message, match)
    break
  end
end
on_view(payload) click to toggle source
# File lib/sardonyx_ring/app.rb, line 87
def on_view(payload)
  @logger.debug("view payload: #{payload}")

  view_event = Events::ViewEvent.new(payload)

  view_handlers.each do |handler|
    next unless handler.match?(view_event)

    handler.run(self, view_event)
    break
  end
end
register_next_cron(cron_handler) click to toggle source
# File lib/sardonyx_ring/app.rb, line 41
def register_next_cron(cron_handler)
  time = Time.now
  next_time = [(cron_handler.next_time(time) - time).ceil, 1].max
  EventMachine::Timer.new(next_time) do
    cron_handler.run(self)
    register_next_cron(cron_handler)
  end
end
say(message_event, params = {}) click to toggle source
# File lib/sardonyx_ring/app.rb, line 118
def say(message_event, params = {})
  client.chat_postMessage(
    {
      channel: message_event.raw_payload.event.channel,
      as_user: true
    }.merge(params)
  )
end
start_cron() click to toggle source
# File lib/sardonyx_ring/app.rb, line 35
def start_cron
  cron_handlers.each do |handler|
    register_next_cron(handler)
  end
end