LoginSignup
85
78

More than 3 years have passed since last update.

[Ruby][LINE] ごみ出し日に LINE で通知する

Last updated at Posted at 2021-02-23

概要

ごみ出しって忘れちゃうことありますよね :sweat_drops: 捨てられる曜日が限られた燃えないごみやペットボトルの日は特に忘れがちです。そして Google カレンダーの通知だと気づけなくて 😇

そこで cron と Ruby を使用してごみ出し日に LINE (LINE Notify) で通知するようにしました。僕の場合は cron は Raspberry Pi 3 で動かしています。

方法

バージョン情報

  • Ruby 3.0.0
  • Bundler 2.2.11
  • Active Support 6.1.3

コード

Gem は日付に関する処理のために Active Support のみ使用します。

Gemfile
source 'https://rubygems.org'

gem 'activesupport'

LINE 通知については過去に書いた 自分の LINE に Ruby で通知を送る という記事を参考にしました。この記事を参考に LINE Notify のトークンを取得してください。

main.rb
require 'active_support'
require 'active_support/core_ext'
require 'net/http'
require 'uri'

# LINE に通知するためのサービスクラス。
class NotifyLINE
  TOKEN = 'ここに LINE Notify のトークンを入力する。'
  URL = 'https://notify-api.line.me/api/notify'

  attr_reader :message

  def self.call(message)
    new(message).call
  end

  def initialize(message)
    @message = message
  end

  def call
    Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |https|
      https.request(request)
    end
  end

  private

  def request
    request = Net::HTTP::Post.new(uri)
    request['Authorization'] = "Bearer #{TOKEN}"
    request.set_form_data(message: message)
    request
  end

  def uri
    URI.parse(URL)
  end
end

# ゴミ捨て日を表すクラス。
class GarbageDisposalDay
  attr_reader :wday, :nth, :type

  WDAYS = %w(日 月 火 水 木 金 土).freeze

  def initialize(wday:, nth:, type:)
    @wday = WDAYS.index(wday)
    @nth = nth
    @type = type
  end

  def to_s
    "第 #{nth} #{WDAYS[wday]}曜日「#{type}」"
  end

  def apply?(date)
    wday == date.wday && nth == nth_day_of_week(date)
  end

  private

  # 日付がその月で何回目の曜日かどうか。
  def nth_day_of_week(date)
    (date.beginning_of_month..date).count { _1.wday == date.wday }
  end
end

# 住んでいる地域のごみ出し日を定義する。
GARBAGE_DISPOSAL_DAYS = [
  # 毎週月・木は燃えるゴミの日。
  *%w(月 木).product([*1..5]).map { |wday, nth| GarbageDisposalDay.new(wday: wday, nth: nth, type: '燃えるゴミ') },
  GarbageDisposalDay.new(wday: '日', nth: 2, type: '燃えないごみ'),
  GarbageDisposalDay.new(wday: '日', nth: 4, type: '空きびん・ペットボトル'),
  GarbageDisposalDay.new(wday: '火', nth: 2, type: '古紙・ダンボール (翌朝回収)'),
  GarbageDisposalDay.new(wday: '火', nth: 4, type: '古紙・ダンボール (翌朝回収)')
]

today = Date.today
garbage_disposal_day = GARBAGE_DISPOSAL_DAYS.find { _1.apply?(today) }

if garbage_disposal_day
  NotifyLINE.call("今日は#{garbage_disposal_day}の日です。忘れずに捨てましょう!")
end

上記の Ruby ファイルを cron で定期実行します。通知してほしい時刻を指定してください。

crontab
0 19 * * * /bin/bash -c 'export PATH="$HOME/.rbenv/bin:$PATH"; eval "$(rbenv init -)"; cd $HOME/workspace/dustdas; bundle exec ruby main.rb'

これで通知が届きました 💖

なお、僕の住んでいる地域は深夜にごみが回収されるのですが、朝に回収される地域の場合は

  • Date.todayDate.tomorrow に変更する。
  • 通知のメッセージの 今日明日 に変更する。

と変更すればよいと思います。

85
78
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
85
78