Class: Lita::Handlers::Kintai
- Inherits:
-
Handler
- Object
- Handler
- Lita::Handlers::Kintai
- Defined in:
- lib/lita/handlers/kintai.rb
Constant Summary collapse
- OOB_URI =
'urn:ietf:wg:oauth:2.0:oob'- APPLICATION_NAME =
'Lita Kintai'- CLIENT_SECRETS_PATH =
'client_secret.json'- CREDENTIALS_PATH =
File.join(Dir.home, '.credentials', "lita-kintai.yaml")
- SCOPE =
Google::Apis::GmailV1::AUTH_GMAIL_READONLY
- USER_ID =
'default'
Instance Method Summary collapse
- #authorize ⇒ Object
- #authorizer ⇒ Object
- #code(response) ⇒ Object
- #current_kintai ⇒ Object
- #find_mail(query) ⇒ Object
- #find_mail_by_id(id) ⇒ Object
- #kintai(response) ⇒ Object
- #kintai_info ⇒ Object
- #load_on_start(_payload) ⇒ Object
- #schedule ⇒ Object
- #send_kintai(user: user, room: room) ⇒ Object
Instance Method Details
#authorize ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/lita/handlers/kintai.rb', line 117 def return @service if !@servise.nil? && !@servise..nil? credentials = .get_credentials(USER_ID) if credentials @service. = credentials return @service end return nil end |
#authorizer ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/lita/handlers/kintai.rb', line 129 def return unless .nil? @service = Google::Apis::GmailV1::GmailService.new @service..application_name = APPLICATION_NAME FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH)) client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH) token_store = Google::Auth::Stores::FileTokenStore.new( file: CREDENTIALS_PATH) = Google::Auth::UserAuthorizer.new( client_id, SCOPE, token_store) end |
#code(response) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/lita/handlers/kintai.rb', line 54 def code(response) code = response.matches[0][0] .get_and_store_credentials_from_code( user_id: USER_ID, code: code, base_url: OOB_URI) response.reply("Confirmed") end |
#current_kintai ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/lita/handlers/kintai.rb', line 62 def current_kintai if .nil? auth_url = .(base_url: OOB_URI) return "Authenticate your Google account.\nThen tell me the code as follows: `code \\\#{your_code}`\n\n\#{auth_url}\n EOS\n end\n\n kintai_info\nend\n" |
#find_mail(query) ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/lita/handlers/kintai.rb', line 144 def find_mail(query) ids = @service.('me', q: query) return [] unless ids. ids..map do || find_mail_by_id(.id) end end |
#find_mail_by_id(id) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/lita/handlers/kintai.rb', line 153 def find_mail_by_id(id) results = @service.('me', id) body = results.payload.parts ? results.payload.parts.first.body.data : results.payload.body.data headers = results.payload.headers { subject: headers.select { |e| e.name == 'Subject'}.first.value, from: headers.select { |e| e.name == 'From'}.first.value, date: Time.parse(headers.select { |e| e.name == 'Date'}.first.value), body: body.force_encoding('utf-8'), } end |
#kintai(response) ⇒ Object
32 33 34 |
# File 'lib/lita/handlers/kintai.rb', line 32 def kintai(response) response.reply(current_kintai) end |
#kintai_info ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/lita/handlers/kintai.rb', line 76 def kintai_info texts = "" texts << "#{Date.today.strftime("%m/%d")} (#{%w(日 月 火 水 木 金 土)[Date.today.wday]})#{config.template_header}" mails = 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] reason = "私用のため、" if text.match(/電車|列車/) reason = "電車遅延のため、" end if text.match(/体調|痛/) reason = "体調不良のため、" end if text.match(/健康診断|検診|健診/) reason = "健康診断のため、" end at = "出社時刻未定です。" if hm = text.match(/([0-1][0-9]|[2][0-3]):[0-5][0-9]/) at = "#{hm}頃出社予定です。" elsif min = text.match(/([0-5][0-9])分/) at = "10:#{min[1]}頃出社予定です。" end if text.match(/おやすみ|休み|有給|休暇/) reason = "本日お休みです。" at = "" end texts << "#{name}さん: #{reason}#{at}\n" end texts << config. end |
#load_on_start(_payload) ⇒ Object
41 42 43 |
# File 'lib/lita/handlers/kintai.rb', line 41 def load_on_start(_payload) schedule end |
#schedule ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/lita/handlers/kintai.rb', line 45 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) ⇒ Object
36 37 38 39 |
# File 'lib/lita/handlers/kintai.rb', line 36 def send_kintai(user: user, room: room) target = Source.new(user: user, room: room) robot.(target, current_kintai) end |