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
Instance Method Details
#authorize ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/lita/handlers/kintai.rb', line 93 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
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/lita/handlers/kintai.rb', line 105 def return @authorizer unless @authorizer.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) @authorizer = Google::Auth::UserAuthorizer.new( client_id, SCOPE, token_store) end |
#code(response) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/lita/handlers/kintai.rb', line 30 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
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lita/handlers/kintai.rb', line 38 def current_kintai if .nil? auth_url = .(base_url: OOB_URI) return <<-EOS Authenticate your Google account. Then tell me the code as follows: `code \#{your_code}` #{auth_url} EOS end kintai_info end |
#find_mail(query) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/lita/handlers/kintai.rb', line 120 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
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/lita/handlers/kintai.rb', line 129 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
26 27 28 |
# File 'lib/lita/handlers/kintai.rb', line 26 def kintai(response) response.reply(current_kintai) end |
#kintai_info ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/lita/handlers/kintai.rb', line 52 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 |