Sha256: a3eaa71fe2148480b9230ce6ef61b5900937b27b206acf01e18518b18349a58d

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require 'icalendar'
require 'open-uri'
require 'time'

module Lita
  module Handlers
    class Gomi < Handler
      config :ical_url,    required: true # I use this site: http://gomical.net/
      config :period_time, required: true # e.g. '8:30'

      route /^gomi$/,         :gomi,    help: {'gomi'         => 'Shows next collection.'}
      route /^gomi refresh$/, :refresh, help: {'gomi refresh' => 'Refreshes gomi data.'}

      def gomi(response)
        event = next_collection

        response.reply ":recycle: #{event.dtstart.strftime('%m/%d')} #{event.summary}"
      end

      def refresh(response)
        redis.set :ical, open(config.ical_url).read

        response.reply ':recycle:'
      end

      def next_collection
        calendar = Icalendar.parse(redis.get(:ical)).first

        calendar.events.sort_by(&:dtstart).find {|event|
          event.dtstart >= collection_date
        }
      end

      def collection_date
        period = Time.parse(config.period_time)

        Time.now < period ? Date.today : Date.today.succ
      end
    end

    Lita.register_handler Gomi
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lita-gomi-0.1.0 lib/lita/handlers/gomi.rb