class Auspost
Constants
- PLUGIN
- REFRESH_INTERVAL_SEC
- STATE_KEY
- USER_AGENT
Public Class Methods
Source
# File plugins/auspost.rb, line 78 def self.add_parcel(state, tracking_id:, added_by:, scrape:) AuspostWatch.add_parcel( state, tracking_id: tracking_id, added_by: added_by, scrape: scrape ) end
Source
# File plugins/auspost.rb, line 25 def self.command_pattern /auspost(?:\s+(.+))?$/i end
Source
# File plugins/auspost.rb, line 62 def self.default_state AuspostWatch.default_state end
Source
# File plugins/auspost.rb, line 74 def self.due_for_refresh?(state, now: Time.now, interval_sec: REFRESH_INTERVAL_SEC) AuspostWatch.due_for_refresh?(state, now: now, interval_sec: interval_sec) end
Source
# File plugins/auspost.rb, line 95 def self.format_list(state) AuspostReport.format_list(state["parcels"], interval_min: REFRESH_INTERVAL_SEC / 60) end
Source
# File plugins/auspost.rb, line 99 def self.info(tracking_id:, scrape: nil, fetch: nil) diagnosis = AuspostDiagnose.run(tracking_id: tracking_id, scrape: scrape, fetch: fetch) return { error: diagnosis[:error] } if diagnosis[:error] { text: AuspostReport.format_info_card(diagnosis) } end
Source
# File plugins/auspost.rb, line 66 def self.load_state(network:, channel:, store: RabbotDb) AuspostWatch.load_state(network: network, channel: channel, store: store) end
Source
# File plugins/auspost.rb, line 37 def self.parse_command(args) text = args.to_s.strip return { action: :show } if text.empty? return { action: :list } if text.casecmp?("list") if (match = text.match(/\Ainfo(?:\s+(\S+))?\z/i)) tracking_id = AuspostTrack.normalize_tracking_id(match[1]) return { error: usage_message } unless tracking_id return { action: :info, tracking_id: tracking_id } end if (match = text.match(/\Arem(?:ove)?\s+(\S+)\z/i)) tracking_id = AuspostTrack.normalize_tracking_id(match[1]) return { error: usage_message } unless tracking_id return { action: :rem, tracking_id: tracking_id } end tracking_id = AuspostTrack.normalize_tracking_id(text) return { action: :add, tracking_id: tracking_id } if tracking_id { error: usage_message } end
Source
# File plugins/auspost.rb, line 91 def self.refresh_state(state, scrape:, now: Time.now) AuspostWatch.refresh_state(state, scrape: scrape, now: now) end
Source
# File plugins/auspost.rb, line 87 def self.remove_parcel(state, tracking_id:) AuspostWatch.remove_parcel(state, tracking_id: tracking_id) end
Source
# File plugins/auspost.rb, line 70 def self.save_state(network:, channel:, state:, store: RabbotDb) AuspostWatch.save_state(network: network, channel: channel, state: state, store: store) end
Source
# File plugins/auspost.rb, line 33 def self.usage_message "Usage: !auspost <tracking_id> | list | rem <tracking_id> | info <tracking_id>" end
Public Instance Methods
Source
# File plugins/auspost.rb, line 110 def default_scrape(tracking_id) AuspostScrape.scrape(tracking_id) end
Source
# File plugins/auspost.rb, line 134 def execute(m, args) return unless m.channel parsed = self.class.parse_command(args) if parsed[:error] m.reply parsed[:error] return end network = bot.config.server channel = m.channel.name state = self.class.load_state(network: network, channel: channel) case parsed[:action] when :list themed_flood_safe_reply(m, self.class.format_list(state)) when :add result = self.class.add_parcel( state, tracking_id: parsed[:tracking_id], added_by: m.user.nick, scrape: scrape_tracking ) if result[:error] m.reply result[:error] return end self.class.save_state(network: network, channel: channel, state: result[:state]) themed_flood_safe_reply(m, result[:text]) when :rem result = self.class.remove_parcel(state, tracking_id: parsed[:tracking_id]) if result[:error] m.reply result[:error] return end self.class.save_state(network: network, channel: channel, state: result[:state]) m.reply "Parcel removed" when :info result = self.class.info(tracking_id: parsed[:tracking_id], scrape: scrape_tracking) if result[:error] m.reply result[:error] return end themed_flood_safe_reply(m, result[:text]) else if state["parcels"].empty? m.reply self.class.usage_message else themed_flood_safe_reply(m, self.class.format_list(state)) end end end
Source
# File plugins/auspost.rb, line 114 def poll_parcels 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 = self.class.load_state(network: network, channel: channel_name) next unless self.class.due_for_refresh?(state) next if state["parcels"].empty? result = self.class.refresh_state(state, scrape: scrape_tracking) self.class.save_state(network: network, channel: channel_name, state: result[:state]) result[:announcements].each do |entry| channel.send(entry[:text]) end end end
Source
# File plugins/auspost.rb, line 106 def scrape_tracking method(:default_scrape) end