class Lita::Handlers::Kintai

Public Class Methods

kintai_from_text(text) click to toggle source
# File lib/lita/handlers/kintai.rb, line 152
def self.kintai_from_text(text)
  reason = kintai_reason(text)
  time = kintai_time(text)
  "#{reason}のため、#{time}です。"
end
kintai_reason(text) click to toggle source
# File lib/lita/handlers/kintai.rb, line 172
def self.kintai_reason(text)
  if text.match(/電車|列車/)
    return "電車遅延"
  elsif text.match(/体調|痛/)
    return "体調不良"
  elsif text.match(/健康診断|検診|健診/)
    return "健康診断"
  end
  return  "私用"
end
kintai_time(text) click to toggle source
# File lib/lita/handlers/kintai.rb, line 183
def self.kintai_time(text)
  if hm = text.match(/([0-1][0-9]|[2][0-3]):[0-5][0-9]/)
    return "#{hm}頃出社予定"
  elsif min = text.match(/(([0-5])*[0-9])分/)
    return "10:#{min[1].rjust(2, "0")}頃出社予定"
  elsif half = text.match(/([0-1][0-9]|[2][0-3])時半/)
    return "#{half[1]}:30頃出社予定"
  elsif half = text.match(/(\d)時間半/)
    return "#{10+half[1].to_i}:30頃出社予定"
  elsif text.match(/おやすみ|休み|有給|休暇/)
    return "本日お休み"
  end
  return "出社時刻未定"
end

Public Instance Methods

authenticate_info() click to toggle source
# File lib/lita/handlers/kintai.rb, line 198
      def authenticate_info
        <<-EOS
Authenticate your Google account.
Then tell me the code as follows: `code \#{your_code}`

#{Gmail.authorization_url}
        EOS
      end
code(response) click to toggle source
# File lib/lita/handlers/kintai.rb, line 47
def code(response)
  code = response.matches[0][0]
  Gmail.credentials_from_code(code)

  response.reply("Confirmed")
end
create_kintai_mail(info) click to toggle source
# File lib/lita/handlers/kintai.rb, line 104
def create_kintai_mail(info)
  create_mail(
    to: config.mail_to,
    cc: config.mail_cc,
    subject: kintai_subject,
    body: kintai_body(info)
  )
end
create_mail(to: to, cc: cc, subject: subject, body: body) click to toggle source
# File lib/lita/handlers/kintai.rb, line 122
def create_mail(to: to, cc: cc, subject: subject, body: body)
  Mail.new do
    to to
    cc cc
    subject subject
    body body
  end
end
draft(response) click to toggle source
# File lib/lita/handlers/kintai.rb, line 33
def draft(response)
  info = response.matches[0][0]
  register_draft(response, info)
end
kintai(response) click to toggle source
# File lib/lita/handlers/kintai.rb, line 25
def kintai(response)
  if Gmail.authorized?
    register_draft(response, kintai_info)
  else
    response.reply(authenticate_info)
  end
end
kintai_body(info) click to toggle source
# File lib/lita/handlers/kintai.rb, line 162
      def kintai_body(info)
        <<-EOS
#{config.template_header}

#{info}

#{config.template_footer}
        EOS
      end
kintai_info() click to toggle source
# File lib/lita/handlers/kintai.rb, line 135
def kintai_info
  texts = ""

  mails = Gmail.find_mail(config.query)
  # query の `newer:#{Date.today.strftime("%Y/%m/%d")}` 昨日のも一部返ってくる
  # `newer_than:1d` だと24h以内になるので、ここで今日のだけにする
  mails.select{ |m| m[:date] > Date.today.to_time }.each do |m|
    name = m[:from].split("\"")[1]

    text = m[:subject] + m[:body]
    info = Kintai.kintai_from_text(text)

    texts << "#{name}さん: #{info}\n"
  end
  texts << config.template_info
end
kintai_subject() click to toggle source
# File lib/lita/handlers/kintai.rb, line 158
def kintai_subject
  "#{Date.today.strftime("%m/%d")} (#{%w(日 月 火 水 木 金 土)[Date.today.wday]})#{config.template_subject}"
end
load_on_start(_payload) click to toggle source
# File lib/lita/handlers/kintai.rb, line 54
def load_on_start(_payload)
  schedule
end
mail_to_message(mail) click to toggle source
# File lib/lita/handlers/kintai.rb, line 113
      def mail_to_message(mail)
        <<-EOS
To: #{mail.to}
Cc: #{mail.cc}
Subject: #{mail.subject}
#{mail.body.to_s}
        EOS
      end
reaction_added(_payload) click to toggle source
# File lib/lita/handlers/kintai.rb, line 78
def reaction_added(_payload)
  case _payload[:name]
  when '+1' then
    if !@@draft.nil? &&
      _payload[:item]["type"] == "message" &&
      _payload[:item]["channel"] == @@draft[:channel] &&
      _payload[:item]["ts"] == @@draft[:ts]
      if Gmail.authorized?
        # TODO: 成功失敗
        send_mail(@@draft[:mail])
        @@draft = nil
        send_message(room: _payload[:item]["channel"],
          message: 'Sent email.')
      else
        send_message(room: _payload[:item]["channel"],
          message: authenticate_info)
      end
    end
  end
end
register_draft(response, info) click to toggle source
# File lib/lita/handlers/kintai.rb, line 38
def register_draft(response, info)
  mail = create_kintai_mail(info)
  reply = response.reply(mail_to_message(mail))
  if (reply.kind_of?(Hash) && reply.has_key?("channel") && reply.has_key?("ts"))
    @@draft = { channel: reply["channel"], ts: reply["ts"], mail: mail }
  end
  reply
end
schedule() click to toggle source
# File lib/lita/handlers/kintai.rb, line 58
def schedule
  return if config.schedule_cron.nil?
  return if config.schedule_room.nil?
  scheduler = Rufus::Scheduler.new
  scheduler.cron config.schedule_cron do
    send_kintai(room: config.schedule_room)
  end
end
send_kintai(user: user, room: room) click to toggle source
# File lib/lita/handlers/kintai.rb, line 67
def send_kintai(user: user, room: room)
  if Gmail.authorized?
    mail = create_kintai_mail(kintai_info)
    reply = send_message(user: user, room: room, message: mail_to_message(mail))
    @@draft = { channel: reply["channel"], ts: reply["ts"], mail: mail }
    reply
  else
    send_message(user: user, room: room, message: authenticate_info)
  end
end
send_mail(mail) click to toggle source
# File lib/lita/handlers/kintai.rb, line 131
def send_mail(mail)
  return Gmail.send_message(mail)
end
send_message(user: user, room: room, message: message) click to toggle source
# File lib/lita/handlers/kintai.rb, line 99
def send_message(user: user, room: room, message: message)
  target = Source.new(user: user, room: room)
  robot.send_message(target, message)
end