module BomWarnings
Constants
- INDEX_URL
- STYLE_TITLE
Public Instance Methods
Source
# File lib/bom_warnings.rb, line 13 def fetch_index(fetch: RabbotHttp.method(:fetch)) fetch.call(INDEX_URL) end
Source
# File lib/bom_warnings.rb, line 37 def format_report(state:, items:) title = state.to_s.strip.empty? ? "Australia" : state.upcase header = IrcFormat.report_header(tag: "WARN", title: title, style: STYLE_TITLE) if items.empty? return [header, "No current warnings found"].join("\n") end lines = items.map.with_index(1) { |item, index| "#{index}. #{item[:title]}" } footer = IrcFormat.report_footer("#{items.length} warnings", "Bureau of Meteorology") ([header] + lines + [footer]).join("\n") end
Source
# File lib/bom_warnings.rb, line 49 def lookup(state: nil, fetch: RabbotHttp.method(:fetch)) html = fetch_index(fetch: fetch) items = parse_warnings(html, state: state) format_report(state: state, items: items) rescue StandardError "Could not fetch BOM warnings" end
Source
# File lib/bom_warnings.rb, line 17 def parse_warnings(html, state: nil) doc = Nokogiri::HTML(html.to_s) items = doc.css("ul li a, .list li a").map do |link| title = link.text.to_s.squeeze(" ").strip href = link["href"].to_s next if title.empty? { title: title, href: href } end.compact if state needle = state.to_s.downcase items = items.select do |item| item[:title].downcase.include?(needle) || item[:href].downcase.include?(needle) end end items.uniq { |item| item[:title] }.first(8) end