class Holidays
Constants
- PLUGIN
Public Class Methods
Source
# File plugins/holidays.rb, line 20 def self.command_pattern /holidays(?: (.+))?$/i end
Source
# File plugins/holidays.rb, line 72 def self.format_set_ack(country) code = HolidayCalendar.country_label(country) [ IrcFormat.report_header(tag: "HOLIDAYS", title: "#{code} ยท channel default", style: HolidayCalendar::STYLE_TITLE), "Channel default country updated.", IrcFormat.report_footer("public holidays", code) ].join("\n") end
Source
# File plugins/holidays.rb, line 60 def self.lookup(country: nil, year: nil, fetch: nil, today: HolidayCalendar.local_date) fetch ||= lambda do |url| RabbotHttp.fetch(url, user_agent: HolidayCalendar::USER_AGENT) end HolidayCalendar.lookup_report( country: country || HolidayCalendar::DEFAULT_COUNTRY, year: year, fetch: fetch, today: today ) end
Source
# File plugins/holidays.rb, line 32 def self.parse_command(args) text = args.to_s.strip return { action: :show } if text.empty? if (match = text.match(/\Aset\s+(.+)\z/i)) country = HolidayCalendar.normalize_country(match[1]) return { action: :error, error: "Unknown country: #{match[1].to_s.strip}." } unless country return { action: :set, country: country } end parts = text.split(/\s+/) country = nil year = nil parts.each do |part| if part.match?(/\A\d{4}\z/) year = part.to_i elsif (normalized = HolidayCalendar.normalize_country(part)) country = normalized else return { action: :error, error: usage_message } end end { action: :show, country: country, year: year } end
Source
# File plugins/holidays.rb, line 28 def self.usage_message "Usage: !holidays [country|year] โ !holidays set <country>" end
Public Instance Methods
Source
# File plugins/holidays.rb, line 109 def execute(m, args = nil) return unless m.channel parsed = self.class.parse_command(args) if parsed[:action] == :error themed_flood_safe_reply(m, parsed[:error]) return end network = bot.config.server channel = m.channel.name state = HolidayStore.load_state(network: network, channel: channel) if parsed[:action] == :set state = state.merge("country" => parsed[:country]) HolidayStore.save_state(network: network, channel: channel, state: state) themed_flood_safe_reply(m, format_set_ack(parsed[:country])) return end country = parsed[:country] || state["country"] themed_flood_safe_reply( m, self.class.lookup( country: country, year: parsed[:year], fetch: method(:fetch_holidays) ) ) end
Source
# File plugins/holidays.rb, line 81 def fetch_holidays(url) RabbotHttp.fetch(url, user_agent: HolidayCalendar::USER_AGENT) end
Source
# File plugins/holidays.rb, line 85 def poll_holidays return unless bot network = bot.config.server bot.config.channels.each do |channel_name| channel = bot.channels.find { |entry| entry.name.casecmp?(channel_name.to_s) } next unless channel state = HolidayStore.load_state(network: network, channel: channel_name) next unless HolidayCalendar.due_for_check?(state) result = HolidayCalendar.poll_channel( state, country: state["country"], fetch: method(:fetch_holidays), now: Time.now ) HolidayStore.save_state(network: network, channel: channel_name, state: result[:state]) result[:announcements].each do |entry| themed_flood_safe_reply(channel, entry[:text]) end end end