module SunbusRss
Constants
- FEED_BUS
- FEED_NOTICES
- MAX_ITEMS
- USER_AGENT
Public Instance Methods
Source
# File lib/sunbus/rss.rb, line 71 def alerts(region:, fetch: nil, limit: MAX_ITEMS) body = fetch_feed(FEED_BUS, fetch: fetch) filter_by_region(parse_items(body), region: region).first(limit) end
Source
# File lib/sunbus/rss.rb, line 60 def ascii_title(text) text.to_s.gsub(/[^\x20-\x7E]/, "").strip end
Source
# File lib/sunbus/rss.rb, line 20 def fetch_feed(url, fetch: nil) if fetch fetch.call(url) else RabbotHttp.fetch(url, user_agent: USER_AGENT) end end
Source
# File lib/sunbus/rss.rb, line 64 def filter_by_region(items, region:) label = SunbusConfig.region_rss_category(region) items.select do |item| item[:categories].any? { |category| category.match?(/\b#{Regexp.escape(label)}\b/i) } end end
Source
# File lib/sunbus/rss.rb, line 32 def item_categories(node) REXML::XPath.match(node, ".//*[local-name()='category']").filter_map do |category| text = category.text.to_s.strip text unless text.empty? end end
Source
# File lib/sunbus/rss.rb, line 76 def notices(region:, fetch: nil, limit: MAX_ITEMS) body = fetch_feed(FEED_NOTICES, fetch: fetch) filter_by_region(parse_items(body), region: region).first(limit) end
Source
# File lib/sunbus/rss.rb, line 39 def parse_item(node) title = ascii_title(rss_element_text(node, "title")) return nil if title.empty? { title: title, link: rss_element_text(node, "link"), description: rss_element_text(node, "description"), categories: item_categories(node) } end
Source
# File lib/sunbus/rss.rb, line 51 def parse_items(xml) doc = REXML::Document.new(xml.to_s) REXML::XPath.match(doc, "//*[local-name()='item' or local-name()='entry']").filter_map do |node| parse_item(node) end rescue REXML::ParseException, StandardError [] end
Source
# File lib/sunbus/rss.rb, line 28 def rss_element_text(node, name) REXML::XPath.first(node, ".//*[local-name()='#{name}']")&.text.to_s.strip end